dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
141 stars 14 forks source link

optional raw element bug fixed. #160

Closed dapper91 closed 5 months ago

dapper91 commented 5 months ago

Fixes the bug with optional raw element.

Example:

>>> class Model(BaseXmlModel, tag='model', arbitrary_types_allowed=True):
...     element1: Optional[ElementT] = element(default=None)
...
>>> Model().to_xml()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/model.py", line 399, in to_xml
    return etree.tostring(self.to_xml_tree(skip_empty=skip_empty), **kwargs)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/model.py", line 380, in to_xml_tree
    self.__xml_serializer__.serialize(
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/serializers/factories/model.py", line 170, in serialize
    field_serializer.serialize(
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/serializers/factories/raw.py", line 38, in serialize
    sub_element = element.from_native(value)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pydantic_xml/element/native/lxml.py", line 22, in from_native
    tag=element.tag,
        ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'tag'

Expected behavior:

>>> from pydantic_xml import BaseXmlModel, element
>>> from xml.etree.ElementTree import Element
>>> from typing import Optional
>>> 
>>> class Model(BaseXmlModel, tag='model', arbitrary_types_allowed=True):
...     element1: Optional[Element] = element(default=None)
... 
>>> print(Model().to_xml().decode())
<model/>

fixes the issue https://github.com/dapper91/pydantic-xml/issues/158

codecov-commenter commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (73228d7) 91.71% compared to head (c5e77d5) 91.77%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #160 +/- ## ========================================== + Coverage 91.71% 91.77% +0.06% ========================================== Files 26 26 Lines 1508 1508 ========================================== + Hits 1383 1384 +1 + Misses 125 124 -1 ``` | [Flag](https://app.codecov.io/gh/dapper91/pydantic-xml/pull/160/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/dapper91/pydantic-xml/pull/160/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin) | `91.77% <100.00%> (+0.06%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.