OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-angular] response with anyOf type array leads to wrong imports in generated code

Open
#9,301 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

If anyOf with type array is used in a response, the generated code uses wrong imports:

import { Array<ImageDTO> } from '../model/models';
import { Array<ImageFileDTO> } from '../model/models';
openapi-generator version

5.1.0

OpenAPI declaration file content or url

Defining the following schema in a response:

"schema": {
    "title": "Get Content",
    "anyOf": [
      {
        "type": "array",
        "items": { "$ref": "#/components/schemas/ImageFile" }
      },
      {
        "type": "array",
        "items": { "$ref": "#/components/schemas/Image" }
      }
    ]
}

leads to the following import statements, which try to import the array type:

import { Array<ImageDTO> } from '../model/models';
import { Array<ImageFileDTO> } from '../model/models';

Correctly only ImageDTO and ImageFileDTO should be imported.

Here is a full example Gist

Command line used for generation

java -jar openapi-generator-cli-5.1.0.jar generate -i openapi\minimal_openapi.json -g typescript-angular -o .\src\client --additional-properties=modelSuffix=DTO

Workaround

Wrapping the anyOf in a schema which is used as response does not lead to this issue.

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 provided OpenAPI schema and the typescript-angular generator command. Trace how response anyOf array schemas are converted into imports, then verify that generation imports only ImageDTO and ImageFileDTO rather than Array and Array.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, openapi, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.