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

Problem after using ver 7 with generated types for mapping of discriminator

Open nenadfspartans opened this issue 1 year ago • 5 comments

Description

This was working before ver 7 and it creates working code for my usecase.

This is what creates problem for me:

This is request body that can accept one of these options.

schema:
              oneOf:
                - $ref: '#/components/schemas/Model1'
                - $ref: '#/components/schemas/Model2'
              discriminator:
                propertyName: template
                mapping:
                  model1: '#/components/schemas/Model1'
                  model2: '#/components/schemas/Model2'

Here we define descriminator, redocly populates last 2 rows of mapping, it was not problem with 6.7.0

Document:
      type: object
      properties:
        template:
          $ref: '#/components/schemas/TemplateField'
      discriminator:
        propertyName: template
        mapping:
          model2: '#/components/schemas/Model2'
          model1: '#/components/schemas/Model1'
          Model1: '#/components/schemas/Model1'
          Model2: '#/components/schemas/Model2'

Generated code with v6.7.0, perfectly fine for my needs.

 Model1: {
      template: "model1";
    } & Omit<components["schemas"]["Document"], "template"> & {
      data: {
        page: number;
        batch: {
          sku: string;
          name: string;
        };
        totalPages: number;
        images: components["schemas"]["ImageUrlObject"][];
      };
    };

Generated code with v7.0.0

Model1: Omit<components["schemas"]["Document"], "template"> & {
            data: {
                page: number;
                batch: {
                    sku: string;
                    name: string;
                };
                totalPages: number;
                images: components["schemas"]["ImageUrlObject"][];
            };
        } & {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            template: "model1";
        } & {
            /**
             * @description discriminator enum property added by openapi-typescript
             * @enum {string}
             */
            template: "model2" | "Model1";
        };

Thanks!

Checklist

nenadfspartans avatar Jun 28 '24 17:06 nenadfspartans

Your bug report is not complete. Please provide a minimum example with all necessary schemas. In your case the models are missing.

mzronek avatar Jun 28 '24 20:06 mzronek

probably duplicate of #1700

nenadfspartans avatar Jul 01 '24 08:07 nenadfspartans

@nenadfspartans Hello,

I have same problem. But I think that raised by TypeScript version and VSCODE. I was change TypeScript version 5.5.2 > 5.4.5, This problem not occured.

Can someone tell me how I can fix or walkaround this issue in TypeScript 5.5.x?

imjuni avatar Jul 02 '24 05:07 imjuni

I am on 5.4.5 and have this problem.

nenadfspartans avatar Jul 02 '24 08:07 nenadfspartans

The OP's problem is not a Typescript issue. The generated type is wrong. If anyone has this problem, please post an example for reproduction.

mzronek avatar Jul 03 '24 07:07 mzronek