swagger-api / swagger-api/swagger-codegen

[typescript-node] Runtime error when using array definitions

Open
#10,000 2 comments 0 reactions 1 assignee View on GitHub

@HugoMario is already working on this.

Since Oct 24, 2020.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When using arrays in definitions (swagger 2.0), an error occures when calling the api via the generated code (typescript-node):

TypeError: typeMap[type].getAttributeTypeMap is not a function
    at Function.deserialize ((...)\generated-client\api.ts:128:48)
    at Request.localVarRequest [as _callback] ((...)\generated-client\api.ts:653:45)
    at Request.self.callback ((...)\node_modules\request\request.js:185:22)
    at Request.emit (events.js:189:13)
    at Request.<anonymous> ((...)\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:189:13)
    at IncomingMessage.<anonymous> ((...)\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:277:13)
    at IncomingMessage.emit (events.js:194:15)
    at endReadableNT (_stream_readable.js:1103:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

at the following code line:
let attributeTypes = typeMap[type].getAttributeTypeMap();

The requested definition typeMap[type] is LocationGetResponse, which is an array (see below). So the error is comprehensible because the class definition is
export class LocationGetResponse extends Array<LocationGetResponseInner>
-> getAttributeTypeMap only exists for the array elements, not the array itself.

Swagger-codegen version

Same error with current master (2.4.13) and older versions (2.4.9 - 2.4.12)

Swagger declaration file content or url
swagger: "2.0"
info:
  description: ""
  version: "1.0.0"
  title: "MyAPI"``

paths:
  /location:
    get:
      x-swagger-router-controller: "location"
      operationId: "location_get"
      summary: "..."
      description: ""
      parameters:
        - name: "input"
          in: "query"
          description: "search string"
          required: true
          pattern: '^.+$'
          type: "string"
      produces:
        - "application/json"
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/LocationGetResponse"

definitions:
  LocationGetResponse:
    type: "array"
    items:
      type: "object"
      required:
        - id
        - text
      properties:
        id:
          type: "string"
          description: "ID of location"
        text:
          type: "string"
          description: "Name of location"
Command line used for generation

java -jar swagger-codegen-cli-2.4.12.jar generate --input-spec api.yaml --lang typescript-node --config typescript-node.json

with typescript-node.json:

{
    "supportsES6": true
}
Steps to reproduce

You need at least a small dummy server which produces request results.

  1. generate api.ts via swagger-codegen
  2. Try to call the location method
Related issues/PRs
Suggest a fix/enhancement

Maybe checking for array type before reaching the throwing line...

PS: I'm looking forward generating typescript-node for openapi3 :)

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.