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

Parsing error when nullable elements with empty value and "xsi:nil"=true attribute #54

Closed sloncho closed 5 months ago

sloncho commented 11 months ago

It appears that the generated code for nullable property getter only inspects if the element exists. It fails when the element exists with attribute "xsi:nil"=true.

Example: xsd: <xsd:element name="createDate" type="xsd:dateTime" minOccurs="0" nillable="true" />

Incoming xml:

<someType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
      <createDate xsi:nil="true"/>
 </someType>

When accessing the generated createDate property, it throws, as it finds the element, and tries to parse the empty value to DateTime.

As XDocument/XElement are not schema-aware, I'm not even sure which approach for solving this will be more appropriate - inspecting for the nil=true, or using the XElement.IsEmpty to detect the null value.

hieu-informatics commented 5 months ago

Do you have any update for this serious bug? I suppose GetElement should apply the same check on nil attribute as done for SetElement.

    internal void SetElement(XName name, object value, bool addToExisting, XmlSchemaDatatype datatype, Type elementBaseType)
    {
        XElement untyped = GetUntyped();
        CheckXsiNil(untyped);
...

    private void CheckXsiNil(XElement parentElement)
    {
        XAttribute xAttribute = parentElement.Attributes(XName.Get("nil", "http://www.w3.org/2001/XMLSchema-instance")).FirstOrDefault();
        if (xAttribute != null && xAttribute.Value == "true")
        {
            xAttribute.Remove();
        }
    }
hieu-informatics commented 5 months ago

@mamift Do you have some plan for this bug fix?

mamift commented 5 months ago

https://www.nuget.org/packages/LinqToXsdCore/3.4.3 https://www.nuget.org/packages/XObjectsCore/3.4.3