Skip to content

Commit

Permalink
Merge pull request #209 from dapper91/dev
Browse files Browse the repository at this point in the history
- collection of tagged union deserialization bug fixed
  • Loading branch information
dapper91 authored Aug 26, 2024
2 parents 6e88b73 + b918ae7 commit cc9af42
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.12.1 (2024-08-26)
-------------------

- collection of tagged union deserialization bug fixed. See https://github.com/dapper91/pydantic-xml/issues/206


2.12.0 (2024-08-24)
-------------------

Expand Down
1 change: 1 addition & 0 deletions pydantic_xml/serializers/factories/heterogeneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def from_core_schema(schema: pcs.TupleSchema, ctx: Serializer.Context) -> Serial
SchemaTypeFamily.MAPPING,
SchemaTypeFamily.TYPED_MAPPING,
SchemaTypeFamily.UNION,
SchemaTypeFamily.TAGGED_UNION,
SchemaTypeFamily.IS_INSTANCE,
SchemaTypeFamily.CALL,
):
Expand Down
2 changes: 2 additions & 0 deletions pydantic_xml/serializers/factories/homogeneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def from_core_schema(schema: HomogeneousCollectionTypeSchema, ctx: Serializer.Co
SchemaTypeFamily.MAPPING,
SchemaTypeFamily.TYPED_MAPPING,
SchemaTypeFamily.UNION,
SchemaTypeFamily.TAGGED_UNION,
SchemaTypeFamily.IS_INSTANCE,
SchemaTypeFamily.CALL,
SchemaTypeFamily.TUPLE,
Expand All @@ -122,6 +123,7 @@ def from_core_schema(schema: HomogeneousCollectionTypeSchema, ctx: Serializer.Co
if items_type_family not in (
SchemaTypeFamily.MODEL,
SchemaTypeFamily.UNION,
SchemaTypeFamily.TAGGED_UNION,
SchemaTypeFamily.TUPLE,
SchemaTypeFamily.CALL,
) and ctx.entity_location is None:
Expand Down
1 change: 1 addition & 0 deletions pydantic_xml/serializers/factories/named_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def from_core_schema(schema: pcs.CallSchema, ctx: Serializer.Context) -> Seriali
SchemaTypeFamily.MAPPING,
SchemaTypeFamily.TYPED_MAPPING,
SchemaTypeFamily.UNION,
SchemaTypeFamily.TAGGED_UNION,
SchemaTypeFamily.IS_INSTANCE,
SchemaTypeFamily.CALL,
):
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.12.0"
version = "2.12.1"
description = "pydantic xml extension"
authors = ["Dmitry Pershin <[email protected]>"]
license = "Unlicense"
Expand Down
40 changes: 40 additions & 0 deletions tests/test_unions.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,46 @@ class TestModel(RootXmlModel, tag='model'):
assert_xml_equal(actual_xml, xml)


@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python 3.9 and above")
def test_tagged_union_collection():
from typing import Annotated

class SubModel1(BaseXmlModel):
type: Literal['type1'] = attr()
data: int

class SubModel2(BaseXmlModel):
type: Literal['type2'] = attr()
data: str

class TestModel(BaseXmlModel, tag='model'):
collection: List[Annotated[Union[SubModel1, SubModel2], Field(discriminator='type')]] = element('submodel')

xml = '''
<model>
<submodel type="type1">1</submodel>
<submodel type="type2">a</submodel>
<submodel type="type2">b</submodel>
<submodel type="type1">2</submodel>
</model>
'''

actual_obj = TestModel.from_xml(xml)
expected_obj = TestModel(
collection=[
SubModel1(type='type1', data='1'),
SubModel2(type='type2', data='a'),
SubModel2(type='type2', data='b'),
SubModel1(type='type1', data='2'),
],
)

assert actual_obj == expected_obj

actual_xml = actual_obj.to_xml()
assert_xml_equal(actual_xml, xml)


def test_union_snapshot():
class SubModel1(BaseXmlModel, tag='submodel'):
attr1: int = attr()
Expand Down

0 comments on commit cc9af42

Please sign in to comment.