Reputation: 5785
Using datamodel-codegen
command with JSON
data as input type and generating Pydantic
schema as output, during this process I was seeing warnings.
What is the meaning of these warnings and how to fix them? What kind of issues it can create(or why this is a warning)?
UserWarning:
Expected `Union[list[definition-ref], definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
return self.__pydantic_serializer__.to_python(
To reproduce:
pets.json
{
"pets": [
{
"name": "dog",
"age": 2
},
{
"name": "cat",
"age": 1
},
{
"name": "snake",
"age": 3,
"nickname": "python"
}
],
"status": 200
}
Command:
datamodel-codegen --input pets.json --input-file-type json --output model.py
Versions:
python - 3.11.4
pydantic==2.1.1
datamodel-code-generator==0.21.4
genson==1.2.2
Upvotes: 6
Views: 13532
Reputation: 1
In my case updating pydantic and datamodel-code-generator helped
pip install -U pydantic datamodel-code-generator
For python 3.10
Upvotes: 0
Reputation: 1
I had a similar issue when there are cyclic nested models. If that is your problem, you need to call:
YourSchemaContainingNestedModel.model_rebuild()
after declaring the model.
Not sure if this is your case, though, since I cannot see the schemas you are using.
Upvotes: 0