OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.6k stars 6.29k forks source link

[BUG] [typescript-fetch] Type error in oneOf when legacyDiscriminatorBehavior is false #18876

Closed fa0311 closed 4 weeks ago

fa0311 commented 4 weeks ago

Bug Report Checklist

Description
openapi-generator version
OpenAPI declaration file content or url
inputSpec: input.yaml
outputDir: test_client_ts
legacyDiscriminatorBehavior: false
openapi: 3.0.1
info:
  title: ""
  version: ""
paths: {}
components:
  schemas:
    OneOf:
      discriminator:
        propertyName: type
        mapping":
          A: "#/components/schemas/A"
          B: "#/components/schemas/B"

    A:
      type: object
      properties:
        type:
          type: string

    B:
      type: object
      properties:
        type:
          type: string
> npm run build

test_client_ts/models/OneOf.ts:47:13 - error TS2322: Type 'A & { type: string; }' is not assignable to type 'OneOf'.
  Type 'A & { type: string; }' is not assignable to type '{ type: "B"; } & B'.
    Type 'A & { type: string; }' is not assignable to type '{ type: "B"; }'.
      Types of property 'type' are incompatible.
        Type 'string' is not assignable to type '"B"'.

47             return Object.assign({}, AFromJSONTyped(json, true), { type: 'A' });
               ~~~~~~

test_client_ts/models/OneOf.ts:49:13 - error TS2322: Type 'B & { type: string; }' is not assignable to type 'OneOf'.
  Type 'B & { type: string; }' is not assignable to type '{ type: "B"; } & B'.
    Type 'B & { type: string; }' is not assignable to type '{ type: "B"; }'.
      Types of property 'type' are incompatible.
        Type 'string' is not assignable to type '"B"'.

49             return Object.assign({}, BFromJSONTyped(json, true), { type: 'B' });
               ~~~~~~

Found 2 errors in the same file, starting at: test_client_ts/models/OneOf.ts:47
Generation Details
Steps to reproduce
Related issues/PRs

The change in #18154 now causes this issues.

Suggest a fix