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

[Feature Request] Add exclude_unset and exclude_none options to the to_xml method #203

Closed
maya-brandi opened this issue Aug 15, 2024 · 2 comments
Labels
enhancement New feature or request v2 Version 2 related

Comments

@maya-brandi
Copy link

maya-brandi commented Aug 15, 2024

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()
@maya-brandi 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
@dapper91 dapper91 added enhancement New feature or request v2 Version 2 related labels Aug 24, 2024
@dapper91
Copy link
Owner

@maya-brandi Hi

Implemented this feature in version 2.12.0.

@maya-brandi
Copy link
Author

Fantastic! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v2 Version 2 related
Projects
None yet
Development

No branches or pull requests

2 participants