datamodel-code-generator
datamodel-code-generator copied to clipboard
model_config incorrectly added to RootModel classes with --use-annotated and model configuration flags
Describe the bug
The generator incorrectly includes a model_config on subclasses of RootModel
To Reproduce You can reproduce with a schema that defines an array type having items with constrained primitive types, and a generator configuration that requires adding model_config and uses the annotated pattern.
Example schema:
{
"components": {
"schemas": {
"MyModel": {
"type": "object",
"properties": {
"theItems": {
"type": "array",
"items": {
"type": "string",
"maxLength": 140,
"minLength": 2
}
}
}
}
}
}
}
Which generates:
from typing import Annotated, List, Optional
from pydantic import BaseModel, ConfigDict, Field, RootModel
class TheItem(RootModel[str]):
model_config = ConfigDict(
populate_by_name=True,
)
root: Annotated[str, Field(max_length=140, min_length=2)]
class MyModel(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
)
theItems: Optional[List[TheItem]] = None
Used commandline:
datamodel-codegen --input test.json --input-file-type=openapi --output test.py --output-model-type=pydantic_v2.BaseModel --allow-population-by-field-name --use-annotated
Expected behavior
RootModel subclasses should not have model_config
Version:
- OS: macOS Sequoia 15.5
- Python version:
3.11 - datamodel-code-generator version:
0.33.0