OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript] Type for map>array>item generated but not imported

Open
#6,804 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Same as #6192, but affecting the new typescript generator from #6341 instead of typescript-fetch. Updated description, error messages and repro below.

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used? 4.3.1
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When an item of an array of a map is specified through a referenced schema, the model type for the item is not imported into DefaultApi.ts.

For example, for an endpoint specification of:

  "/characters":
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                additionalProperties:
                  type: array
                  items:
                    $ref: "#/components/schemas/Character"

And a schema specification of:

    Character:
      required: ["first", "name"]
      properties:
        id:
          type: integer
        name:
          type: string

Compiling results in the following error:

Failed to compile.

/tmp/test-openapigen/src/api/apis/DefaultApi.ts
TypeScript error in /tmp/test-openapigen/src/api/apis/DefaultApi.ts(54,92):
Cannot find name 'Character'.  TS2304

    52 |      * @throws ApiException if the response code was not in [200, 299]
    53 |      */
  > 54 |      public async charactersGet(response: ResponseContext): Promise<{ [key: string]: Array<Character>; } > {
       |                                                                                            ^
    55 |         const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
    56 |         if (isCodeInRange("200", response.httpStatusCode)) {
    57 |             const body: { [key: string]: Array<Character>; } = ObjectSerializer.deserialize(
openapi-generator version

5.0.0-SNAPSHOT

OpenAPI declaration file content or url

https://github.com/bard/openapi-generator-missing-type-2/blob/master/example.yaml

Command line used for generation
OPENAPI_GENERATOR_VERSION=5.0.0-SNAPSHOT /tmp/openapi-generator-cli.sh generate -g typescript -i example.yaml -o ./src/api
Steps to reproduce
  1. Clone https://github.com/bard/openapi-generator-missing-type-2
  2. Run OPENAPI_GENERATOR_VERSION=5.0.0-SNAPSHOT /tmp/openapi-generator-cli.sh generate -g typescript -i example.yaml -o ./src/api
  3. Run yarn && yarn start
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/5995 seems to solve a similar issue

Suggest a fix

Import the missing type into DefaultApi.ts:

// TODO: better import syntax?
  import { BaseAPIRequestFactory, RequiredError } from './baseapi';
  import {Configuration} from '../configuration';
  import { RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http';
  import {ObjectSerializer} from '../models/ObjectSerializer';
  import {ApiException} from './exception';
  import {isCodeInRange} from '../util';
+ import {Character} from '../models/Character'
  
  
  /**
   * no description
   */
  export class DefaultApiRequestFactory extends BaseAPIRequestFactory {

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 TypeScript generator command and the linked example.yaml, then inspect how the generator assembles imports for the generated DefaultApi.ts. Done means the generated API imports Character for the map-of-array response and the example compiles successfully with yarn.

Written by the indexing model from the issue text.

Assessment

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