OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-axios] Unions generated with incorrect intersections with Enum discriminator property, leads to type never

Open
#16,475 1 comment 4 reactions 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 (example)?
  • 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

I have schema generated with FastAPI/Pydantic with polymorphic models represented with discriminated unions, based on IntEnum (EventResponse model). I have solved issue with anyOf/oneOf, but now I have another problem.

export type Data = { type: '1' } & EventOut | { type: '2' } & MessageOut;

type is IntEnum, so it has numeric values and Data is always never type. Property type for MessageOut, for example, has correct type:

export const MessageOutTypeEnum = {
    NUMBER_2: 2
} as const;

export type MessageOutTypeEnum = typeof MessageOutTypeEnum[keyof typeof MessageOutTypeEnum];

Same problem with typescript-fetch.
Correct output must be

export type Data = EventOut | MessageOut;

or

export type Data = { type: 1 } & EventOut | { type: 2 } & MessageOut;
openapi-generator version

6.6.0, 7.0.0

OpenAPI declaration file content or url

openapi.json

Generation Details

docker run --rm -v /tmp/data:/data openapitools/openapi-generator-cli:latest generate -g typescript-axios -i /data/openapi.json -o /data/api

Steps to reproduce

Put my schema in /tmp/data and execute command above, error will be in 30-36 lines in result api.ts file.

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 documented Docker command with the linked openapi.json and inspect lines 30-36 of the generated api.ts. Compare the typescript-axios output with the expected union and repeat with typescript-fetch to confirm both generators are affected. Done means the generated Data type preserves the numeric discriminator values without becoming never.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.