Open luminoso opened 1 year ago
Thank you for creating the issue. I didn't know the behavior. I will fix it.
I may change the field name instead of the Model name.
🤔 Any updates ?
I had the same issue. I solved it by using the --use-annotated
flag. However, I still it would be helpful if the root cause is addressed.
Describe the bug
Generating
"pydantic_v2.BaseModel
withField()
generates an impossible solution for current pydantic v2 implementation.To Reproduce
Example schema:
Used commandline:
Expected behavior The generated class should work when running with pydantic v2.
Version:
Fedora 38 - Linux-6.1.50-200.fc38.x86_64-x86_64-with-glibc2.37
3.11.4 (main, Jun 7 2023, 00:00:00) [GCC 13.1.1 20230511 (Red Hat 13.1.1-2)]
0.21.4
Additional context The generate class is the following:
Which is exactly the same as the one generated with
--output-model-type "pydantic.BaseModel"
. However, due to pydantic v2 limitations, the generated class will lead to a pydantic v2 error:RecursionError: maximum recursion depth exceeded
The pydantic v1 and v2 behaviour duality has been reported at pydantic repository: https://github.com/pydantic/pydantic/issues/7327
To make datamodel-code-generator work with the current pydantic v2 implementation, as suggested by https://github.com/pydantic/pydantic/issues/7327#issuecomment-1706012608, generated classes must have a different name from the field names, like so:
In which, having a class name that doesn't collide with field name, makes the generated class to work with pydantic v2.