nillable fields are skipped by XmlSerializer
See original GitHub issueHi,
I have nillable=“true” in my xsd. I generated a python class for this xsd. I serialize the class into xml using XmlSerializer. My problem is that the nillable field is missing in the xml. I have
"required": True,
"nillable": True,
in the generated code for the nillable field. So I think that XmlSerializer should generate an empty tag for this field.
I don’t know if it’s a bug or not. Could you please suggest.
More details:
I created a small repo to reproduce: https://github.com/barsv/xsdata-issue.git
My test.xsd file:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I generated test.py using xsdata test.xsd
:
@dataclass
class Shiporder:
class Meta:
name = "shiporder"
name: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "",
"required": True,
"nillable": True,
}
)
I serialize it using the following code:
config = SerializerConfig(pretty_print=True)
xml_serializer = XmlSerializer(config=config)
order = Shiporder()
#order.name = "test"
xml = xml_serializer.render(order)
print(xml)
and I get the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<shiporder/>
This xml doesn’t pass validation against the original xsd.
I would expect to get the following xml instead:
<?xml version="1.0" encoding="UTF-8"?>
<shiporder>
<name/>
</shiporder>
ps: I’m not an expert in neither xsd nor python.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Serialize nullable type to optional non-nillable element
In the data model I would like to map this to a field of .net type Nullable<int> , where a null value should...
Read more >XmlSerializer For Nullable Types - Microsoft Q&A
I have a class that contains the following properties: [XmlAttribute()] public int? HorizontalSum { get; set; }; [XmlAttribute()] public int ...
Read more >How to parse nillable fields in XML. - TIBCO Support
There are at least two ways to enable the nillable element. One way is by adding the following lines into your schema:
Read more >Required fields in the XML patron schema
Example code from the XML Patron Schema. This (institutionID) is optional: <xs:element name="institutionID" nillable="true" minOccurs="0"> ...
Read more >XML Schema nillable=”true” vs minOccurs=”0″ | Dimuthu's Blog
The opposite of the above scenario is nillable=”true” but minOccurrs != 0. Check the 'nilint' element in the schema. There you can't skip...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you for reporting @barsv the fix is on master and will be included in the next release.
No leave it open I am planning to change this behavior in the next release, to avoid the empty string hack I just need to test a few more things