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
Issue1414: wrong behavior of json() method with nested models containing custom root #1429
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Change the behavior of json() method to remove the extra `__root__` keys in the result when using nested models with custom root types |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,6 +204,37 @@ class Model(BaseModel): | |
assert Model(__root__=['a', 'b']).json() == '["a", "b"]' | ||
|
||
|
||
def test_encode_nested_root(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has nothing to do with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure to understand your comment. Those tests build models with nested root as child elements, then calls the json method and the expected result is a json without the |
||
class Pets(BaseModel): | ||
__root__: List[str] | ||
|
||
class PetHouse(BaseModel): | ||
Animals: Pets | ||
Location: str | ||
|
||
assert ( | ||
PetHouse(**{'Animals': ['dog', 'cat'], 'Location': 'Montpellier'}).json() | ||
== '{"Animals": ["dog", "cat"], "Location": "Montpellier"}' | ||
) | ||
|
||
|
||
def test_encode_several_nested_root(): | ||
class PetsAlias(BaseModel): | ||
__root__: List[str] | ||
|
||
class Pets(BaseModel): | ||
__root__: PetsAlias | ||
|
||
class PetHouse(BaseModel): | ||
Animals: Pets | ||
Location: str | ||
|
||
assert ( | ||
PetHouse(**{'Animals': ['dog', 'cat'], 'Location': 'Montpellier'}).json() | ||
== '{"Animals": ["dog", "cat"], "Location": "Montpellier"}' | ||
) | ||
|
||
|
||
def test_custom_decode_encode(): | ||
load_calls, dump_calls = 0, 0 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be done more easily with a change to
_get_value
, though I haven't tried myself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to look at _get_value but it would spread the change also to dict() method and make some other tests failing (and might introduce new issues?).