Skip to content

Mandatory bytes with no default should return an empty byte string #872

@SrirachaHorse

Description

@SrirachaHorse

When parsing documents, xsdata will produce a sensible default for mandatory elements without a provided value. For example, if parsing a mandatory element with the XML type string which has no value, the library will generate an empty string.

For byte strings (for example a base64Binary element), an empty string is also produced, instead of a byte string.

A simple example - with the below schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="2">
    <xsd:element name="foo">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="bar" type="xsd:base64Binary"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

and this example XML file:

<foo>
  <bar/>
</foo>

If I run xsdata generate schema.xsd, then run the following script, I will get a blank string for the bar field, where I would expect an empty bytestring.

from pathlib import Path

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

from generated import Foo

context = XmlContext()
parser = XmlParser(context=context)

example_path = Path('example.xml')
data = parser.parse(example_path, Foo)
print(data)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions