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
43 stars 15 forks source link

Impossible to set nullable properties to null #22

Closed jods4 closed 3 years ago

jods4 commented 3 years ago

The generated setter for a nullable DateTime looks like this:

        public virtual System.Nullable<System.DateTime> RltdDt {
            get { /* ... */ }
            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);
            }
        }

Trying to remove such an element by setting it to null fails with an exception. The setter calls SetElementWithValidation with value null and the validation first performed by this method fails, although the code after validation would correctly remove the element upon receiving a null value.

Notice that the situation is slightly worse for an enum, because the generated code passes value.ToString() to SetElementWithValidation and that .ToString() would fail even earlier.

jods4 commented 3 years ago

I'm working on this. Do you happen to know why validation of attribute values is disabled? https://github.com/mamift/LinqToXsdCore/blob/master/XObjectsCode/Src/ClrPropertyInfo.cs#L868

(not gonna change this in the PR but it struck me as odd?)

jods4 commented 3 years ago

It also struck me as odd that the ToString call on enums is only on the xNameParm branches. https://github.com/mamift/LinqToXsdCore/blob/master/XObjectsCode/Src/ClrPropertyInfo.cs#L928

xNameParm actually indicates whether the origin of the value is element/attribute vs text content. When xNameParm is false, the the origin is text content and SetValue is called. https://github.com/mamift/LinqToXsdCore/blob/master/XObjectsCode/Src/ClrPropertyInfo.cs#L872-L875

Should that text content be typed as an enum, I don't see why it shouldn't be formatted ToString the same way it would be in an attribute or child element. I'm changing this as it seems like a logical thing to do + it makes my new code easier.

jods4 commented 3 years ago

Done in #28, as well as another NRE crash unrelated to validation when setting nullable enum attributes.

mamift commented 3 years ago

I'm working on this. Do you happen to know why validation of attribute values is disabled? https://github.com/mamift/LinqToXsdCore/blob/master/XObjectsCode/Src/ClrPropertyInfo.cs#L868

(not gonna change this in the PR but it struck me as odd?)

I know I'm replying after the fact, but to address your question, no I don't know why. But agree with your change, and that it's odd.

jods4 commented 3 years ago

Just to be sure there is no misunderstanding:

But agree with your change

I left attribute validation disabled as I was afraid of the reason why it was disabled in the first place: https://github.com/mamift/LinqToXsdCore/blob/master/XObjectsCode/Src/ClrPropertyInfo.cs#L871