OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Typescript] Invalid model for endpoint with return type OneOf Array or individual object

Open
#20,620 2 comments 3 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

Openapi generator creates model file with invalid typescript syntax. Invalid file is attached

PositionsGroupColIdPathGet200Response.ts.txt

For illustration, here is beginning of file, where majority of syntax errors appears:

import type { Array<Position> } from './Array&lt;Position&gt;';
import {
    instanceOfArray<Position>,
    Array<Position>FromJSON,
    Array<Position>FromJSONTyped,
    Array<Position>ToJSON,
} from './Array&lt;Position&gt;';
import type { Position } from './Position';
import {
    instanceOfPosition,
    PositionFromJSON,
    PositionFromJSONTyped,
    PositionToJSON,
} from './Position';

/**
 * @type PositionsGroupColIdPathGet200Response
 * 
 * @export
 */
export type PositionsGroupColIdPathGet200Response = Array<Position> | Position;

export function PositionsGroupColIdPathGet200ResponseFromJSON(json: any): PositionsGroupColIdPathGet200Response {
    return PositionsGroupColIdPathGet200ResponseFromJSONTyped(json, false);
}

export function PositionsGroupColIdPathGet200ResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): PositionsGroupColIdPathGet200Response {
    if (json == null) {
        return json;
    }
    if (instanceOfArray<Position>(json)) {
        return Array<Position>FromJSONTyped(json, true);
    }
    if (instanceOfPosition(json)) {
        return PositionFromJSONTyped(json, true);
    }

    return {} as any;
}

Obviously something fishy is happening with use of generics.

It seems to be old problem, as I saw it couple years back too. Now find time to report it.

openapi-generator version

7.11.0 , same fo 7.12.0 latest snapshot

OpenAPI declaration file content or url

Full OpenAPI file is here: https://github.com/izderadicka/audioserve/blob/rss/docs/audioserve-api-v1.yaml

Problem seems to be with return type of last endpoint in the declaration:

          content:
            application/json:
              schema:
                oneOf:
                  - $ref: "#/components/schemas/Position"
                  - type: array
                    items:
                      $ref: "#/components/schemas/Position"
                    maxItems: 1000
Generation Details

Generated with CLI (no special config file):

java -jar ~/local/openapi-generator/openapi-generator-cli-7.11.0.jar generate -i ~/workspace/audioserve/docs/audioserve-api-v1.yaml -g typescript-fetch -o ./client --additional-properties=typescriptThreePlus=true

Steps to reproduce

Just get mentioned OpenAPI yaml and run cli command. Then check file ./client/models/PositionsGroupColIdPathGet200Response.ts

Related issues/PRs

#5202 seems to similar - also problem with OneOf type, but errors in code are different. And it's quite old.

Suggest a fix

Definitely something is wrong with handling generic types - first they should not be in imports - then some non valid identifiers are created from them as Array<Position>FromJSON

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

Reproduce the issue with the linked audioserve-api-v1.yaml using the provided openapi-generator CLI command. Inspect the generated client/models/PositionsGroupColIdPathGet200Response.ts file and trace how the oneOf response containing Position and an array of Position is represented. Done means the typescript-fetch output contains valid TypeScript identifiers and imports for both alternatives.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.