You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thank you for the great work on datamodel-code-generator! I have a question regarding how to handle oneOf in OpenAPI schemas, and I'm wondering if it’s possible to generate Python Enums from oneOf, similar to how they are generated from enum fields.
When I use enum in my schema, the generator creates an Enum class as expected, restricting values to the specified enum values. However, when I use oneOf with const values, I would expect similar behavior — an Enum to be generated to enforce these restricted values, but instead, a normal Pydantic model is generated.
To Reproduce
Example schema:
raid:type: integerenum:- 0
When enum is used, it correctly generates the following code:
class DvrRaidLevel(Enum):
integer_0 = 0
class DvrStorageConfig(BaseModel):
class Config:
orm_mode = True
use_enum_values = True
raid: Optional[DvrRaidLevel] = Field(None, description="")
class DvrRaidLevel(BaseModel):
class Config:
orm_mode = True
use_enum_values = True
__root__: int
class DvrStorageConfig(BaseModel):
class Config:
orm_mode = True
use_enum_values = True
raid: Optional[DvrRaidLevel] = Field(None, description="")
Expected behavior
I expect the generator to treat oneOf with const values in a similar way to how it handles enum. Specifically, I would like an Enum to be generated for fields using oneOf to restrict the allowed values. For example, in the case of oneOf, I expect the following code to be generated:
class DvrRaidLevel(Enum):
integer_0 = 0
class DvrStorageConfig(BaseModel):
class Config:
orm_mode = True
use_enum_values = True
raid: Optional[DvrRaidLevel] = Field(None, description="")
Version:
OS: MacOS
Python version: 3.10
datamodel-code-generator version: 0.26.0
The text was updated successfully, but these errors were encountered:
Describe the bug
First of all, thank you for the great work on datamodel-code-generator! I have a question regarding how to handle oneOf in OpenAPI schemas, and I'm wondering if it’s possible to generate Python Enums from oneOf, similar to how they are generated from enum fields.
When I use enum in my schema, the generator creates an Enum class as expected, restricting values to the specified enum values. However, when I use oneOf with const values, I would expect similar behavior — an Enum to be generated to enforce these restricted values, but instead, a normal Pydantic model is generated.
To Reproduce
Example schema:
When enum is used, it correctly generates the following code:
But with oneOf:
It generates:
Expected behavior
I expect the generator to treat oneOf with const values in a similar way to how it handles enum. Specifically, I would like an Enum to be generated for fields using oneOf to restrict the allowed values. For example, in the case of oneOf, I expect the following code to be generated:
Version:
The text was updated successfully, but these errors were encountered: