-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Generated models using RootModel to represent Annotated Unions are out of order #1921
Comments
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 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) }} |
Was just about to file the List discriminator issue I mentioned above, but someone beat me to it: #1937 |
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: