Open veaviticus opened 7 months ago
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) }}
Was just about to file the List discriminator issue I mentioned above, but someone beat me to it: https://github.com/koxudaxi/datamodel-code-generator/issues/1937
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 theRootModel
already, so we don't need the types in the inheritance declaration as well. The type hints on theroot
property can do postponed annotation just fine since they aren't part of the object's type declaration
Thanks for this, this fixes an issue I was having in creating a model from the OGCAPI Features schema.
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