Skip to content

Discriminator values are generated using the name of referenced types instead of the actual property value #2149

Open
@thirteenthstep

Description

@thirteenthstep

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

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