Skip to content
Merged

Dev #166

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.8.1 (2024-01-21)
------------------

- union collection deserialization bug fixed. See https://github.com/dapper91/pydantic-xml/pull/165.


2.8.0 (2024-01-13)
------------------

Expand Down
10 changes: 10 additions & 0 deletions pydantic_xml/element/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def apply_snapshot(self, snapshot: 'XmlElement[Any]') -> None:
Applies a snapshot to the current element.
"""

@abc.abstractmethod
def step_forward(self) -> None:
"""
Increment the current element index.
"""

@abc.abstractmethod
def to_native(self) -> Any:
"""
Expand Down Expand Up @@ -305,6 +311,7 @@ def create_snapshot(self) -> 'XmlElement[NativeElement]':
attributes=dict(self._state.attrib) if self._state.attrib is not None else None,
elements=[element.create_snapshot() for element in self._state.elements],
nsmap=dict(self._nsmap) if self._nsmap is not None else None,
sourceline=self._sourceline,
)
element._state.next_element_idx = self._state.next_element_idx

Expand All @@ -319,6 +326,9 @@ def apply_snapshot(self, snapshot: 'XmlElement[NativeElement]') -> None:
self._state.elements = snapshot._state.elements
self._state.next_element_idx = snapshot._state.next_element_idx

def step_forward(self) -> None:
self._state.next_element_idx += 1

def is_empty(self) -> bool:
if not self._state.text and not self._state.tail and not self._state.attrib and len(self._state.elements) == 0:
return True
Expand Down
1 change: 1 addition & 0 deletions pydantic_xml/serializers/factories/union.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def deserialize(
last_error = e

if last_error is not None:
element.step_forward()
raise last_error

return result
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydantic-xml"
version = "2.8.0"
version = "2.8.1"
description = "pydantic xml extension"
authors = ["Dmitry Pershin <dapper1291@gmail.com>"]
license = "Unlicense"
Expand Down
47 changes: 47 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,50 @@ class TestModel(BaseXmlModel, tag='model'):
},
},
]


def test_models_union_errors():
class TestSubModel1(BaseXmlModel, tag='submodel1'):
data: int

class TestSubModel2(BaseXmlModel, tag='submodel2'):
data: float

class TestModel(BaseXmlModel, tag='model'):
submodel: List[Union[TestSubModel1, TestSubModel2]]

xml = '''
<model>
<submodel2>a</submodel2>
<submodel1>b</submodel1>
</model>
'''

with pytest.raises(pd.ValidationError) as exc:
TestModel.from_xml(xml)

err = exc.value
assert err.title == 'TestModel'
assert err.error_count() == 2
assert err.errors() == [
{
'input': 'a',
'loc': ('submodel', 0, 'data'),
'msg': f'[line {fmt_sourceline(3)}]: Input should be a valid number, unable to parse string as a number',
'type': 'float_parsing',
'ctx': {
'orig': 'Input should be a valid number, unable to parse string as a number',
'sourceline': fmt_sourceline(3),
},
},
{
'input': 'b',
'loc': ('submodel', 1, 'data'),
'msg': f'[line {fmt_sourceline(4)}]: Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
'ctx': {
'orig': 'Input should be a valid integer, unable to parse string as an integer',
'sourceline': fmt_sourceline(4),
},
},
]