question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nillable fields are skipped by XmlSerializer

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tefracommented, Feb 15, 2021

Thank you for reporting @barsv the fix is on master and will be included in the next release.

0reactions
tefracommented, Feb 13, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found