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 mapping does not seem to work properly #1700

Open
1 of 2 tasks
ivansky opened this issue Jun 19, 2024 · 0 comments
Open
1 of 2 tasks

Discriminator mapping does not seem to work properly #1700

ivansky opened this issue Jun 19, 2024 · 0 comments
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!

Comments

@ivansky
Copy link

ivansky commented Jun 19, 2024

Description
Discriminator + Enums does not work, there are a few problems.

Name Version
openapi-typescript 7.0.0-rc.0
Node.js v18.17.1
OS + version macOS

Reproduction

openapi: 3.0.1
info:
  title: API
  version: v1
servers:
  - url: http://localhost:8082
    description: Generated server url
paths:
  /api/pets:
    get:
      operationId: getPets
      summary: Get pets
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
components:
  schemas:
    PetType:
      type: string
      enum:
        - Dog
        - Cat
        - Lizard
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          $ref: '#/components/schemas/PetType'
      discriminator:
        propertyName: petType
        mapping:
          Dog: '#/components/schemas/Dog'
          Cat: '#/components/schemas/Cat'
          Lizard: '#/components/schemas/Lizard'
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            name:
              type: string
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: string
    Lizard:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Lizard`
          properties:
            lovesRocks:
              type: boolean

npx openapi-typescript@next ./testapi.yaml -o ./testapi.ts --enum=true
Results in:

export interface paths {
    "/api/pets": {
        parameters: {
            query?: never;
            header?: never;
            path?: never;
            cookie?: never;
        };
        /** Get pets */
        get: operations["getPets"];
        put?: never;
        post?: never;
        delete?: never;
        options?: never;
        head?: never;
        patch?: never;
        trace?: never;
    };
}
export type webhooks = Record<string, never>;
export interface components {
    schemas: {
        /** @enum {string} */
        PetType: PetType;
        Pet: {
            petType: components["schemas"]["PetType"];
        };
        Cat: Omit<components["schemas"]["Pet"], "petType"> & {
            name?: string;
        } & {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            petType: CatPetType;
        };
        Dog: Omit<components["schemas"]["Pet"], "petType"> & {
            bark?: string;
        } & {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            petType: DogPetType;
        };
        Lizard: Omit<components["schemas"]["Pet"], "petType"> & {
            lovesRocks?: boolean;
        } & {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            petType: LizardPetType;
        };
    };
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: never;
}
export type $defs = Record<string, never>;
export interface operations {
    getPets: {
        parameters: {
            query?: never;
            header?: never;
            path?: never;
            cookie?: never;
        };
        requestBody?: never;
        responses: {
            /** @description OK */
            200: {
                headers: {
                    [name: string]: unknown;
                };
                content: {
                    "application/json": components["schemas"]["Pet"][];
                };
            };
        };
    };
}
export enum PetType {
    Dog = "Dog",
    Cat = "Cat",
    Lizard = "Lizard"
}
export enum CatPetType {
    Cat = "Cat"
}
export enum DogPetType {
    Dog = "Dog"
}
export enum LizardPetType {
    Lizard = "Lizard"
}

Expected result

Cat: Omit<components["schemas"]["Pet"], "petType"> & {
    name?: string;
} & {
    /**
     * @description discriminator enum property added by openapi-typescript
     * @enum {string}
     */
    petType: CatPetType;
};
  • Instead of CatPetType better to have PetType.Cat
  • Exclude Omit<components["schemas"]["Pet"], "petType"> from the type to get rid of cycle type dependency Cat -> Pet -> Cat that causes typescript error

Preferred outcome

Cat: {
    name?: string;
} & {
    petType: PetType.Cat;
};

Checklist

@ivansky ivansky added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jun 19, 2024
@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

No branches or pull requests

2 participants