mganss / XmlSchemaClassGenerator

Generate C# classes from XML Schema files
Apache License 2.0
603 stars 180 forks source link

Duplicate Attributes if the same xsd:element is listed multiple times in the schema #521

Closed jhancock-taxa closed 4 months ago

jhancock-taxa commented 4 months ago

I can't share the schema because contracts that prevent me from sharing, but basically if there is the same element many times and it merges them, it's duplicating schema attributes.

I.e. the MaxLengthAttribute will get duplicated in the output because every single example has xsd:length value="6" in it in all references to the same thing.

mganss commented 4 months ago

Could you post a (made up) minimal schema that shows the issue?

jhancock-taxa commented 4 months ago

I should also note that if it finds. Min and Max Length it also creates duplicate StringLengthAttributes.

So here's a property that's outputted:

        /// <summary>
        /// <para xml:lang="en">Maximum length: 150.</para>
        /// <para xml:lang="en">Minimum length: 16. Maximum length: 16.</para>
        /// <para xml:lang="en">Pattern: [A-Za-z0-9]*.</para>
        /// </summary>
        [global::System.ComponentModel.DataAnnotations.StringLengthAttribute(150)]
        [global::System.ComponentModel.DataAnnotations.StringLengthAttribute(16, MinimumLength=16)]
        [global::System.ComponentModel.DataAnnotations.RegularExpressionAttribute("[A-Za-z0-9]*")]
        [global::System.Diagnostics.CodeAnalysis.AllowNullAttribute()]
        [global::System.Diagnostics.CodeAnalysis.MaybeNullAttribute()]
        [global::System.Xml.Serialization.XmlElementAttribute("VendorControlNum")]
        public string VendorControlNum { get; set; }

From this in the xsd:

        <xsd:element name="VendorControlNum" minOccurs="0">
            <xsd:simpleType>
                <xsd:restriction base="AlphaNumericType">
                    <xsd:length value="16"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:element>

And then there is another restriction elsewhere:

    <xsd:simpleType name="AlphaNumericType">
        <xsd:restriction base="StringType">
            <xsd:pattern value="[A-Za-z0-9]*" />
            <xsd:maxLength value="150" />
        </xsd:restriction>
    </xsd:simpleType>

Every single definition is identical. Somehow the StringLength(150) is there but that isn't listed anywhere that this field is defined.

From my understanding, these should be similar to how CSS works in that it goes from least to most specific and takes the most specific version that is found in the chain.

Thus this should not use the maxlength in the restriction because it's defined on the field, but should add the pattern

It appears that instead it's adding multiples instead of doing the merging.

jhancock-taxa commented 4 months ago

Also, it appears that this is also doubling the XML documentation in places (along with xml documentation that generates msbuild warnings because if incorrectly escaped docs)

mganss commented 4 months ago

Thanks. This occurred because xs:length restrictions were handled separately from xs:maxLength and xs:minLength restrictions.

mganss commented 4 months ago

along with xml documentation that generates msbuild warnings because if incorrectly escaped docs)

Do you have an example of incorrectly escaped docs?