8000 fix: Parsing of custom root models (#4883) (#4884) · pydantic/pydantic@a220f87 · GitHub
[go: up one dir, main page]

Skip to content

Commit a220f87

Browse files
authored
fix: Parsing of custom root models (#4883) (#4884)
1 parent a825106 commit a220f87

File tree

3 files changed

+3
-0
lines changed

3 files changed

+3
-0
lines changed

changes/4883-gou177.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix parsing of custom root models

pydantic/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ def json(
508508
def _enforce_dict_if_root(cls, obj: Any) -> Any:
509509
if cls.__custom_root_type__ and (
510510
not (isinstance(obj, dict) and obj.keys() == {ROOT_KEY})
511+
and not (isinstance(obj, BaseModel) and obj.__fields__.keys() == {ROOT_KEY})
511512
or cls.__fields__[ROOT_KEY].shape in MAPPING_LIKE_SHAPES
512513
):
513514
return {ROOT_KEY: obj}

tests/test_parse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MyModel(BaseModel):
4646
m = MyModel.parse_obj('a')
4747
assert m.dict() == {'__root__': 'a'}
4848
assert m.__root__ == 'a'
49+
assert MyModel.parse_obj(m) == m
4950

5051

5152
def test_parse_root_list():

0 commit comments

Comments
 (0)
0