@@ -390,6 +390,46 @@ class TestModel(RootXmlModel, tag='model'):
390
390
assert_xml_equal (actual_xml , xml )
391
391
392
392
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
+
393
433
def test_union_snapshot ():
394
434
class SubModel1 (BaseXmlModel , tag = 'submodel' ):
395
435
attr1 : int = attr ()
0 commit comments