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
21.94k stars 6.59k forks source link

[BUG][typescript-fetch] Nested oneOf generates type object instead of union type #20155

Open foxable opened 1 day ago

foxable commented 1 day ago

Bug Report Checklist

Description

When defining nested schemas using oneOf, the nested property will be of type object instead the corresponding union type.

openapi-generator version

This is a regression introduced in openapi-generator 7.5.0, which is still present in 7.10.0.

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Test API
  version: "0.1"
paths:
  /contacts:
    get:
      operationId: getContact
      responses:
        "200":
          content:
            '*/*':
              schema:
                oneOf:
                  - $ref: "#/components/schemas/InstitutionContact"
                  - $ref: "#/components/schemas/PersonContact"
          description: OK
      tags:
        - Contact
components:
  schemas:
    Contact:
      type: object
      discriminator:
        propertyName: type
      properties:
        '@type':
          type: string
      required:
        - type
    InstitutionContact:
      type: object
      allOf:
        - $ref: "#/components/schemas/Contact"
        - type: object
          properties:
            address:
              type: object
              oneOf:
                - $ref: "#/components/schemas/DomesticAddress"
                - $ref: "#/components/schemas/PostboxAddress"
      required:
        - address
    PersonContact:
      type: object
      allOf:
        - $ref: "#/components/schemas/Contact"
        - type: object
          properties:
            address:
              type: object
              oneOf:
                - $ref: "#/components/schemas/DomesticAddress"
                - $ref: "#/components/schemas/PostboxAddress"
      required:
        - address
    Address:
      type: object
      discriminator:
        propertyName: type
      properties:
        type:
          type: string
      required:
        - type
    DomesticAddress:
      type: object
      allOf:
        - $ref: "#/components/schemas/Address"
        - type: object
          properties:
            street:
              type: string
      required:
        - street
    PostboxAddress:
      type: object
      allOf:
        - $ref: "#/components/schemas/Address"
        - type: object
          properties:
            postbox:
              type: string
      required:
        - postbox
Generation Details

I'm using the openapi-generator Gradle plugin with the following options:

'supportsES6': 'true',
'withInterfaces': 'true',
'useSingleRequestParameter': 'false',
'legacyDiscriminatorBehavior': 'false',

Up until openapi-generator 7.4.0, this generated the following types:

export type GetContact200Response = { type: 'InstitutionContact' } & InstitutionContact | { type: 'PersonContact' } & PersonContact;

export interface InstitutionContact extends Contact {
  address: InstitutionContactAllOfAddress;
}

export type InstitutionContactAllOfAddress = { type: 'DomesticAddress' } & DomesticAddress | { type: 'PostboxAddress' } & PostboxAddress;

Since openapi-generator 7.5.0, the following type is generated for InstitutionContact:

export interface InstitutionContact extends Contact {
  address: object;
}
Steps to reproduce

Execute the openapi-generator via Gradle using the specified declaration file and options, using typescript-fetch with version 7.5.0 or later.