Describe the bug
Default values for objects with additionalProperties mapped to python dictionaries are incorrectly rendered in the generated pydantic models.
To Reproduce
- Save schema provided below as
schema.json
- Generate the code using command
datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py
- Incorrect schema is generated, see Expected behavior for details
Example schema:
{
"$defs": {
"ItemModel": {
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"description": {
"title": "Description",
"type": "string"
}
},
"required": [
"name",
"description"
],
"title": "ItemModel",
"type": "object"
}
},
"properties": {
"dict_with_defaults": {
"additionalProperties": {
"$ref": "#/$defs/ItemModel"
},
"default": {},
"title": "Dict With Defaults",
"type": "object"
}
},
"title": "ParentModel",
"type": "object"
}
Used commandline:
$ datamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py
Expected behavior
Currently the following models are generated as following:
class ItemModel(BaseModel):
name: str = Field(..., title='Name')
description: str = Field(..., title='Description')
class ParentModel(BaseModel):
dict_with_defaults: dict[str, ItemModel] | None = Field(
default_factory=lambda: ItemModel.model_validate({}), title='Dict With Defaults'
)
dict_with_defaults has incorrect default_factory, because it tries to validate the default as an instance of value class instead of a dictionary.
Expected line:
dict_with_defaults: dict[str, ItemModel] | None = Field(
default_factory=dict, title='Dict With Defaults'
)
Version:
- OS: MacOs 26.2
- Python version: 3.13
- datamodel-code-generator version: 0.54.1
Additional context
N/A
Describe the bug
Default values for objects with
additionalPropertiesmapped to python dictionaries are incorrectly rendered in the generated pydantic models.To Reproduce
schema.jsondatamodel-codegen --input schema.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.pyExample schema:
{ "$defs": { "ItemModel": { "properties": { "name": { "title": "Name", "type": "string" }, "description": { "title": "Description", "type": "string" } }, "required": [ "name", "description" ], "title": "ItemModel", "type": "object" } }, "properties": { "dict_with_defaults": { "additionalProperties": { "$ref": "#/$defs/ItemModel" }, "default": {}, "title": "Dict With Defaults", "type": "object" } }, "title": "ParentModel", "type": "object" }Used commandline:
Expected behavior
Currently the following models are generated as following:
dict_with_defaults has incorrect default_factory, because it tries to validate the default as an instance of value class instead of a dictionary.
Expected line:
Version:
Additional context
N/A