mamift / LinqToXsdCore

LinqToXsd ported to .NET Core (targets .NET Standard 2 for generated code and .NET Core 3.1, .NET 5+ for the code generator CLI tool).
Microsoft Public License
41 stars 15 forks source link

Fix: enums should be considered value types #21

Closed jods4 closed 3 years ago

jods4 commented 3 years ago

Fixes #20

The problem was that enums are strings with restrictions in the XSD, and because string is a ref type, this evaluated to false in ClrTypeInfo (514):

                if (datatype.ValueType.IsValueType)
                {
                    typeRefFlags |= ClrTypeRefFlags.IsValueType;
                }

So I made sure ClrTypeInfo considers enums as value types as well, which only seem to impact ClrPropertyInfo codegen.

I did a before/after on the large pain.002.001.03.xsd that I referred to in my bug report.

The only differences are optional / choice enum properties, which now look like this: image

This is perfectly in line with the codegen for similar value types such as DateTime (unchanged):

        /// <summary>
        /// <para>
        /// Occurrence: optional
        /// </para>
        /// <para>
        /// Regular expression: (Tp?, Nb?, RltdDt?)
        /// </para>
        /// </summary>
        public virtual System.Nullable<System.DateTime> RltdDt {
            get {
                XElement x = this.GetElement(System.Xml.Linq.XName.Get("RltdDt", "urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"));
                if ((x == null)) {
                    return null;
                }
                return XTypedServices.ParseValue<System.DateTime>(x, XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Date).Datatype);
            }
            set {
                this.SetElementWithValidation(System.Xml.Linq.XName.Get("RltdDt", "urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"), value, "RltdDt", global::Iso20022.Pain_002_001_03.ISODate.TypeDefinition);
            }
        }

I'm attaching the schema and a before/after for your convenience. Test.zip

jods4 commented 3 years ago

A few tests are failing but it seems to me they were failing before and a quick debug of the first test (EnumAtNamespaceLevelGenerationTest) seems to indicate that the test might just be old/incorrect (for instance in this test, the expected number of 46 enums just doesn't seem right when looking at the codegen).

mamift commented 3 years ago

Hi thanks for this. And yes the EnumAtNamespaceLevelGenerationTest has faulty logic for the time being, so its safe to ignore.