From b4ae2896e308caaeb91c5817431adb2a88bd7de2 Mon Sep 17 00:00:00 2001 From: "hugo.barrigas@mindera.com" Date: Wed, 22 Aug 2018 18:10:46 +0100 Subject: [PATCH 1/6] add constructor to allow custom webclient to be passed into ApiClient --- .../Java/libraries/webclient/ApiClient.mustache | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache index 28260368a9b3..1256d3b73a30 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache @@ -33,6 +33,7 @@ import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ExchangeStrategies; import reactor.core.publisher.Mono; import reactor.core.publisher.Flux; +import java.util.Optional; import java.io.BufferedReader; import java.io.IOException; @@ -76,7 +77,7 @@ public class ApiClient { } private HttpHeaders defaultHeaders = new HttpHeaders(); - + private String basePath = "{{basePath}}"; private final WebClient webClient; @@ -94,6 +95,10 @@ public class ApiClient { this(buildWebClient(mapper.copy(), format), format); } + public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) { + this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy(), format)), format); + } + private ApiClient(WebClient webClient, DateFormat format) { this.webClient = webClient; this.dateFormat = format; @@ -104,7 +109,7 @@ public class ApiClient { dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat; } - + protected void init() { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap();{{#authMethods}}{{#isBasic}} @@ -132,7 +137,7 @@ public class ApiClient { return webClient.build(); } - + /** * Get the current base path * @return String the base path @@ -585,7 +590,7 @@ public class ApiClient { auth.applyToParams(queryParams, headerParams); } } - + private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private final Log log = LogFactory.getLog(ApiClientHttpRequestInterceptor.class); @@ -624,7 +629,7 @@ public class ApiClient { builder.setLength(builder.length() - 1); // Get rid of trailing comma return builder.toString(); } - + private String bodyToString(InputStream body) throws IOException { StringBuilder builder = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8)); From 8d99ddefbb5b2b5b18d1beeabf296b659e7c2bd3 Mon Sep 17 00:00:00 2001 From: "hugo.barrigas@mindera.com" Date: Thu, 23 Aug 2018 11:45:27 +0100 Subject: [PATCH 2/6] run ./bin/java-petstore-webclient.sh to generate new ApiClient --- .../java/org/openapitools/client/ApiClient.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java index ee1885811e6f..211a522a0f84 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java @@ -33,6 +33,7 @@ import org.springframework.web.reactive.function.client.ExchangeStrategies; import reactor.core.publisher.Mono; import reactor.core.publisher.Flux; +import java.util.Optional; import java.io.BufferedReader; import java.io.IOException; @@ -76,7 +77,7 @@ private String collectionToString(Collection collection) } private HttpHeaders defaultHeaders = new HttpHeaders(); - + private String basePath = "http://petstore.swagger.io:80/v2"; private final WebClient webClient; @@ -94,6 +95,10 @@ public ApiClient(ObjectMapper mapper, DateFormat format) { this(buildWebClient(mapper.copy(), format), format); } + public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) { + this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy(), format)), format); + } + private ApiClient(WebClient webClient, DateFormat format) { this.webClient = webClient; this.dateFormat = format; @@ -104,7 +109,7 @@ public DateFormat createDefaultDateFormat() { dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat; } - + protected void init() { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); @@ -133,7 +138,7 @@ public static WebClient buildWebClient(ObjectMapper mapper, DateFormat dateForma return webClient.build(); } - + /** * Get the current base path * @return String the base path @@ -586,7 +591,7 @@ private void updateParamsForAuth(String[] authNames, MultiValueMap Date: Thu, 23 Aug 2018 18:02:51 +0100 Subject: [PATCH 3/6] call init function for proper authorizations instantiation. --- .../main/resources/Java/libraries/webclient/ApiClient.mustache | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache index 1256d3b73a30..61119a80c622 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache @@ -88,7 +88,7 @@ public class ApiClient { public ApiClient() { this.dateFormat = createDefaultDateFormat(); - this.webClient = buildWebClient(new ObjectMapper(), this.dateFormat); + this(buildWebClient(new ObjectMapper(), this.dateFormat), this.dateFormat); } public ApiClient(ObjectMapper mapper, DateFormat format) { @@ -102,6 +102,7 @@ public class ApiClient { private ApiClient(WebClient webClient, DateFormat format) { this.webClient = webClient; this.dateFormat = format; + this.init(); } public DateFormat createDefaultDateFormat() { From 44f07e4645e242ea81e6156b275f33e8ae8f7c60 Mon Sep 17 00:00:00 2001 From: "hugo.barrigas@mindera.com" Date: Thu, 23 Aug 2018 18:16:18 +0100 Subject: [PATCH 4/6] call init function for proper authorizations instantiation. --- .../main/resources/Java/libraries/webclient/ApiClient.mustache | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache index 61119a80c622..41b81a004c5f 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache @@ -88,7 +88,8 @@ public class ApiClient { public ApiClient() { this.dateFormat = createDefaultDateFormat(); - this(buildWebClient(new ObjectMapper(), this.dateFormat), this.dateFormat); + this.webClient = buildWebClient(new ObjectMapper(), dateFormat); + this.init(); } public ApiClient(ObjectMapper mapper, DateFormat format) { From 0a2822aee4301015dd73765d29c5ddf457cc4a70 Mon Sep 17 00:00:00 2001 From: "hugo.barrigas@mindera.com" Date: Thu, 23 Aug 2018 18:19:07 +0100 Subject: [PATCH 5/6] run ./bin --- .../src/main/java/org/openapitools/client/ApiClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java index 211a522a0f84..2eacb1e1de1c 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java @@ -88,7 +88,7 @@ private String collectionToString(Collection collection) public ApiClient() { this.dateFormat = createDefaultDateFormat(); - this.webClient = buildWebClient(new ObjectMapper(), this.dateFormat); + this(buildWebClient(new ObjectMapper(), this.dateFormat), this.dateFormat); } public ApiClient(ObjectMapper mapper, DateFormat format) { @@ -102,6 +102,7 @@ public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) { private ApiClient(WebClient webClient, DateFormat format) { this.webClient = webClient; this.dateFormat = format; + this.init(); } public DateFormat createDefaultDateFormat() { From c0c1d742866350dd11e20047f9505313c905092f Mon Sep 17 00:00:00 2001 From: "hugo.barrigas@mindera.com" Date: Thu, 23 Aug 2018 18:56:27 +0100 Subject: [PATCH 6/6] add correct ApiClient example --- .../src/main/java/org/openapitools/client/ApiClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java index 2eacb1e1de1c..460bd563c0f2 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java @@ -88,7 +88,8 @@ private String collectionToString(Collection collection) public ApiClient() { this.dateFormat = createDefaultDateFormat(); - this(buildWebClient(new ObjectMapper(), this.dateFormat), this.dateFormat); + this.webClient = buildWebClient(new ObjectMapper(), dateFormat); + this.init(); } public ApiClient(ObjectMapper mapper, DateFormat format) {