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:
raid:
type: integer
enum:
- 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="")
But with oneOf:
raid:
type: integer
oneOf:
- title: 0
const: 0
description: RAID level 0
It generates:
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:
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