Openapi-generator: typescript-node generates invalid typescript

Created on 13 Mar 2019  路  16Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

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

Using a valid swagger JSON I generated a client using the following command:

openapi-generator  generate -i the.json -l typescript-node

The resulting code is invalid Typescript and cannot be transpiled without manual intervention:

import Promise = require('bluebird');

The above line is completely redundant in Typescript. There is no requirement to use bluebird as promises are part of the basic node offering.

import { ObjectSerializer, Authentication, HttpBasicAuth, ApiKeyAuth, OAuth, VoidAuth } from '../model/models';

error TS6133: 'HttpBasicAuth' is declared but its value is never read.
error TS6133: 'ApiKeyAuth' is declared but its value is never read.
error TS6133: 'OAuth' is declared but its value is never read.

Apart from the errors themselves, it is concerning that no oauth authentication is included in the authentications property.

Asynchronous methods (those that return a Promise) should be declared as async methods. For example, this:

public calculateLine (calculateLineRequest: CalculateLineRequest) : Promise<{ response: http.ClientResponse; body: CalculateLineResponse;  }>

should be

public async calculateLine (calculateLineRequest: CalculateLineRequest) : Promise<{ response: http.ClientResponse; body: CalculateLineResponse;  }>

otherwise typescript consumers of the API cannot use await when calling the API method.

openapi-generator version

3.3.4

OpenAPI declaration file content or url

I cannot provide this as it a private commercial API

Command line used for generation
openapi-generator  generate -i the.json -l typescript-node
Steps to reproduce

Just generate the code

Related issues/PRs
Suggest a fix

Drop the reference to bluebird.
Declare asynchronous methods as async
Investigate why there is no oauth authentication

TypeScript Bug

Most helpful comment

I'm not sure this is the correct place to raise it, but there is another problem too:

error TS2694: Namespace '"http"' has no exported member 'ClientResponse'.

The code looks like this:

public remoteMethod (param: String....) : Promise<{ response: http.ClientResponse; body: Response;  }> {

It appears that ClientResponse has been removed since Node 10 (I'm on 11.7.0), using the same version of generator (3.3.4).

A related bug:
https://github.com/OpenAPITools/openapi-generator/issues/1138

EDIT: It appears the " --additional-properties supportsES6=true" flag will generate Node 11 compatible code. Should it be the default perhaps?

All 16 comments

馃憤 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.

sounds good. would you like to give it a try and fix it?

I have given it a try in a locally cloned repo that I branched off master. However I don't know how you would prefer me to proceed with regard to a PR? Firstly is it master that you want me to branch from or not? I cannot push anything upstream because I am not authorised so do I need to be a contributor?

My change was simply to typescript-node/api-single.mustache

please fork the repository, create a new branch from master and file a PR against master. I will be happy to review your changes

OK

@kpturner this may help: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-submit-a-pr-to-fix-bugs-or-make-enhancements

I'm not sure this is the correct place to raise it, but there is another problem too:

error TS2694: Namespace '"http"' has no exported member 'ClientResponse'.

The code looks like this:

public remoteMethod (param: String....) : Promise<{ response: http.ClientResponse; body: Response;  }> {

It appears that ClientResponse has been removed since Node 10 (I'm on 11.7.0), using the same version of generator (3.3.4).

A related bug:
https://github.com/OpenAPITools/openapi-generator/issues/1138

EDIT: It appears the " --additional-properties supportsES6=true" flag will generate Node 11 compatible code. Should it be the default perhaps?

EDIT: It appears the " --additional-properties supportsES6=true" flag will generate Node 11 compatible code. Should it be the default perhaps?

Good idea. We can make supportsES6 the default in 4.0.0 release. May I know if you've time to file a PR for this?

I'm sorry @wing328, even though I suspect it's a one-line change, it's very unlikely I would have any time to work on this before July.

resolved in #2452

EDIT: It appears the " --additional-properties supportsES6=true" flag will generate Node 11 compatible code. Should it be the default perhaps?

Good idea. We can make supportsES6 the default in 4.0.0 release. May I know if you've time to file a PR for this?

Just in case anyone comes across this, in version 4.1.1 you still need this flag to produce output compatible with Node v10.

I think it has other issue then ?
I am using 4.1.1 and ended up with this:

import { ApiKeyAuth } from '../model/models';
import { ApiKeyAuth } from '../model/models';

(two times lol)

any idea?

@jaydeep987 can you open a separate issue?

Im still experiencing this issue with typescript-node Namespace '"http"' has no exported member 'ClientResponse'. return new Promise<{ response: http.ClientResponse; body: string; }>((resolve, reject). I am using version generator version 4.2.3 and tsc version 3.6.4.

Edit:
This was fixed by downgrading @types/node to version 8.*

@dsudzy as @adam-ah already mentioned, you just have to add --additional-properties supportsES6=true to your generator command.

Was this page helpful?
0 / 5 - 0 ratings