Describe the bug
When the default
value is set in a referenced schema in OpenAPI 3.1 and it has type: ["string", "null"]
, the default value of dataclass becomes None
while the non-referenced property properly sets the default value as described in the OpenAPI.
To Reproduce
Example schema:
openapi: 3.1.0
info:
title: AutoML Notebook Parameters
version: 1.0.0
components:
schemas:
UserColumn:
type: ["string", "null"]
default: userid
Params1:
type: object
properties:
user_column:
$ref: '#/components/schemas/UserColumn'
user_column2:
type: ["string", "null"]
default: userid
user_column3:
$ref: '#/components/schemas/UserColumn'
default: userid
Used commandline:
$ datamodel-codegen --input .openapi.yaml --output-model-type dataclasses.dataclass --output model.py
And I got the following model.py:
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-09-24T03:02:05+00:00
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
UserColumn = Optional[str]
@dataclass
class Params1:
user_column: Optional[UserColumn] = None
user_column2: Optional[str] = 'userid'
user_column3: Optional[UserColumn] = 'userid'
Expected behavior
The expected dataclass is as follows:
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-09-24T03:02:05+00:00
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
UserColumn = Optional[str]
@dataclass
class Params1:
user_column: Optional[UserColumn] = 'userid' # Should not be None
user_column2: Optional[str] = 'userid'
user_column3: Optional[UserColumn] = 'userid'
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