Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collection of tagged union deserialization bug fixed. #207

Merged
merged 1 commit into from
Aug 26, 2024
Merged
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
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
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
Loading