openapi-generator: [BUG][typescript-fetch] Generating incorrect types

When using openapi-generator to generate a typescript-fetch API from this specification:

openapi-generator generate -i https://api.demo.kaizenep.com/swagger.json -g typescript-fetch -o kaizen-api

The generated code has incorrect types where “List” is referenced. For example line 182 and 219 of output apis/EventsApi.ts

async postEventSearchRaw(requestParameters: PostEventSearchRequest): Promise<runtime.ApiResponse<Array>> {
async postEventSearch(requestParameters: PostEventSearchRequest): Promise<Array> {

This should be:

async postEventSearchRaw(requestParameters: PostEventSearchRequest): Promise<runtime.ApiResponse<List>> {
async postEventSearch(requestParameters: PostEventSearchRequest): Promise<List> {

this is using openapi-generator 4.0.3

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 10
  • Comments: 23 (12 by maintainers)

Most upvoted comments

@Bakkiyaraj A general workaround is to replace GlobalFetch['fetch'] with WindowOrWorkerGlobalScope['fetch']. The correct code is generated if you use the --additional-properties=typescriptThreePlus=true flag during generation

@janniclas I’m confused: shouldn’t TS3+ be the default?

An option to generate code for typescript 3+ was introduced, which should fix that

So if I comment out that line, it generates the correct types.

However I still have to make a manual change to get it to compile without error, that I might as well mention here, I have to replace GlobalFetch with WindowOrWorkerGlobalScope in src/runtime.ts

Have just discovered this is an Error in production mode, it’s a warning only for development compilation, so it is important to me to get this resolved.

[ ERROR ]  TypeScript: src/kaizen-api/src/apis/EventsApi.ts:182:102
           Generic type 'Array<T>' requires 1 type argument(s).

    L181:   */
    L182:  async postEventSearchRaw(requestParameters: PostEventSearchRequest): Promise<runtime.ApiResponse<Array>> {
    L183:      if (requestParameters.payload === null || requestParameters.payload === undefined) {

[ ERROR ]  TypeScript: src/kaizen-api/src/apis/UsersApi.ts:390:100
           Generic type 'Array<T>' requires 1 type argument(s).

    L389:   */
    L390:  async postUserSearchRaw(requestParameters: PostUserSearchRequest): Promise<runtime.ApiResponse<Array>> {
    L391:      if (requestParameters.payload === null || requestParameters.payload === undefined) {

[ ERROR ]  TypeScript: src/kaizen-api/src/runtime.ts:127:24
           Cannot find name 'GlobalFetch'.

    L127:  export type FetchAPI = GlobalFetch['fetch'];

[22:56.4]  build failed in 14.45 s

So I am back to hand editing the Array to List to get this to work.

@janniclas I’m confused: shouldn’t TS3+ be the default?

Related: https://github.com/OpenAPITools/openapi-generator/issues/9973