Skip to content
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

wrong json() behavior for nested models with __root__ #1414

Closed
silversurfer34 opened this issue Apr 22, 2020 · 2 comments
Closed

wrong json() behavior for nested models with __root__ #1414

silversurfer34 opened this issue Apr 22, 2020 · 2 comments
Labels
bug V1 Bug related to Pydantic V1.X help wanted Pull Request welcome

Comments

@silversurfer34
Copy link

silversurfer34 commented Apr 22, 2020

hello I have some issues with root

class Pets(BaseModel):
    __root__: List[str]
class PetHouse(BaseModel):
    Animals: Pets
    Location: str


pet1 = Pets(__root__=['dog', 'cat'])
pet2 = Pets.parse_obj(['dog', 'cat'])
pet1_json_str = json.dumps(pet1.json())
pet2_json_str = json.dumps(pet2.json())

pet_house_json_str = '{"Animals": ["dog", "cat"], "Location": "Montpellier"}'
pet_house = PetHouse(**json.loads(pet_house_json_str))
pet_house_result_json_str = json.dumps(pet_house.json())

my_pets = pet_house.Animals
my_pets_result_json_str = json.dumps(my_pets.json())

we have {"Animals": ["dog", "cat"], "Location": "Montpellier"} as input json
we have {"Animals": {"root": ["dog", "cat"]}, "Location": "Montpellier"} as result json, from the pydantic object which make it impossible to reload by other application

Moreover my_pets_result_json_str gives ["dog", "cat"] ...
(sorry if the format is not correct, it is my first bug on github)

@silversurfer34 silversurfer34 changed the title wrong json() behavior for nested models with __root wrong json() behavior for nested models with __root__ Apr 22, 2020
@samuelcolvin samuelcolvin added the bug V1 Bug related to Pydantic V1.X label Apr 22, 2020
@samuelcolvin
Copy link
Member

sorry if the format is not correct, it is my first bug on github

No problem. In future please follow the template you would have seen when you first create the issue if you'd chosen "bug" or "question" and fill the information it requests.


This look like a bug, I haven't seen it before since normally users don't use custom root types except in the root model.

I'll try and fix or or (or PR welcome if you'd like to fix it) but the best work around would be to not use models with custom root types as sub-models

@samuelcolvin samuelcolvin added the help wanted Pull Request welcome label Apr 22, 2020
@silversurfer34
Copy link
Author

silversurfer34 commented Apr 24, 2020

Hello again, I propose this PR #1429 to fix this.
(BTW I do not know if it is the right way to link the PR)

To explain why I have nested models with root, in fact I am using another project that generates a pydantic model from an openapi definition (https://github.com/koxudaxi/datamodel-code-generator) and this is the output of this generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X help wanted Pull Request welcome
Projects
None yet
Development

No branches or pull requests

2 participants