openapi-generator: typescript-node generates invalid typescript
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?
- 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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 16 (9 by maintainers)
I’m not sure this is the correct place to raise it, but there is another problem too:
The code looks like this:
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?
@dsudzy as @adam-ah already mentioned, you just have to add
--additional-properties supportsES6=trueto your generator command.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.*