Skip to content

Commit

Permalink
fix: Parsing of custom root models (#4883)
Browse files Browse the repository at this point in the history
  • Loading branch information
gou177 committed Dec 29, 2022
1 parent c04923c commit d845e9d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/4883-gou177.md
@@ -0,0 +1 @@
fix parsing of custom root models
1 change: 1 addition & 0 deletions pydantic/main.py
Expand Up @@ -508,6 +508,7 @@ def json(
def _enforce_dict_if_root(cls, obj: Any) -> Any:
if cls.__custom_root_type__ and (
not (isinstance(obj, dict) and obj.keys() == {ROOT_KEY})
and not (isinstance(obj, BaseModel) and obj.__fields__.keys() == {ROOT_KEY})
or cls.__fields__[ROOT_KEY].shape in MAPPING_LIKE_SHAPES
):
return {ROOT_KEY: obj}
Expand Down
1 change: 1 addition & 0 deletions tests/test_parse.py
Expand Up @@ -46,6 +46,7 @@ class MyModel(BaseModel):
m = MyModel.parse_obj('a')
assert m.dict() == {'__root__': 'a'}
assert m.__root__ == 'a'
assert MyModel.parse_obj(m) == m


def test_parse_root_list():
Expand Down

0 comments on commit d845e9d

Please sign in to comment.