Hello!
I have spec which uses ref
. I would like to have default value for referenced field to be generated automatically. The only default value I managed to get is None
.
My spec is like this:
...
components:
schemas:
ParameterSet:
type: object
properties:
parameters:
$ref: '#/components/schemas/Parameters'
# default:
# fraction: 99
Parameters:
type: object
properties:
fraction: {default: 0.75, type: number}
...
Generated code is:
class ParameterSet(BaseModel):
parameters: Optional[Parameters] = None
If I uncomment default
section I get this, which is not correct as I understand:
class ParameterSet(BaseModel):
parameters: Optional[Parameters] = {'fraction': 99}
I would like to get something like this:
class ParameterSet(BaseModel):
parameters: Optional[Parameters] = Field(default_factory=lambda: Parameters(fraction = 99))
I run generator like this:
python3 -m datamodel_code_generator --input specs/spec.yml --output src/generated/models.py --use-subclass-enum --use-default --set-default-enum-member --output-model-type pydantic_v2.BaseModel --strip-default-none --field-constraints --field-include-all-keys
Is there a way to achieve this? Maybe there are some arguments I missed?
datamodel-code-generator version is 0.25.1.
Thanks!
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