Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
bard opened this issue Jun 29, 2020 · 1 comment
Open
5 of 6 tasks

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

bard opened this issue Jun 29, 2020 · 1 comment

Comments

@bard
Copy link

bard commented Jun 29, 2020

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

#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 {
@auto-labeler
Copy link

auto-labeler bot commented Jun 29, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant