OpenAPITools / OpenAPITools/openapi-generator

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

Open
#20,155 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typescript-fetch generator and reproduce the issue using the supplied OpenAPI declaration, Gradle options, and version comparison between 7.4.0 and 7.5.0. Done means nested address properties generate the corresponding union type instead of object, while the top-level oneOf output remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.