OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python] oneOf generates properties with invalid Literal fields

Open
#21,891 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using the python-pydantic-v2 generator, if a schema property is defined as oneOf, the generator produces an intermediate model as follows:

APIRESPONSEEXTRAVALUE_ONE_OF_SCHEMAS = ["ExtraAlpha", "ExtraBeta"]

class ApiResponseExtraValue(BaseModel):
    """
    ApiResponseExtraValue
    """
    # data type: ExtraAlpha
    oneof_schema_1_validator: Optional[ExtraAlpha] = None
    # data type: ExtraBeta
    oneof_schema_2_validator: Optional[ExtraBeta] = None
    actual_instance: Optional[Union[ExtraAlpha, ExtraBeta]] = None
    one_of_schemas: List[str] = Literal["ExtraAlpha", "ExtraBeta"]

    model_config = {
        "validate_assignment": True,
        "protected_namespaces": (),
    }

Where one_of_schemas is defined as Literal[...]. This is not correct as Literal is a type and should be used as a type hint, not a value.
Pydantic (and FastAPI's serialize_response()) treats this like a real runtime field causing the error:

pydantic_core._pydantic_core.PydanticSerializationError: 
Unable to serialize unknown type: <class 'typing._LiteralGenericAlias'>

at runtime when serializing models.

openapi-generator version

openapi-generator v7.15.0 (latest at the moment)
generator: python-fastapi (pydantic v2)
pydantic version: 2.11.7 (latest at the moment)
fastapi version: 0.116.1 (latest at the moment)

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Literal Bug Example
  version: 1.0.0
paths: {}
components:
  schemas:
    ApiResponse:
      type: object
      additionalProperties: false
      properties:
        message:
          type: string
          example: "Hello world"
        extra:
          description: "Map of extras"
          type: object
          additionalProperties:
            oneOf:
              - $ref: '#/components/schemas/ExtraAlpha'
              - $ref: '#/components/schemas/ExtraBeta'
      required:
        - message

    ExtraAlpha:
      type: object
      properties:
        type:
          type: string
          enum: ["alpha"]
        foo:
          type: string
      required: [type, foo]

    ExtraBeta:
      type: object
      properties:
        type:
          type: string
          enum: ["beta"]
        bar:
          type: integer
      required: [type, bar]
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied OpenAPI 3.0.3 YAML through the python-fastapi generator with Pydantic v2, then inspect the generated ApiResponseExtraValue model. Confirm how one_of_schemas is declared and reproduce serialization through FastAPI's serialize_response(). Done means the generated model no longer treats Literal[...] as a runtime field and serialization succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.