Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Stack overflow in XmlSchemaClassGenerator.ModelBuilder.CreatePropertiesForAttributes #299

Closed
HofmeisterAn opened this issue Jan 16, 2022 · 6 comments

Comments

@HofmeisterAn
Copy link

I'm trying to generate XmlSerializer classes for xhtml-inlinexbrl-1_1.xsd. Unfortunately, it does not work. It cancels the generation with a stack overflow in XmlSchemaClassGenerator.ModelBuilder.CreatePropertiesForAttributes. I think it relates to the recursive call:

var groupProperties = CreatePropertiesForAttributes(source, typeModel, groupItems.Cast<XmlSchemaObject>());

I get following error:

at System.Collections.ArrayList.GetEnumerator()
at System.Collections.CollectionBase.GetEnumerator()
at System.Linq.Enumerable+<CastIterator>d__63`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
at XmlSchemaClassGenerator.ModelBuilder.CreatePropertiesForAttributes(System.Uri, XmlSchemaClassGenerator.TypeModel, System.Collections.Generic.IEnumerable`1<System.Xml.Schema.XmlSchemaObject>)
at XmlSchemaClassGenerator.ModelBuilder.CreatePropertiesForAttributes(System.Uri, XmlSchemaClassGenerator.TypeModel, System.Collections.Generic.IEnumerable`1<System.Xml.Schema.XmlSchemaObject>)
at XmlSchemaClassGenerator.ModelBuilder.CreatePropertiesForAttributes(System.Uri, XmlSchemaClassGenerator.TypeModel, System.Collections.Generic.IEnumerable`1<System.Xml.Schema.XmlSchemaObject>)
...

CreatePropertiesForAttributes gets called with following arguments: debug.log. Please notice the end of the log file, it always calles xhtml-image-1.xsd. Any idea what could go wrong?

To generate the classes I use:

var generator = new Generator();
generator.Log = message => this.outputStream.WriteLine(message);
generator.OutputFolder = OutputDirectory;
generator.Generate(Directory.EnumerateFiles(SchemaDirectory, "xhtml-inlinexbrl-1_1.xsd", SearchOption.TopDirectoryOnly));
@mganss
Copy link
Owner

mganss commented Jan 18, 2022

I've fixed this in 2.0.633. When generating, you still need to specifiy GenerateInterfaces = false (-t- on the command line). This because of the issue described here: #240 (comment)

Do you have any test XML files so we can incorporate them into the unit tests?

@HofmeisterAn
Copy link
Author

HofmeisterAn commented Jan 19, 2022

Do you have any test XML files so we can incorporate them into the unit tests?

I use this XHTML to test. There are even repots that include styles like this one.

🙏 Thanks a lot. The fix in 2.0.633 works. At least I'm able to generate classes and deserialize the XHTML file with some workarounds (even if some parts are missing). I haven't done anything with such complex XML schemas before. It's difficult for me to be sure that I haven't done anything wrong 😄.

After I generated the classes for xhtml-inlinexbrl-1_1.xsd, the class Xlink.cs had some missing enums (Xlink.Type, Xlink.Show, and Xlink.Actuate). I used XmlSchemaClassGenerator again and generated the classes for xlink.xsd. I renamed the generated class file, moved it to the project and renamed the enums (TypeType to Type, etc.). After that I was able to deserialize:

var serializer = new XmlSerializer(typeof(XhtmlPeriodHtmlPeriodType));
(XhtmlPeriodHtmlPeriodType)serializer.Deserialize(xhtmlStream)

Do you think I can replace xlink-2003-12-31.xsd with xlink.xsd to avoid the workaround in the future?


Edit: Any chances to get "Groups with maxOccurs > 0" to work? I think that's another issue I have. It looks like the HTML elements only occurs once, e.g. the body only has one div, h1, h2, etc.

If I change

[System.Xml.Serialization.XmlElementAttribute("div")]
public Xhtml.XhtmlPeriodDivPeriodType Div { get; set; }

to

[System.Xml.Serialization.XmlElementAttribute("div")]
public List<Xhtml.XhtmlPeriodDivPeriodType> Div { get; set; }

I get all divs.

@mganss
Copy link
Owner

mganss commented Jan 20, 2022

Regarding the first part (XLink): It works out of the box if you specify an individual C# namespace for the XML namespace http://www.xbrl.org/2003/XLink (e.g. by specifying -nhttp://www.xbrl.org/2003/XLink=XbrlLink on the command line). By default, both http://www.w3.org/1999/xlink and http://www.xbrl.org/2003/XLink are mapped to the same C# namespace.

Still investigating the second issue.

@HofmeisterAn
Copy link
Author

By default, both http://www.w3.org/1999/xlink and http://www.xbrl.org/2003/XLink are mapped to the same C# namespace.

Thanks. I added a custom namespace generator. Now, it creates the classes for both schemas 👍 .

mganss pushed a commit that referenced this issue Jan 21, 2022
Add XBRL test
@mganss
Copy link
Owner

mganss commented Jan 21, 2022

Found the cause of the other issue. My original fix wasn't actually a fix but merely made the symptoms of not handling redefines disappear. I've now added proper support for redefines in 2.0.634 which fixes the issue (at least for this schema).

I've noticed that the Schaltbau.xhtml file isn't schema compliant: It uses lowercase elements which are defined as mixed case in the schema, e.g. nonnumeric vs nonNumeric. A fixed version is here: https://github.com/mganss/XmlSchemaClassGenerator/blob/master/XmlSchemaClassGenerator.Tests/xml/xbrl_tests/Schaltbau.xhtml

@HofmeisterAn
Copy link
Author

HofmeisterAn commented Jan 21, 2022

I really appreciate your help, thanks a lot. I'm sorry that I couldn't contribute more. Maybe I can create a pull request with my namespace generator. It automatically appends and increases a number if the same namespace is used among different xsd files. 

http://www.w3.org/1999/xlink + xlink-2003-12-31.xsdXlink.cs
http://www.xbrl.org/2003/XLink + xl-2003-12-31.xsdXlink2.cs
http://www.example.com/2022/XLink + foo.xsdXlink3.cs

Version 2.0.634 works fine 👍 Now I need to think about a good way to reduce and map the data.

If nothing is left on your side, we can close the issue. Thanks again.

@mganss mganss closed this as completed Jan 24, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants