Skip to content

[BUG][typescript-fetch] Additional header parameters override all default parameters #11744

@BernhardPosselt

Description

@BernhardPosselt

Bug Report Checklist

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,
};

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions