Skip to content

Commit cc9af42

Browse files
authored
Merge pull request #209 from dapper91/dev
- collection of tagged union deserialization bug fixed
2 parents 6e88b73 + b918ae7 commit cc9af42

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
2.12.1 (2024-08-26)
5+
-------------------
6+
7+
- collection of tagged union deserialization bug fixed. See https://github.com/dapper91/pydantic-xml/issues/206
8+
9+
410
2.12.0 (2024-08-24)
511
-------------------
612

pydantic_xml/serializers/factories/heterogeneous.py

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def from_core_schema(schema: pcs.TupleSchema, ctx: Serializer.Context) -> Serial
9393
SchemaTypeFamily.MAPPING,
9494
SchemaTypeFamily.TYPED_MAPPING,
9595
SchemaTypeFamily.UNION,
96+
SchemaTypeFamily.TAGGED_UNION,
9697
SchemaTypeFamily.IS_INSTANCE,
9798
SchemaTypeFamily.CALL,
9899
):

pydantic_xml/serializers/factories/homogeneous.py

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def from_core_schema(schema: HomogeneousCollectionTypeSchema, ctx: Serializer.Co
111111
SchemaTypeFamily.MAPPING,
112112
SchemaTypeFamily.TYPED_MAPPING,
113113
SchemaTypeFamily.UNION,
114+
SchemaTypeFamily.TAGGED_UNION,
114115
SchemaTypeFamily.IS_INSTANCE,
115116
SchemaTypeFamily.CALL,
116117
SchemaTypeFamily.TUPLE,
@@ -122,6 +123,7 @@ def from_core_schema(schema: HomogeneousCollectionTypeSchema, ctx: Serializer.Co
122123
if items_type_family not in (
123124
SchemaTypeFamily.MODEL,
124125
SchemaTypeFamily.UNION,
126+
SchemaTypeFamily.TAGGED_UNION,
125127
SchemaTypeFamily.TUPLE,
126128
SchemaTypeFamily.CALL,
127129
) and ctx.entity_location is None:

pydantic_xml/serializers/factories/named_tuple.py

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def from_core_schema(schema: pcs.CallSchema, ctx: Serializer.Context) -> Seriali
6363
SchemaTypeFamily.MAPPING,
6464
SchemaTypeFamily.TYPED_MAPPING,
6565
SchemaTypeFamily.UNION,
66+
SchemaTypeFamily.TAGGED_UNION,
6667
SchemaTypeFamily.IS_INSTANCE,
6768
SchemaTypeFamily.CALL,
6869
):

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydantic-xml"
3-
version = "2.12.0"
3+
version = "2.12.1"
44
description = "pydantic xml extension"
55
authors = ["Dmitry Pershin <[email protected]>"]
66
license = "Unlicense"

tests/test_unions.py

+40
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,46 @@ class TestModel(RootXmlModel, tag='model'):
390390
assert_xml_equal(actual_xml, xml)
391391

392392

393+
@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python 3.9 and above")
394+
def test_tagged_union_collection():
395+
from typing import Annotated
396+
397+
class SubModel1(BaseXmlModel):
398+
type: Literal['type1'] = attr()
399+
data: int
400+
401+
class SubModel2(BaseXmlModel):
402+
type: Literal['type2'] = attr()
403+
data: str
404+
405+
class TestModel(BaseXmlModel, tag='model'):
406+
collection: List[Annotated[Union[SubModel1, SubModel2], Field(discriminator='type')]] = element('submodel')
407+
408+
xml = '''
409+
<model>
410+
<submodel type="type1">1</submodel>
411+
<submodel type="type2">a</submodel>
412+
<submodel type="type2">b</submodel>
413+
<submodel type="type1">2</submodel>
414+
</model>
415+
'''
416+
417+
actual_obj = TestModel.from_xml(xml)
418+
expected_obj = TestModel(
419+
collection=[
420+
SubModel1(type='type1', data='1'),
421+
SubModel2(type='type2', data='a'),
422+
SubModel2(type='type2', data='b'),
423+
SubModel1(type='type1', data='2'),
424+
],
425+
)
426+
427+
assert actual_obj == expected_obj
428+
429+
actual_xml = actual_obj.to_xml()
430+
assert_xml_equal(actual_xml, xml)
431+
432+
393433
def test_union_snapshot():
394434
class SubModel1(BaseXmlModel, tag='submodel'):
395435
attr1: int = attr()

0 commit comments

Comments
 (0)