OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-fetch] Additional header parameters override all default parameters
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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? Generated code still present here https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L63
- 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
runtime.ts has the following code:
private createFetchParams(context: RequestOpts, initOverrides?: RequestInit) {
let url = this.configuration.basePath + context.path;
if (context.query !== undefined && Object.keys(context.query).length !== 0) {
// only add the querystring to the URL if there are query parameters.
// this is done to avoid urls ending with a "?" character which buggy webservers
// do not handle correctly sometimes.
url += '?' + this.configuration.queryParamsStringify(context.query);
}
const body = ((typeof FormData !== "undefined" && context.body instanceof FormData) || context.body instanceof URLSearchParams || isBlob(context.body))
? context.body
: JSON.stringify(context.body);
const headers = Object.assign({}, this.configuration.headers, context.headers);
const init = {
method: context.method,
headers: headers,
body,
credentials: this.configuration.credentials,
...initOverrides
};
return { url, init };
}
}
...initOverrides here overrides everything, even useful defaults like Content-Type. This leads to losing stuff like Content-Type: application/json headers when you set a custom header for a request. In my case, I need to override the built in Basic Auth header generation because btoa does not work with non ASCII characters and I need to use js-base64 instead.
openapi-generator version
5.4.0
Suggest a fix
Instead of
const init = {
method: context.method,
headers: headers,
body,
credentials: this.configuration.credentials,
...initOverrides
};
you should probably go for something like:
const remainingOverrides = Object.assign({}, initOverrides);
delete remainingOverrides['method'];
delete remainingOverrides['headers'];
delete remainingOverrides['body'];
delete remainingOverrides['credentials'];
const init = {
method: initOverrides.method ?? context.method,
headers: {
...headers,
...initOverrides.headers
},
body: initOverrides.body ?? body,
credentials: initOverrides.credentials ?? this.configuration.credentials,
...remainingOverrides,
};
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache, especially createFetchParams and the initOverrides handling shown in the issue. Inspect the generated typescript-fetch runtime and verify that custom overrides no longer remove useful default headers such as Content-Type while still applying the requested override behavior. Done means the generated request preserves defaults and supports the reported custom-header use case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100