Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][JS] Description - ApiClient.js generated 'User-Agent' header but should be optional #12743

Closed
dcmwong opened this issue Jun 30, 2022 · 3 comments · Fixed by #20367
Closed

Comments

@dcmwong
Copy link

dcmwong commented Jun 30, 2022

Description

When using our site in Chrome there is an error in the console:

Refused to set unsafe header "User-Agent"

We use the openapi-generator-cli tool that generates the ApiClient.js setting the header User-Agent even though we haven't specified it in the cli. There is no way, that we've seen, to tell the cli to not set the User-Agent header and therefore no way to remove the above error from Chrome, besides manually removing it after the build.

openapi-generator version

With version :

    "@openapitools/openapi-generator-cli": "^2.4.26",

The actual version in yarn is:
"resolved" "https://registry.npmjs.org/@openapitools/openapi-generator-cli/-/openapi-generator-cli-2.4.26.tgz"

Generation Details

We use: this command to generate our js client code

openapi-generator-cli generate -g javascript -i swagger.json -o api --additional-properties=usePromises=true
Steps to reproduce

Generate client js code by running the below command on an example swagger.json

openapi-generator-cli generate -g javascript -i swagger.json -o api --additional-properties=usePromises=true

Navigate to the generated code api/src/ApiClient.js you will see the User-Agent code is there:

        /**
         * The default HTTP headers to be included for all API calls.
         * @type {Array.<String>}
         * @default {}
         */
        this.defaultHeaders = {
            'User-Agent': 'OpenAPI-Generator/1.0.0/Javascript'
        };

        /**

Even though it wasn't specified.

Related issues/PRs

I believe
this commit was where the issue was introduced.

Suggest a fix

The mustache template should probably check to see if the User Agent variable was passed before generating it.

@davesargrad
Copy link

davesargrad commented Nov 8, 2022

I have this problem as well. This is really an issue for us.
I've traced this to

callApi(path, httpMethod, pathParams,
        queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
        returnType, apiBasePath, callback)

image

The defaultHeaders found within the _this3: ApiClient scope:

image

It's been a few months since this bug report was first written. No one has picked this up. Does this imply that the openapi generator is no longer supported?

@davesargrad
Copy link

This is easily solved at the application tier. Simply by adding the following line after construction of an instance:

let defaultClient = ApiClient.instance
defaultClient.defaultHeaders = {}

I've tested this, and it eliminates the chrome error.

@wing328
Copy link
Member

wing328 commented Dec 22, 2024

closed via #20367 to add an option to skip setting the default header in ApiClient.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants