OpenAPITools / OpenAPITools/openapi-generator

[BUG] typescript-fetch preMiddleware fetchAPI not used

Open
#10,402 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I need an upload progress for some of my requests. since plane old fetch doesn't support the uploadevents, my solution is to use a modified whatwg-fetch-polyfill to allow me to modify the underlying XHR object and attach the eventlisteners

A simplified version of what I'm doing looks like this:

const replaceFetch = <T extends BaseAPI>(api: T ): T => {
    return api.withPreMiddleware(async ctx => ({
        ...ctx,
        fetch: (input: RequestInfo, init?: RequestInit | undefined) => {
             throw new Error("this should always happen")
        }
    }))
}

const api = new MyApi(config)

const impossibleApi = replaceFetch(api)

// this should always crash
impossibleApi.getSomeResource()

In this code, it should not be possible to do any requests, since the fetch API always throws an error.

The problem is, that the fetch override is never used (it never gets called), it only uses the fetch API set in the initial configuration

The bug does not concern any custom generated code, it only effects the BaseAPI.withPreMiddleware

openapi-generator version

openapi-generator-cli 5.2.0

OpenAPI declaration file content or url

I can provide it if needed, but it should be reproducible with any declaration

Generation Details
openapi-generator-cli generate -i ./swagger.yaml -g typescript-fetch --type-mappings Date=string -c ./swagger/clientgen.config.json -o ./api --enable-post-process-file

clientgen.config.json:

{
    "allowUnicodeIdentifiers": true,
    "modelPropertyNaming": "camelCase",
    "enumPropertyNaming": "camelCase",
    "paramNaming": "camelCase",
    "nullSafeAdditionalProps": true,
    "supportsES6": true,
    "typescriptThreePlus": true,
    "withoutRuntimeChecks": true,
    "legacyDiscriminatorBehavior": false
}
Steps to reproduce
  1. create an API
  2. use the snippet from above to apply a middleware in your API that should replace the fetch
  3. see that nothing happened, since fetch was not replaced
Related issues/PRs

none I found

Suggest a fix

it concerns this function:
https://github.com/OpenAPITools/openapi-generator/blob/60dcf8613f6b75b606757c4a991fe018ad10ee99/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L31

from my observation this.fetchApi has never changed, so here it will always use the old one
https://github.com/OpenAPITools/openapi-generator/blob/60dcf8613f6b75b606757c4a991fe018ad10ee99/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L38

here lies the core of the problem:
https://github.com/OpenAPITools/openapi-generator/blob/60dcf8613f6b75b606757c4a991fe018ad10ee99/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L77
the middlewares have been looped over to get the fetchParams, but the fetch is still pulled from the configuration

the only issue I have with fixing it myself is that I don't know what should happen when multiple middlewares override the fetchAPI

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache, especially the BaseAPI.withPreMiddleware implementation and the fetch selection around the referenced lines. Reproduce the issue with the provided replaceFetch snippet and inspect how multiple middleware fetch overrides should compose. Done means a middleware-provided fetchAPI is used instead of the initial configuration fetch, with the multiple-override behavior established.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.