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

Duplicate discriminator enum from createDiscriminatorEnum in 7.x

Open danmichaelo opened this issue 10 months ago • 0 comments

openapi-typescript version

7.6.0

Node.js version

20.15.0

OS + version

macOS 15.2

Description

After upgrading from openapi-typescript 6.7.0 to 7.6.0, our linter started warning about duplicates in intersection types (@typescript-eslint/no-duplicate-type-constituents):

  625:9  error  Intersection type constituent is duplicated with {
      /** @enum {string} */
      type: "agent";
    }  @typescript-eslint/no-duplicate-type-constituents

This is only a warning, not an error, but still seems like a bug.

Reproduction

Given the following schema:

# openapi-reproduction.yaml
openapi: 3.0.0
components:
  schemas:
    Image:
      oneOf:
        - $ref: '#/components/schemas/ImageTypeAgent'
      discriminator:
        propertyName: type
        mapping:
          agent: '#/components/schemas/ImageTypeAgent'
    ImageTypeAgent:
      allOf:
        - $ref: '#/components/schemas/ImageBase'
        - type: object
          required:
            - type
          properties:
            type:
              type: string
              enum:
                - agent
    ImageBase:
      required:
        - url
      properties:
        url:
          type: string

which is valid given the following .redocly.lint-ignore.yaml file:

openapi-reproduction.yaml:
  struct:
    - '#/'
  no-empty-servers:
    - '#/openapi'
  no-unused-components:
    - '#/components/schemas/Image'

Then the following command:

npx [email protected] openapi-reproduction.yaml

gives the following result:

export type paths = Record<string, never>;
export type webhooks = Record<string, never>;
export interface components {
    schemas: {
        Image: components["schemas"]["ImageTypeAgent"];
        ImageTypeAgent: components["schemas"]["ImageBase"] & {
            /** @enum {string} */
            type: "agent";
        } & {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            type: "agent";
        };
        ImageBase: {
            url: string;
        };
    };
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: never;
}
export type $defs = Record<string, never>;
export type operations = Record<string, never>;

It seems like createDiscriminatorEnum adds an enum even if one is already present in the schema.

Expected result

export type paths = Record<string, never>;
export type webhooks = Record<string, never>;
export interface components {
    schemas: {
        Image: components["schemas"]["ImageTypeAgent"];
        ImageTypeAgent: components["schemas"]["ImageBase"] & {
            /** @enum {string} */
            type: "agent";
        };
        ImageBase: {
            url: string;
        };
    };
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: never;
}
export type $defs = Record<string, never>;
export type operations = Record<string, never>;

Required

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

Extra

danmichaelo avatar Jan 27 '25 13:01 danmichaelo