-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Open
Labels
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,
};Reactions are currently unavailable