Skip to content

Commit 80eb45b

Browse files
committed
custom root type serialization format changed.
1 parent 968a2d0 commit 80eb45b

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
from pydantic_xml import BaseXmlModel, attr, element
1+
from typing import List
22

3+
from pydantic import HttpUrl
34

4-
# [model-start]
5-
class CompanyInfo(BaseXmlModel):
6-
trade_name: str = attr(name='trade-name')
7-
website: str = element()
5+
from pydantic_xml import BaseXmlModel, element
86

9-
... # data validation logic
107

8+
# [model-start]
9+
class Socials(BaseXmlModel, tag='socials'):
10+
__root__: List[HttpUrl] = element(tag='social')
1111

12-
class Company(BaseXmlModel, tag='company'):
13-
__root__: CompanyInfo
1412

15-
... # business logic
13+
class Contacts(BaseXmlModel, tag='contacts'):
14+
__root__: Socials
1615
# [model-end]
1716

1817

1918
# [xml-start]
2019
xml_doc = '''
21-
<company trade-name="SpaceX">
22-
<website>https://www.spacex.com</website>
23-
</company>
20+
<contacts>
21+
<socials>
22+
<social>https://www.linkedin.com/company/spacex</social>
23+
<social>https://twitter.com/spacex</social>
24+
<social>https://www.youtube.com/spacex</social>
25+
</socials>
26+
</contacts>
2427
''' # [xml-end]
2528

2629
# [json-start]
2730
json_doc = '''
28-
{
29-
"trade_name": "SpaceX",
30-
"website": "https://www.spacex.com"
31-
}
31+
[
32+
"https://www.linkedin.com/company/spacex",
33+
"https://twitter.com/spacex",
34+
"https://www.youtube.com/spacex"
35+
]
3236
''' # [json-end]
3337

34-
env = Company.from_xml(xml_doc)
35-
assert env == Company.parse_raw(json_doc)
38+
contacts = Contacts.from_xml(xml_doc)
39+
40+
assert contacts == Contacts.parse_raw(json_doc)

pydantic_xml/serializers/factories/model.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,8 @@ def build(
171171
) -> 'Serializer':
172172
if field_location is Location.ELEMENT:
173173
return cls.ElementSerializer(model, model_field, ctx)
174-
elif not ctx.parent_is_root and field_location is Location.MISSING:
174+
elif field_location is Location.MISSING:
175175
return cls.ElementSerializer(model, model_field, ctx)
176-
elif ctx.parent_is_root and field_location is Location.MISSING:
177-
return cls.DeferredSerializer(model_field)
178176
elif field_location is Location.ATTRIBUTE:
179177
raise errors.ModelFieldError(
180178
model.__name__, model_field.name, "attributes of model type are not supported",

tests/test_forward_ref.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,15 @@ def test_root_model_forward_ref():
249249
class TestModel(BaseXmlModel, tag='model1'):
250250
__root__: 'TestSubModel'
251251

252-
class TestSubModel(BaseXmlModel):
252+
class TestSubModel(BaseXmlModel, tag='model2'):
253253
__root__: int
254254

255255
TestModel.update_forward_refs(**locals())
256256

257257
xml = '''
258-
<model1>1</model1>
258+
<model1>
259+
<model2>1</model2>
260+
</model1>
259261
'''
260262

261263
actual_obj = TestModel.from_xml(xml)

tests/test_submodels.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ class TestModel(BaseXmlModel, tag='model1'):
7979

8080

8181
def test_root_submodel_root_extraction():
82-
class TestSubModel(BaseXmlModel):
82+
class TestSubModel(BaseXmlModel, tag='model2'):
8383
__root__: int
8484

8585
class TestModel(BaseXmlModel, tag='model1'):
8686
__root__: TestSubModel
8787

8888
xml = '''
89-
<model1>1</model1>
89+
<model1>
90+
<model2>1</model2>
91+
</model1>
9092
'''
9193

9294
actual_obj = TestModel.from_xml(xml)
@@ -101,7 +103,7 @@ class TestModel(BaseXmlModel, tag='model1'):
101103

102104

103105
def test_nested_root_submodel_element_extraction():
104-
class TestSubModel2(BaseXmlModel):
106+
class TestSubModel2(BaseXmlModel, tag='model3'):
105107
__root__: int
106108

107109
class TestSubModel1(BaseXmlModel):
@@ -112,7 +114,9 @@ class TestModel(BaseXmlModel, tag='model1'):
112114

113115
xml = '''
114116
<model1>
115-
<element1>1</element1>
117+
<element1>
118+
<model3>1</model3>
119+
</element1>
116120
</model1>
117121
'''
118122

0 commit comments

Comments
 (0)