Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow discriminator field literals to be set to enum values #1773

Open
berkowitzi opened this issue Dec 12, 2023 · 2 comments
Open

Allow discriminator field literals to be set to enum values #1773

berkowitzi opened this issue Dec 12, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@berkowitzi
Copy link

berkowitzi commented Dec 12, 2023

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")
@koxudaxi koxudaxi added the enhancement New feature or request label Dec 22, 2023
@koxudaxi
Copy link
Owner

@berkowitzi
I'm sorry for my late replay.
It's a good suggestion.
We should implement it with a new CLI option.

@Mig-Foxhound
Copy link

Is this change going to happen? I am running into the same issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants