I have the following schema (from a bigger xml) which defines a node as mandatory:
However since there are many optional values in the xml I'd like to serialize without the default values (which are None in my case).
That's why I call to_xml with skip_empty set to True.
Unfortunately this also removes the required node thus producing an invalid xml.
from pydantic_xml import BaseXmlModel, attr, element
class Codes(BaseXmlModel, tag='codes'):
codes: list[str] = element(tag='code')
class MyData(BaseXmlModel, tag='MyData'):
revision: str = attr()
codes: Codes
d = MyData(revision='MyData 1', codes=Codes(codes=[]))
for line in d.to_xml(pretty_print=True, encoding='utf-8', skip_empty=True).decode().splitlines():
print(line)
produces
<MyData revision="MyData 1"/>
As you can see codes is not optional but gets wrongly removed.
I would like to have
<MyData revision="MyData 1">
<codes />
</MyData>
Would it be possible to add another parameter skip_none or skip_unset like in pydantic which would work accordingly?
I have the following schema (from a bigger xml) which defines a node as mandatory:![grafik](https://github.com/dapper91/pydantic-xml/assets/10754716/36358ac4-8e93-4cc3-8738-c07166e74d38)
However since there are many optional values in the xml I'd like to serialize without the default values (which are
None
in my case). That's why I callto_xml
withskip_empty
set toTrue
. Unfortunately this also removes the required node thus producing an invalid xml.produces
As you can see
codes
is not optional but gets wrongly removed.I would like to have
Would it be possible to add another parameter
skip_none
orskip_unset
like in pydantic which would work accordingly?