Skip to content

Discriminator dependent type generation differs based on alphabetical order of related types (Regression) #2200

Open
@karlmacklin

Description

@karlmacklin

openapi-typescript version

7.6.1

Node.js version

22.11

OS + version

node:22.11-alpine docker image

Description

My typescript vernacular is limited but from what I can tell when using discriminator values that relates to some related type, then type generation works fine up until the alphabetical point where you encounter that related type.

I have a specific test case and output attached to this issue.

In short, when it works, it could look like this:

ADocumentContentModel: {
            properties?: components["schemas"]["ADocumentPropertiesModel"];
        } & (Omit<components["schemas"]["IApiContentModelBase"], "contentType"> & {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            contentType: "aDocument";
        });

and then anything alphabetically "after" IApiContentModelBase will fail by instead outputting:

JDocumentContentModel: {
            contentType: "JDocumentContentModel";
            properties?: components["schemas"]["JDocumentPropertiesModel"];
        } & Omit<components["schemas"]["IApiContentModelBase"], "contentType">;

contentType: "JDocumentContentModel"; is wrong, it should be set to jDocument.
Just as the above working example is generated to contentType: "aDocument";.

The input from Swagger here clearly defines the discriminator:

"discriminator": {
          "propertyName": "contentType",
          "mapping": {
            "aDocument": "#/components/schemas/ADocumentContentModel",
            "zDocument": "#/components/schemas/ZDocumentContentModel",
            "hDocument": "#/components/schemas/HDocumentContentModel",
            "jDocument": "#/components/schemas/JDocumentContentModel"
          }
        }

From all my testing it seems that since these types depend on the IApiContentModelBase, anything a-h works fine, and anything j-z fails.

This might be a regression issue. If I go all the way back to version 6.7.6 then it works fine.

Reproduction

Use the attached swagger.json file here.

swagger.json

Run:
npx openapi-typescript swagger.json --output myoutput.d.ts

This was run in an Alpine Node 22.11 Docker image.
Running this, notice this result.

I have attached a complete generated output that shows the error:
myoutput.d.txt
(github upload does not allow .ts files so I had to rename it to .txt)

Expected result

In this example, all discriminators contentType are set but with different approaches, and I expect them to be correct according to the Swagger specification.

For example jDocument instead of JDocumentContentModel.

Required

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

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions