You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optional complex element with default value is always instantiated and serialized
#645
I'm running into an issue with an optional complex element that has a default value. I'm not sure whether this is intended behavior or an edge case in the generator.
I'm using XmlSchemaClassGenerator 3.0.1356.
Given an XSD element similar to this:
where:
XmlSchemaClassGenerator generates a property with a backing field initialized approximately like this:
private GeometryTypeThicknessReduction _thicknessReduction =
new GeometryTypeThicknessReduction { Value = false };
[XmlElement("thicknessReduction")]
public GeometryTypeThicknessReduction ThicknessReduction
{
get { return _thicknessReduction; }
set { _thicknessReduction = value; }
}
The generated wrapper class is essentially:
public partial class GeometryTypeThicknessReduction
{
[XmlText]
public bool Value { get; set; }
[XmlAttribute("reference")]
public ThicknessReductionReferences Reference { get; set; }
}
The problem is that minOccurs="0" allows the element to be absent, but the generated CLR property is never null by default.
This becomes especially noticeable when round-tripping XML.
For example, suppose the original XML does not contain thicknessReduction at all:
...
After deserializing it using XmlSerializer, ThicknessReduction contains the object created by the field initializer with Value == false.
When the object is serialized again, a thicknessReduction element is therefore written even though it was absent from the original XML:
There is an additional issue here because reference is a required attribute represented by an enum. The newly created object has the CLR default enum value, so the first enum member (REDUCEWITHSHAPE) can also be serialized even though no reference was supplied by the original XML.
My understanding of XSD element defaults is that default="false" does not imply that an optional element with minOccurs="0" is present when it is absent from the XML. Therefore there seems to be an important distinction between:
the element being absent, and
the element being present with the value false.
Initializing the complex CLR property in order to represent the XSD default appears to lose that distinction.
For comparison, if I remove default="false" from the XSD, the property can remain null and deserialize/serialize round-tripping preserves the absence of the element.
Is the eager initialization of an optional complex/simple-content element with an XSD default intentional?
If so, is there an option in XmlSchemaClassGenerator to preserve element absence in this situation?
If not, would it make sense for optional complex elements (minOccurs="0") not to be instantiated solely because their simple content has an XSD default?
The actual schema where I encountered this is B2BOptic LensOrder 1.6.6, but the issue seems independent of B2BOptic and reproducible with the simplified schema above.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
I'm running into an issue with an optional complex element that has a default value. I'm not sure whether this is intended behavior or an edge case in the generator.
I'm using XmlSchemaClassGenerator 3.0.1356.
Given an XSD element similar to this:
where:
XmlSchemaClassGenerator generates a property with a backing field initialized approximately like this:
The generated wrapper class is essentially:
The problem is that
minOccurs="0"allows the element to be absent, but the generated CLR property is never null by default.This becomes especially noticeable when round-tripping XML.
For example, suppose the original XML does not contain
thicknessReductionat all:After deserializing it using
XmlSerializer,ThicknessReductioncontains the object created by the field initializer withValue == false.When the object is serialized again, a
thicknessReductionelement is therefore written even though it was absent from the original XML:There is an additional issue here because
referenceis a required attribute represented by an enum. The newly created object has the CLR default enum value, so the first enum member (REDUCEWITHSHAPE) can also be serialized even though no reference was supplied by the original XML.My understanding of XSD element defaults is that
default="false"does not imply that an optional element withminOccurs="0"is present when it is absent from the XML. Therefore there seems to be an important distinction between:false.Initializing the complex CLR property in order to represent the XSD default appears to lose that distinction.
For comparison, if I remove
default="false"from the XSD, the property can remain null and deserialize/serialize round-tripping preserves the absence of the element.Is the eager initialization of an optional complex/simple-content element with an XSD default intentional?
If so, is there an option in XmlSchemaClassGenerator to preserve element absence in this situation?
If not, would it make sense for optional complex elements (
minOccurs="0") not to be instantiated solely because their simple content has an XSD default?The actual schema where I encountered this is B2BOptic LensOrder 1.6.6, but the issue seems independent of B2BOptic and reproducible with the simplified schema above.
All reactions