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
I'm trying to generate Pydantic V2 models from the Kubernetes OpenAPI Batch spec, but I'm running into issues when the default value in the OpenAPI spec is given as {}. This default value is copied directly into the model code, which generates warnings & results in invalid models when loading & relying on defaults.
One example:
UserWarning: Pydantic serializer warnings:
Expected `ObjectMeta` but got `dict` with value `{}` - serialized value may not be as expected
Expected `ObjectMeta` but got `dict` with value `{}` - serialized value may not be as expected
Expected `ObjectMeta` but got `dict` with value `{}` - serialized value may not be as expected
Expected `ObjectMeta` but got `dict` with value `{}` - serialized value may not be as expected
It seems it may be possible to solve this problem by adding the validate_default = True argument to the generated Field whenever the default value is an object/dict (in fact, I have worked around the issue locally by doing exactly this), but it would be nice to have a better solution for this problem.
Expected behavior
Model code that produces a correct validated model when loading JobSpecs from JSON files.
Version:
OS: linux
Python version: 3.10
datamodel-code-generator version: [e.g. 0.26.4]
The text was updated successfully, but these errors were encountered:
joel-wright
changed the title
OpenAPI spec with a default value of an object causes issues with Pydantic Model
OpenAPI spec with a default value of object/dict causes issues with Pydantic Model
Dec 16, 2024
I'm trying to generate Pydantic V2 models from the Kubernetes OpenAPI Batch spec, but I'm running into issues when the default value in the OpenAPI spec is given as
{}
. This default value is copied directly into the model code, which generates warnings & results in invalid models when loading & relying on defaults.One example:
It seems it may be possible to solve this problem by adding the
validate_default = True
argument to the generatedField
whenever the default value is an object/dict (in fact, I have worked around the issue locally by doing exactly this), but it would be nice to have a better solution for this problem.To Reproduce
Generate a Pydantic V2 model from the kubernetes spec here: https://github.com/kubernetes/kubernetes/tree/master/api/openapi-spec
Used commandline:
Expected behavior
Model code that produces a correct validated model when loading
JobSpec
s from JSON files.Version:
The text was updated successfully, but these errors were encountered: