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 fe3c172a629..41b81a004c5 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; @@ -87,20 +88,22 @@ public class ApiClient { public ApiClient() { this.dateFormat = createDefaultDateFormat(); - this.webClient = buildWebClient(new ObjectMapper(), this.dateFormat); - - init(); + this.webClient = buildWebClient(new ObjectMapper(), dateFormat); + this.init(); } 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; - - init(); + this.init(); } public DateFormat createDefaultDateFormat() { @@ -108,7 +111,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}} @@ -136,7 +139,7 @@ public class ApiClient { return webClient.build(); } - + /** * Get the current base path * @return String the base path @@ -589,7 +592,7 @@ public class ApiClient { auth.applyToParams(queryParams, headerParams); } } - + private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private final Log log = LogFactory.getLog(ApiClientHttpRequestInterceptor.class); @@ -628,7 +631,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)); 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 88b883f366b..460bd563c0f 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.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 = "http://petstore.swagger.io:80/v2"; private final WebClient webClient; @@ -87,20 +88,22 @@ public class ApiClient { public ApiClient() { this.dateFormat = createDefaultDateFormat(); - this.webClient = buildWebClient(new ObjectMapper(), this.dateFormat); - - init(); + this.webClient = buildWebClient(new ObjectMapper(), dateFormat); + this.init(); } 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; - - init(); + this.init(); } public DateFormat createDefaultDateFormat() { @@ -108,7 +111,7 @@ public class ApiClient { dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat; } - + protected void init() { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); @@ -137,7 +140,7 @@ public class ApiClient { return webClient.build(); } - + /** * Get the current base path * @return String the base path @@ -590,7 +593,7 @@ public class ApiClient { auth.applyToParams(queryParams, headerParams); } } - + private class ApiClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private final Log log = LogFactory.getLog(ApiClientHttpRequestInterceptor.class); @@ -629,7 +632,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));