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

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

Open
1 of 2 tasks
karlmacklin opened this issue Mar 13, 2025 · 0 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@karlmacklin
Copy link

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

@karlmacklin karlmacklin added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

1 participant