Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Description

@silversurfer34

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions