Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty lists do not get serialized #686

Closed
teobouvard opened this issue Jun 28, 2022 · 1 comment · Fixed by #687
Closed

Empty lists do not get serialized #686

teobouvard opened this issue Jun 28, 2022 · 1 comment · Fixed by #687

Comments

@teobouvard
Copy link
Contributor

teobouvard commented Jun 28, 2022

Hi,

When I use the following schema

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Wrapper" type="wrapper"/>

    <xsd:complexType name="wrapper">
        <xsd:sequence>
            <xsd:element name="ListOfNumbers" type="listOfNumbers" />
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:simpleType name="listOfNumbers">
        <xsd:list itemType="xsd:long" />
    </xsd:simpleType>
</xsd:schema>

to create a Wrapper object with an empty ListOfNumbers and I serialize it using the code below

from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig

from generated.schema import Wrapper

obj = Wrapper()
obj.list_of_numbers = []

config = SerializerConfig(pretty_print=True)
serializer = XmlSerializer(config)

print(serializer.render(obj))

The ListOfNumbers element do not get serialized which results in the following XML file.

<?xml version="1.0" encoding="UTF-8"?>
<Wrapper/>

First of all, is this the expected behaviour ? I find it surprising to not have an empty element in the generated XML, which I expected to look somewhat like this

<?xml version="1.0" encoding="UTF-8"?>
<Wrapper>
    <ListOfNumbers/>
</Wrapper>

If this is the expected behaviour, would you be open to a pull request adding an option to serialize empty lists as empty elements rather than discard them?

@teobouvard
Copy link
Contributor Author

I tried reading the generated XML with the following sample, and it works.

from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.context import XmlContext

from generated.schema import Wrapper

parser = XmlParser(context=XmlContext())
wrapper = parser.parse("sample.xml", Wrapper)

The issue is that the generated XML is not compliant with the provided schema.

$ xmllint --noout --schema schema.xsd sample.xml 
sample.xml:2: element Wrapper: Schemas validity error : Element 'Wrapper': Missing child element(s). Expected is ( ListOfNumbers ).
sample.xml fails to validate

teobouvard added a commit to teobouvard/xsdata that referenced this issue Jun 29, 2022
Empty lists were treated as None values, which discarded them from
serialization. This commit fixes this issue by explictly checking for
None values, rather than relying on implicit boolean conversion.

Closes tefra#686
@tefra tefra closed this as completed in #687 Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant