question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] typescript-fetch openapi generator fails out-of-the-box

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I added this to my package.json: "@openapitools/openapi-generator-cli": "^2.3.10", and when I run this command: "generate:api": "(rm -rf src/api/gen || rmdir src/api/gen /s /q) && openapi-generator-cli generate -g typescript-fetch --additional-properties=supportsES6=true,typescriptThreePlus=true -i ../openapi/schema.yaml -o ./src/api/gen" I get the following error:


Failed to compile.

/##/runtime.ts
TypeScript error in /###/runtime.ts(152,3):
Type '((input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>) | undefined' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
  Type 'undefined' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.  TS2322

    150 | 
    151 |     get fetchApi(): FetchAPI {
  > 152 |               return this.configuration.fetchApi;
        |               ^
    153 |     }
    154 | 
    155 |     get middleware(): Middleware[] {


When I add “as FetchAPI” to line 152, everything works correctly. I have no idea where the problem is. Why does it generate code with this error?

openapi-generator version

2.3.10

OpenAPI declaration file content or url

N/A

Generation Details

N/A

Steps to reproduce

N/A

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/10164 https://github.com/OpenAPITools/openapi-generator/pull/10216 - supposedly it was fixed but it’s still an issue when using version 2.3.10

Suggest a fix

Not sure about the fix. I quickly worked around it by casting it “as FetchAPI” which is not a good solution:

    150 | 
    151 |     get fetchApi(): FetchAPI {
   152 |               return this.configuration.fetchApi as FetchAPI;
        |               
    153 |     }

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:7

github_iconTop GitHub Comments

1reaction
xsorifc28commented, Sep 15, 2021

I am seeing the same issue with generator-cli v5.2.1

On v5.1.1 the generated code is (runtime.ts):

export class Configuration {
     ...
    get fetchApi(): FetchAPI {
        return this.configuration.fetchApi || window.fetch.bind(window);
    }
    ...
}

On v5.2.1 the generated code is (runtime.ts):

export class Configuration {
     ...
    get fetchApi(): FetchAPI {
        return this.configuration.fetchApi;
    }
    ...
}

Possibly related to changes from #10050?

0reactions
RonnyLarkcommented, Oct 20, 2022

We have been using generated api for a few months and now are trying to move to TS strict mode. But it seems that the generated runtime.ts files have inherent strict-failing lines; for instance:

export class Configuration {
    ...

    protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
        const { url, init } = await this.createFetchParams(context, initOverrides);
        const response = await this.fetchApi(url, init);
        if (response.status >= 200 && response.status < 300) {
            return response;
        }
        throw new ResponseError(response, 'Response returned an error code');
    }

    ...
}
'response' is possibly 'undefined'.ts(18048)
Argument of type 'undefined' is not assignable to parameter of type 'Response'.ts(2345)

There are a total of 12 errors in each runtime.ts file failing the strictNullChecks strict clause. Run with version 6.2.0 and typescript-fetch.

I hope this thread is still being looked at.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage - OpenAPI Generator
The most commonly used openapi-generator-cli commands are: author Utilities for authoring generators or customizing templates. batch Generate ...
Read more >
OpenAPITools/openapi-generator - Gitter
Python seemed to work out-of-the-box though I have not tested it yet. I briefly tested the Java+Jersey one and it worked for the...
Read more >
OpenAPI custom generator with Maven plugin fails with ...
my.company.codegen.MyCodegenGenerator as well as the generatorName and still get a similar error. Please help me to resolve this issue.
Read more >
A Guide to OpenAPI Code Generation for TypeScript
Generate REST client code in TypeScript from an OpenAPI ... Writing client side code for a REST API is tedious, repetitive, and error...
Read more >
Openapi Generator Versions - Open Source Agenda
View the latest Openapi Generator versions. ... [typescript-fetch] Support error handler in middleware #12716; fix typescript node default enum #12633 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found