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 values are generated using the name of referenced types instead of the actual property value #2149

Open
1 of 2 tasks
thirteenthstep opened this issue Feb 11, 2025 · 4 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@thirteenthstep
Copy link

openapi-typescript version

7.6.1

Node.js version

22.12.0

OS + version

macOs 15.3

Description

If the schema declares a oneOf property with discriminator the generated Types will have their name as discriminator value.

Reproduction

Example Schema:

openapi: 3.0.3

components:
  schemas:
    Post:
      properties:
        comments:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/TextComment'
              - $ref: '#/components/schemas/QuoteComment'
            discriminator:
              propertyName: commentType
    TextComment:
      type: object
      required:
        - commentType
      properties:
        commentType:
          type: string
          enum:
            - text
    QuoteComment:
      type: object
      required:
        - commentType
      properties:
        commentType:
          type: string
          enum:
            - quote

Output:

schemas: {
        Post: {
            comments?: (components["schemas"]["TextComment"] | components["schemas"]["QuoteComment"])[];
        };
        TextComment: {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            commentType: "TextComment";
        };
        QuoteComment: {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            commentType: "QuoteComment";
        };

Where actual values of commentType should be text|quote.
This issue does not occur if

            discriminator:
              propertyName: commentType

is absent, or if the types are not nested/referenced at all.

Expected result

schemas: {
        Post: {
            comments?: (components["schemas"]["TextComment"] | components["schemas"]["QuoteComment"])[];
        };
        TextComment: {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            commentType: "text";
        };
        QuoteComment: {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            commentType: "quote";
        };

Required

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

Extra

@thirteenthstep thirteenthstep added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Feb 11, 2025
@fluctus
Copy link

fluctus commented Feb 12, 2025

+1
@thirteenthstep It does not happen when using mapping, but discriminator mapping is not supported when validating with ajv.

@openint-bot
Copy link

just ran into this also @fluctus what do you mean when using mapping? Do you have a workaround?

@fluctus
Copy link

fluctus commented Feb 13, 2025

@thirteenthstep The current implementation seems to match what is written in swagger.io about discriminator without mapping:

In our example, the discriminator points to the objectType property that contains the data type name.

https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/

@fluctus
Copy link

fluctus commented Feb 13, 2025

@openapi-ts-bot You can define mapping under discriminator, the mapping will be used as the discriminator values in the generated types. Read about in the link in the comment above

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

3 participants