You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When generating models via datamodel-codegen against valid jsonschema created from Pydantic models that rely on postponed annotations and Annotated[Union[]]s, the output pydantic models are "out of order" in the file leading to a python NameError.
To Reproduce
Example python:
from __future__ importannotationsimportpydanticimporttypingclassDog(pydantic.BaseModel):
name: typing.Literal['dog'] =pydantic.Field('dog', title='woof')
friends: typing.Optional[typing.List[Animal]] =pydantic.Field(title='Friends', default=[])
classCat(pydantic.BaseModel):
name: typing.Literal['cat'] =pydantic.Field('cat', title='meow')
friends: typing.Optional[typing.List[Animal]] =pydantic.Field(title='Friends', default=[])
classBird(pydantic.BaseModel):
name: typing.Literal['bird'] =pydantic.Field('bird', title='bird noise')
friends: typing.Optional[typing.List[Animal]] =pydantic.Field(title='Friends', default=[])
# This is the key bit. This Annotated[Union[]] is a way to use the discriminator on the UnionAnimal=typing.Annotated[typing.Union[
Dog, Cat, Bird
], pydantic.Field(title='Any animal', discriminator='name')]
classZoo(pydantic.BaseModel):
animals: typing.List[Animal] =pydantic.Field(title='A zoo of Animals', default=[])
Expected behavior
In the generated output, one of:
the Friends and Animals models shouldn't list the other model types in its inheritance to RootModel
the Friends and Animals models should be listed after all the other models (if you simply move them below the Dog model, everything works fine due to the postponed annotations)
Version:
OS: Linux / Rocky 8
Python version: 3.10
datamodel-code-generator version: 0.25.2
Additional context
This only seems to happen when the models contain a reference to themselves as part of an Annotated Union. If you directly inline the Union[Dog,Cat,Bird] into each spot in place of Animal, the generated python code works fine, but you obviously lose the discriminator then
If you run with --collapse-root-models then the Animals and Friends models aren't generated at all (are inlined to the other classes) but you end up with a different bug (I'll write up a report for that as well), where the discriminator is placed incorrectly and is attempting to discriminate on the List[Union[Dog, Cat]] rather than on the Union[Dog,Cat] itself, but in summary:
It seems like this simple patch fixes things for me at least. I think it works fine since we're putting the type hints on the root property of the RootModel already, so we don't need the types in the inheritance declaration as well. The type hints on the root property can do postponed annotation just fine since they aren't part of the object's type declaration
index 0a2810bd..6a42e155 100644
--- a/datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2+++ b/datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2@@ -10,7 +10,7 @@
{{ decorator }}
{% endfor -%}
-class {{ class_name }}({{ base_class }}[{{get_type_hint(fields)}}]):{% if comment is defined %} # {{ comment }}{% endif %}+class {{ class_name }}({{ base_class }}):{% if comment is defined %} # {{ comment }}{% endif %}
{%- if description %}
"""
{{ description | indent(4) }}
Describe the bug
When generating models via
datamodel-codegen
against valid jsonschema created from Pydantic models that rely on postponed annotations andAnnotated[Union[]]
s, the output pydantic models are "out of order" in the file leading to a pythonNameError
.To Reproduce
Example python:
Example schema:
Resulting pydantic from datamodel-codegen
Attempt to use:
Used commandline:
Expected behavior
In the generated output, one of:
Friends
andAnimals
models shouldn't list the other model types in its inheritance toRootModel
Friends
andAnimals
models should be listed after all the other models (if you simply move them below theDog
model, everything works fine due to the postponed annotations)Version:
Additional context
Union[Dog,Cat,Bird]
into each spot in place ofAnimal
, the generated python code works fine, but you obviously lose thediscriminator
then--collapse-root-models
then theAnimals
andFriends
models aren't generated at all (are inlined to the other classes) but you end up with a different bug (I'll write up a report for that as well), where the discriminator is placed incorrectly and is attempting to discriminate on theList[Union[Dog, Cat]]
rather than on theUnion[Dog,Cat]
itself, but in summary:so you end up with
The text was updated successfully, but these errors were encountered: