OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.04k stars 6.38k forks source link

[BUG] APIkey as query parameter is not passed in Java client #16362

Closed bodiam closed 4 days ago

bodiam commented 12 months ago

Bug Report Checklist

Description

I have a OpenAPI spec which uses a request parameter apikey. This is defined as follows:

    "securitySchemes": {
      "apikey": {
        "type": "apiKey",
        "name": "apikey",
        "in": "query"
      }
    }

(The full schema can be found here: https://gist.github.com/bodiam/d770c451e5a67629135c46261b291eac)

I have generated a Java client (and Kotlin client), and the Java client doesn't pass the query auth parameter to the endpoint. The Kotlin client, while having other issues, does send the apikey.

openapi-generator version

6.6.0

OpenAPI declaration file content or url

The problem is most likely in this piece of code:

        // update parameters with authentication settings
        updateParamsForAuth(authNames, allQueryParams, headerParams, cookieParams, requestBodyToString(reqBody), method, URI.create(url));

        final Request.Builder reqBuilder = new Request.Builder().url(url);
        processHeaderParams(headerParams, reqBuilder);
        processCookieParams(cookieParams, reqBuilder);

It seems the request params (allQueryParams) are updated in the updateParamsForAuth method, and while they are, they aren't used in the request, causing the call to fail.

This simple testcases reproduces it:

    @Test
    public void demoTest() throws ApiException {
        String color = "red";
        api.getApiClient().setApiKey("abcdef");
        api.demo(color);
    }
Generation Details

java -jar openapi-generator-cli-6.6.0.jar generate -i http://localhost:8080/v3/api-docs -g java -o /tmp/test/java

Steps to reproduce
  1. Use the attached schema
  2. Use the generation step above
  3. Run the mailbox controller test with the above code

Expected: the apikey is sent. Actual: it's not sent.

Suggest a fix

I think the requestbuilder needs to be updated with the correct request parameters.

wing328 commented 12 months ago

java -jar openapi-generator-cli-6.6.0.jar generate -i http://localhost:8080/v3/api-docs -g java -o /tmp/test/java

can you also please try --library jersey2 or --library native to see if these libraries work better with api key authentication in the query?

tsanti928 commented 5 months ago

+1 to this bug. I did confirm that neither jersey2 nor native fixed the issue.

spraot commented 2 months ago

I see this issue too