Is your feature request related to a problem? Please describe.
When I have a type discriminator with a mapping where the field for the mapping is an enum, I'd like for the generated code to use the enum values for the literals on the discriminator field. Arguably, this should be a feature request for OpenAPI itself, but maybe it's something this library could help with. I'll show an example below (the example is taken from #1760)
Example input
Example schema:
---
components:
schemas:
Request:
oneOf:
- $ref: '#/components/schemas/RequestV1'
- $ref: '#/components/schemas/RequestV2'
discriminator:
propertyName: version
mapping:
v1: '#/components/schemas/RequestV1'
v2: '#/components/schemas/RequestV2'
RequestVersionEnum:
type: string
description: this is not included!
title: no title!
enum:
- v1
- v2
RequestBase:
properties:
version:
$ref: '#/components/schemas/RequestVersionEnum'
required:
- version
RequestV1:
allOf:
- $ref: '#/components/schemas/RequestBase'
properties:
request_id:
type: string
required:
- request_id
RequestV2:
allOf:
- $ref: '#/components/schemas/RequestBase'
Desired output
class RequestVersionEnum(str, Enum):
v1 = 'v1'
v2 = 'v2'
class RequestBase(BaseModel):
version: RequestVersionEnum
class RequestV1(RequestBase):
request_id: str = Field(..., description='there is description', title='test title')
version: Literal[RequestVersionEnum.v1]
class RequestV2(RequestBase):
version: Literal[RequestVersionEnum.v2]
class Request(RootModel[Union[RequestV1, RequestV2]]):
root: Union[RequestV1, RequestV2] = Field(..., discriminator='version')
Describe the solution you'd like
Likely would require a new argument to consider the type discriminator fields as enum values, not just strings
Additional context
Without this mypy
complains, basically of the following:
Incompatible types in assignment (expression has type "Literal['v1']", base class "RequestBase" defined the type as "RequestVersionEnum")
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