#StackBounty: #javascript #typescript #typescript-typings #swagger-codegen Swagger-codegen-js not finding not translating the response …

Bounty: 50

Summary
I am using swagger-codegen-js to generate typescript files according to the swagger.json defined by an external api.

packages used
“swagger-js-codegen”: “^1.12.0”,

Alleged Problem
The return type on the method listMovies on the generated TS file is simply Promise<Request.Response> and not Promise<Array<Movie>>, i did expect it to be array of movies as the response clearly state the schema and thought/assumed that would be translated.

Given a json along the lines of the following, the

 "/movies": {
    "get": {
      "description": "Lists movies",
      "operationId": "listMovies",
      "responses": {
        "200": {
          "description": "Movie",
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/Movie"
            }
          }
        },
        "default": {
          "$ref": "#/responses/genericError"
        }
      }
    },
 "definitions": {
  "Movie": {
    "description": "something",
    "type": "object",
    "properties": {
      "archivedAt": {
        "description": "When the movie was archived",
        "type": "string",
        "format": "nullable-date-time",
        "x-go-name": "ArchivedAt",
        "readOnly": true
      }
  }
}

Generated TS Method

 /**
 * Lists movies
 * @method
 * @name Api#listMovies
 */
listMovies(parameters: {
    $queryParameters ? : any,
    $domain ? : string
}): Promise <request.Response> {
    const domain = parameters.$domain ? parameters.$domain : this.domain;
    let path = '/movies';
    .
    .
    .
        this.request('GET', domain + path, body, headers, queryParameters, form, reject, resolve);
    });
}

The script i use to generate the above ts file is straight from the github sample

const generateTSFilesUsingSwaggerJsCodegen = function () {
var fs = require('fs');
var CodeGen = require('swagger-js-codegen').CodeGen;

var file = 'build/sample.json';
var swagger = JSON.parse(fs.readFileSync(file, 'UTF-8'));
var tsSourceCode = CodeGen.getTypescriptCode({ className: 'Api', swagger: swagger, imports: ['../../typings/tsd.d.ts'] });
fs.writeFileSync('src/api/api.ts', tsSourceCode)

}

Am i missing something on the wire up/options passed or is this the expected generated file given the json file and that i need to write custom script to get what I want?


Get this bounty!!!

Leave a Reply