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

Remove use of __root__ in Pydantic models #40

Open
pederhan opened this issue Apr 4, 2023 · 3 comments
Open

Remove use of __root__ in Pydantic models #40

pederhan opened this issue Apr 4, 2023 · 3 comments
Milestone

Comments

@pederhan
Copy link
Member

pederhan commented Apr 4, 2023

Pydantic V2 removes support for __root__ in models. The Pre-release blog post provides 2 different ways to create models with "root" fields.

The easiest replacement seems to be AnalyzedType. This uses the new Pydantic V2 type that allows for creating arbitrary types from generic annotations.

Another, more complicated approach, if custom behavior is required, is to provide custom model validators and serializers, as demonstrated in the Pydantic test suite. This seems to provide more flexibility, but adds a lot of boilerplate code and it's unclear if we actually need this flexibility.

@pederhan pederhan added this to the Pydantic V2 milestone Apr 4, 2023
@pederhan pederhan added this to Harbor Apr 4, 2023
@pederhan
Copy link
Member Author

I think the name AnalyzedType was changed at some point, even though the blog post still points to that name. Will update once I can be bothered to dig up the new name.

@pederhan
Copy link
Member Author

pederhan commented Aug 1, 2023

With #57 , we can use the newest version of datamodel-code-generator to generate Pydantic V2 models. That uses the new RootModel which serializes to JSON correctly, but requires us to manually add the methods for supporting iteration.

https://docs.pydantic.dev/latest/usage/models/#rootmodel-and-custom-root-types

Furthermore, we need to ensure we don't break current user apps that directly access the __root__ attribute.

All this can probably be done with some patching at runtime? Unless we can specify our own RootModel to use in datamodel-code-generator...?

@pederhan
Copy link
Member Author

pederhan commented Aug 1, 2023

datamodel-codegen --collapse-root-models is NOT the answer we are looking for at least, because that will definitely break stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant