OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] Further union issues

Open
#7,367 9 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 issuue still exists?
  • Have you search 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

With https://github.com/OpenAPITools/openapi-generator/pull/6789 merged I regenerated my client and unfortunately there are still some union-related errors.

There are multiple locations with invalid code generated for handling union values:

        return new runtime.JSONApiResponse(response, (jsonValue) => jsonValue.map(Pet | Cat | DogFromJSON));
        const response = await this.request({
            path: `/pets`,
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: Pet | Cat | DogToJSON(requestParameters.petCatDog),
        });

No errors are thrown during generation but when compiling with typescript it's failing.

openapi-generator version

openapi-generator-cli-5.0.0-20200907.080402-621.jar

OpenAPI declaration file content or url
openapi: "3.0.1"
info:
  version: 1.0.0
  title: typescript union issue
paths:
  /pets:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  oneOf:
                    - $ref: "#/components/schemas/Pet"
                    - $ref: "#/components/schemas/Cat"
                    - $ref: "#/components/schemas/Dog"
                  discriminator:
                    mapping:
                      cat: "#/components/schemas/Cat"
                      dog: "#/components/schemas/Dog"
                    propertyName: "petType"
    post:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: "#/components/schemas/Pet"
                - $ref: "#/components/schemas/Cat"
                - $ref: "#/components/schemas/Dog"
              discriminator:
                mapping:
                  cat: "#/components/schemas/Cat"
                  dog: "#/components/schemas/Dog"
                propertyName: "petType"
      responses:
        "201":
          description: Created
  /pets-alt:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/PetsResponse"
components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
        name:
          type: string
    Cat:
      type: object
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          properties:
            scratching:
              type: boolean
    Dog:
      type: object
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          properties:
            sniffing:
              type: boolean
    PetsResponse:
      type: object
      properties:
        pets:
          type: array
          items:
            oneOf:
              - $ref: "#/components/schemas/Pet"
              - $ref: "#/components/schemas/Cat"
              - $ref: "#/components/schemas/Dog"
            discriminator:
              mapping:
                cat: "#/components/schemas/Cat"
                dog: "#/components/schemas/Dog"
              propertyName: "petType"
Generation Details

Generator: typescript-fetch
Config:

{
  "npmName": "@company/project",
  "npmVersion": "1.0.0",
  "snapshot": false,
  "supportsES6": true,
  "typescriptThreePlus": true
}
Steps to reproduce

This issue can be reproduced by using the typescript-fetch generator with the OpenAPI declaration & config from above.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/6789
https://github.com/OpenAPITools/openapi-generator/issues/6513

Suggest a fix

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 entry point and reproduce the issue using the OpenAPI declaration and config in this report. Compile the generated client and inspect the deserialization and request-body paths shown in the examples; done means the generated union handling is valid TypeScript in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, 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.