From d0233f90e64256073a376ecf1c7aa2d19fd80de2 Mon Sep 17 00:00:00 2001 From: simingweng Date: Fri, 8 Jun 2018 16:03:36 -0400 Subject: [PATCH 1/3] add URL encoding to the query parameters in case they have unsafe characters --- .../Java/libraries/resttemplate/ApiClient.mustache | 12 ++++++++++++ .../main/java/org/openapitools/client/ApiClient.java | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache index 54f2b14573b3..c300acade7fb 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache @@ -46,6 +46,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.ParseException; @@ -527,6 +528,17 @@ public class ApiClient { final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path); if (queryParams != null) { + for (List values : queryParams.values()) { + if (values != null) { + for (int i = 0; i < values.size(); i++) { + try { + values.set(i, URLEncoder.encode(values.get(i), "utf8")); + } catch (UnsupportedEncodingException e) { + + } + } + } + } builder.queryParams(queryParams); } diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index 098bf04cccbe..12fb4d2184b4 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -35,6 +35,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.ParseException; @@ -515,6 +516,17 @@ public T invokeAPI(String path, HttpMethod method, MultiValueMap values : queryParams.values()) { + if (values != null) { + for (int i = 0; i < values.size(); i++) { + try { + values.set(i, URLEncoder.encode(values.get(i), "utf8")); + } catch (UnsupportedEncodingException e) { + + } + } + } + } builder.queryParams(queryParams); } From d0b663492c94721570edbf209ecf3c9ef3f9e25a Mon Sep 17 00:00:00 2001 From: simingweng Date: Fri, 8 Jun 2018 16:08:18 -0400 Subject: [PATCH 2/3] add comment --- .../resources/Java/libraries/resttemplate/ApiClient.mustache | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache index c300acade7fb..471a936ad579 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache @@ -528,6 +528,7 @@ public class ApiClient { final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path); if (queryParams != null) { + //encode the query parameters in case they contain unsafe characters for (List values : queryParams.values()) { if (values != null) { for (int i = 0; i < values.size(); i++) { From 6526bfa5a3b63d349b7f0f68a0a20b998583d5d3 Mon Sep 17 00:00:00 2001 From: simingweng Date: Sat, 9 Jun 2018 09:44:00 -0400 Subject: [PATCH 3/3] update generated samples --- .../java/org/openapitools/client/ApiClient.java | 13 +++++++++++++ .../java/org/openapitools/client/ApiClient.java | 1 + 2 files changed, 14 insertions(+) diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java index f4c90a6acda4..e8829cf00733 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java @@ -40,6 +40,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.ParseException; @@ -520,6 +521,18 @@ public T invokeAPI(String path, HttpMethod method, MultiValueMap values : queryParams.values()) { + if (values != null) { + for (int i = 0; i < values.size(); i++) { + try { + values.set(i, URLEncoder.encode(values.get(i), "utf8")); + } catch (UnsupportedEncodingException e) { + + } + } + } + } builder.queryParams(queryParams); } diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index 12fb4d2184b4..ebb0a2b0f83c 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -516,6 +516,7 @@ public T invokeAPI(String path, HttpMethod method, MultiValueMap values : queryParams.values()) { if (values != null) { for (int i = 0; i < values.size(); i++) {