openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Discriminated Union Not Typed Correctly

Open iulspop opened this issue 9 months ago • 0 comments

openapi-typescript version

7.6.1

Node.js version

22.12.0

OS + version

macOS 15.2

Description

Reproduce Bug

Given

    CreateRepositoryRequest:
      description: Lorem ipsum dolor sit amet.
      type: object
      discriminator:
        propertyName: type
        mapping:
          azure: '#/components/schemas/CreateAzureRepositoryRequest'
          git: '#/components/schemas/CreateGitRepositoryRequest'

When

run npm run generate

Actual

        CreateRepositoryRequest: Record<string, never>;

Expected

A discriminated union of the two types, something like:

        "CreateRepositoryRequest": {
            type: "azure" | "git";
        } & ({
            type: "azure";
        } & components["schemas"]["CreateAzureRepositoryRequest"]) | ({
            type: "git";
        } & components["schemas"]["CreateGitRepositoryRequest"]);

Reproduction

clone: https://github.com/iulspop/bug-report-openapi-typescript-feb-13-2025 then npm i && npm run generate

Expected result

A discriminated union of the two types, something like:

        "CreateRepositoryRequest": {
            type: "azure" | "git";
        } & ({
            type: "azure";
        } & components["schemas"]["CreateAzureRepositoryRequest"]) | ({
            type: "git";
        } & components["schemas"]["CreateGitRepositoryRequest"]);

Required

  • [x] My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

iulspop avatar Feb 13 '25 16:02 iulspop