OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] retrofit2 ApiKeyAuth
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
We have use cases where our apiClient needs to send query parameters with '&' in them. e.g. line1=Selfridges & Co. We are also using ApiKeyAuth on our generated ApiClient to append an apiKey as a query parameter.
Because we want the '&' to not be a query param delimiter it needs to be encoded before the request is sent. Retrofit does currently encode this, until it reaches the Authorisation interceptor which the '&' gets decoded and the request is malformed and behaves incorrectly.
The current implementation of ApiKeyAuth however gets a decoded query string
String newQuery = request.url().uri().getQuery();
adds the apiKey then reforms a URI from a decoded query string
newUri = new URI(request.url().uri().getScheme(), request.url().uri().getAuthority(),
request.url().uri().getPath(), newQuery, request.url().uri().getFragment());
This causes issues when the decoded query string has '&' in it, new URI treats the '&' as query delimiter and forms a malformed request.
I don't see why we need to decompose the request all the way to a URI then a string and rebuild back to a request. When we can simply do the following, which also understands how to not malform the existing query parameters.
HttpUrl url = request.url().newBuilder().addQueryParameter(API_KEY_QUERY_NAME, this.getApiKey()).build();
request = request.newBuilder().url(url).build();
This is the solution we have used to get it working by manually overriding the implementation of ApiKeyAuth
openapi-generator version
6.1.0
Related issues/PRs
Suggest a fix
Replace the query param implementation of ApiKeyAuth with
HttpUrl url = request.url().newBuilder().addQueryParameter(API_KEY_QUERY_NAME, this.getApiKey()).build();
request = request.newBuilder().url(url).build();
return chain.proceed(request);
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
Locate the generated Java ApiKeyAuth implementation and inspect how it reads and rebuilds the request query. Reproduce the encoded ampersand case, then verify that adding the API key preserves existing query parameters and produces a valid request without malformed delimiters.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100