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
Hi! It would be great to have exclude_unset and exclude_none options in the BaseXmlModel.to_xml method. Just as in Pydantics BaseModel.model_dump. I currently have a problem that I dont know how to solve without that option. skip_empty is not enough.
Im including a test to describe the issue.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "lxml",
# "pydantic-xml",
# ]
# ///
import unittest
from unittest import TestCase
from lxml import etree
from pydantic_xml import BaseXmlModel, element
VACCINE_XSD = """\
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="LimitedString4Digits">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{4}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="VaccineType">
<xsd:sequence>
<xsd:element name="batchNumber" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="municipalityScbCode" type="LimitedString4Digits" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Vaccine" type="VaccineType">
</xsd:element>
</xsd:schema>
"""
xsd_schema = etree.XMLSchema(etree.fromstring(VACCINE_XSD))
class VaccineType(
BaseXmlModel,
nsmap={"xsi": "http://www.w3.org/2001/XMLSchema-instance"},
tag="Vaccine",
):
batch_number: str = element(tag="batchNumber")
municipality_scb_code: str | None = element(
tag="municipalityScbCode", default=None, pattern="[0-9]{4}"
)
class TestSkipEmpty(TestCase):
"""
Test the same document validated against the same XSD,
with different settings for `skip_empty`.
These two tests will fail in different ways.
"""
def test_skip_empty_true(self) -> None:
vaccine = VaccineType(batch_number="")
# When `skip_empty = True`, `batchNumber` is skipped when empty,
# but it is mandatory.
vaccine_xml = vaccine.to_xml(skip_empty=True)
xsd_schema.assertValid(etree.fromstring(vaccine_xml))
def test_skip_empty_false(self) -> None:
vaccine = VaccineType(batch_number="")
# When `skip_empty = False`, `municipalityScbCode` is included,
# but it's not allowed when empty.
vaccine_xml = vaccine.to_xml(skip_empty=False)
xsd_schema.assertValid(etree.fromstring(vaccine_xml))
if __name__ == "__main__":
unittest.main()
The text was updated successfully, but these errors were encountered:
maya-brandi
changed the title
add exclude_unset and exclude_none options to the to_xml method
[Feature Request] Add exclude_unset and exclude_none options to the to_xml method
Aug 23, 2024
Hi! It would be great to have
exclude_unset
andexclude_none
options in the BaseXmlModel.to_xml method. Just as in Pydantics BaseModel.model_dump. I currently have a problem that I dont know how to solve without that option.skip_empty
is not enough.Im including a test to describe the issue.
The text was updated successfully, but these errors were encountered: