You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@TestpublicvoidparamMapIssue() throwsIOException, InterruptedException {
MockWebServerserver = newMockWebServer();
server.enqueue(newMockResponse().setResponseCode(200).setBody("[]"));
server.enqueue(newMockResponse().setResponseCode(200).setBody("[]"));
server.start();
try {
Stringendpoint = "http://localhost:" + server.getPort();
NamesGetApinamesGetApi = newApiClient().setBasePath(endpoint).buildClient(NamesGetApi.class);
{
namesGetApi.getNames("this is a test");
RecordedRequestrecordedRequest = server.takeRequest();
assertThat(recordedRequest.getRequestUrl().queryParameterValues("query"))
.containsExactly("this is a test");
}
{
namesGetApi.getNames(newGetNamesQueryParams().query("this is a test"));
RecordedRequestrecordedRequest = server.takeRequest();
assertThat(recordedRequest.getRequestUrl().queryParameterValues("query"))
.containsExactly("this is a test");
}
} finally {
server.shutdown();
}
}
The first assert is successful while the second fails.
The printed logs:
INFO: MockWebServer[50764] starting to accept connections
Aug 13, 2019 6:07:07 PM okhttp3.mockwebserver.MockWebServer$SocketHandler processOneRequest
INFO: MockWebServer[50764] received request: GET /names?query=this%20is%20a%20test HTTP/1.1 and responded: HTTP/1.1 200 OK
Aug 13, 2019 6:07:07 PM okhttp3.mockwebserver.MockWebServer$SocketHandler processOneRequest
INFO: MockWebServer[50764] received request: GET /names?query=this%2Bis%2Ba%2Btest HTTP/1.1 and responded: HTTP/1.1 200 OK
Aug 13, 2019 6:07:07 PM okhttp3.mockwebserver.MockWebServer acceptConnections
INFO: MockWebServer[50764] done accepting connections: Socket closed
As you can see, when the parameter map is used, the blank spaces in the string are converted to a + sign and then escaped with %2B, which results in an incorrect decoding on the server side.
As per Feign documentation:
What about plus? +
Per the URI specification, a + sign is allowed in both the path and query segments of a URI, however, handling of the symbol on the query can be inconsistent. In some legacy systems, the + is equivalent to the a space. Feign takes the approach of modern systems, where a + symbol should not represent a space and is explicitly encoded as %2B when found on a query string.
If you wish to use + as a space, then use the literal character or encode the value directly as %20
Suggest a fix
Even if not very elegant, a possible solution is to add .replaceAll("//+", "%20") every time the java.net.URLEncoder is used in the EncodingUtils file.
The text was updated successfully, but these errors were encountered:
👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
macjohnny
changed the title
[BUG][Java][Client]
[BUG][Java][Client][Feign] does not properly encode query parameters when using a parameter map
Aug 14, 2019
Bug Report Checklist
Description
The generate Feign Java client does not properly encode query parameters when using a parameter map.
openapi-generator version
4.1.0
OpenAPI declaration file content or url
Test declaration file:
Command line used for generation
java -cp openapi-generator-cli-4.1.0.jar org.openapitools.codegen.OpenAPIGenerator generate -i paramMapIssue.yaml -g java --library feign --additional-properties "feignVersion=10.x" -o src
Steps to reproduce
Here is a small unit test to reproduce the issue:
The first assert is successful while the second fails.
The printed logs:
As you can see, when the parameter map is used, the blank spaces in the string are converted to a + sign and then escaped with
%2B
, which results in an incorrect decoding on the server side.As per Feign documentation:
Suggest a fix
Even if not very elegant, a possible solution is to add
.replaceAll("//+", "%20")
every time thejava.net.URLEncoder
is used in theEncodingUtils
file.The text was updated successfully, but these errors were encountered: