Describe the bug
According to this OpenAPI guide, there are two ways to define free-form objects (a.k.a., a dictionary with values of any type).
They are equivalent and we expect the code generator to produce the same results Optional[Dict[str, Any]] = None
.
Unfortunately only one of the syntaxes works as expected.
To Reproduce
Example schema with syntax 1 (additionalProperties: true
)
components:
schemas:
CustomObject:
type: object
properties:
config:
$ref: '#/components/schemas/Config'
Config:
type: object
additionalProperties: true
Output of syntax 1
class Config(BaseModel):
pass
model_config = ConfigDict(
extra="allow",
)
class CustomObject(BaseModel):
config: Optional[Config] = None
Example schema with syntax 2 (additionalProperties: {}
)
components:
schemas:
CustomObject:
type: object
properties:
config:
$ref: '#/components/schemas/Config'
Config:
type: object
additionalProperties: {}
Output of syntax 2
class CustomObject(BaseModel):
config: Optional[Dict[str, Any]] = None
Used commandline:
$ datamodel-codegen --input test.yaml --input-file-type openapi --output test.py --snake-case-field --target-python-version 3.9 --use-schema-description --field-constraints --use-annotated --collapse-root-models --use-one-literal-as-default --enum-field-as-literal one --output-model-type pydantic_v2.BaseModel
Expected behavior
We expect both syntaxes will result in Output of syntax 2.
Version:
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too