From ca031ea4cd2485b09cf1d0c687c72f0517287c90 Mon Sep 17 00:00:00 2001 From: UnleashSpirit Date: Fri, 23 Apr 2021 19:38:12 +0200 Subject: [PATCH 1/2] feat: add access to webclient and webclientBuilder fix: #8602 --- .../libraries/webclient/ApiClient.mustache | 95 +++++++++++++++---- .../org/openapitools/client/ApiClient.java | 91 ++++++++++++++---- 2 files changed, 148 insertions(+), 38 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 0fe89ccb026c..d6abce16ea3f 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 @@ -59,6 +59,9 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; + +import javax.annotation.Nullable; + {{#jsr310}} {{#threetenbp}} import org.threeten.bp.OffsetDateTime; @@ -98,45 +101,58 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { private final WebClient webClient; private final DateFormat dateFormat; + private final ObjectMapper objectMapper; private Map authentications; public ApiClient() { this.dateFormat = createDefaultDateFormat(); - ObjectMapper mapper = new ObjectMapper(); - mapper.setDateFormat(dateFormat); - mapper.registerModule(new JavaTimeModule()); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - {{#openApiNullable}} - JsonNullableModule jnm = new JsonNullableModule(); - mapper.registerModule(jnm); - {{/openApiNullable}} - - this.webClient = buildWebClient(mapper); + this.objectMapper = createDefaultObjectMapper(this.dateFormat); + this.webClient = buildWebClient(this.objectMapper); this.init(); } + public ApiClient(WebClient webClient) { + this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient()), createDefaultDateFormat()); + } + public ApiClient(ObjectMapper mapper, DateFormat format) { this(buildWebClient(mapper.copy()), format); } public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) { - this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy())), format); + this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient(mapper.copy())), format); } private ApiClient(WebClient webClient, DateFormat format) { this.webClient = webClient; this.dateFormat = format; + this.objectMapper = createDefaultObjectMapper(format); this.init(); } - public DateFormat createDefaultDateFormat() { + public static DateFormat createDefaultDateFormat() { DateFormat dateFormat = new RFC3339DateFormat(); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat; } + public static ObjectMapper createDefaultObjectMapper(@Nullable DateFormat dateFormat) { + if (null == dateFormat) { + dateFormat = createDefaultDateFormat(); + } + ObjectMapper mapper = new ObjectMapper(); + mapper.setDateFormat(dateFormat); + mapper.registerModule(new JavaTimeModule()); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + {{#openApiNullable}} + JsonNullableModule jnm = new JsonNullableModule(); + mapper.registerModule(jnm); + {{/openApiNullable}} + return mapper; + } + protected void init() { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}} @@ -149,20 +165,43 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { } /** - * Build the WebClient used to make HTTP requests. + * Build the WebClientBuilder used to make WebClient. * @return WebClient */ - public static WebClient buildWebClient(ObjectMapper mapper) { + public static WebClient.Builder buildWebClientBuilder(ObjectMapper mapper) { ExchangeStrategies strategies = ExchangeStrategies .builder() .codecs(clientDefaultCodecsConfigurer -> { clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON)); clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON)); }).build(); - WebClient.Builder webClient = WebClient.builder().exchangeStrategies(strategies); - return webClient.build(); + WebClient.Builder webClientBuilder = WebClient.builder().exchangeStrategies(strategies); + return webClientBuilder; + } + + /** + * Build the WebClientBuilder used to make WebClient. + * @return WebClient + */ + public static WebClient.Builder buildWebClientBuilder() { + return buildWebClientBuilder(createDefaultObjectMapper(null)); + } + + /** + * Build the WebClient used to make HTTP requests. + * @return WebClient + */ + public static WebClient buildWebClient(ObjectMapper mapper) { + return buildWebClientBuilder(mapper).build(); } + /** + * Build the WebClient used to make HTTP requests. + * @return WebClient + */ + public static WebClient buildWebClient() { + return buildWebClientBuilder(createDefaultObjectMapper(null)).build(); + } /** * Get the current base path @@ -352,6 +391,22 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return dateFormat.format(date); } + /** + * Get the ObjectMapper used to make HTTP requests. + * @return ObjectMapper objectMapper + */ + public ObjectMapper getObjectMapper() { + return objectMapper; + } + + /** + * Get the WebClient used to make HTTP requests. + * @return WebClient webClient + */ + public WebClient getWebClient() { + return webClient; + } + /** * Format the given parameter object into string. * @param param the object to convert @@ -655,10 +710,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { } // collectionFormat is assumed to be "csv" by default - if(collectionFormat == null) { - collectionFormat = CollectionFormat.CSV; - } + if(collectionFormat == null) { + collectionFormat = CollectionFormat.CSV; + } - return collectionFormat.collectionToString(values); + return collectionFormat.collectionToString(values); } } 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 c8b45d5a9038..98c64fbb17b0 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 @@ -57,6 +57,9 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; + +import javax.annotation.Nullable; + import java.time.OffsetDateTime; import org.openapitools.client.auth.Authentication; @@ -87,43 +90,56 @@ private String collectionToString(Collection collection) { private final WebClient webClient; private final DateFormat dateFormat; + private final ObjectMapper objectMapper; private Map authentications; public ApiClient() { this.dateFormat = createDefaultDateFormat(); - ObjectMapper mapper = new ObjectMapper(); - mapper.setDateFormat(dateFormat); - mapper.registerModule(new JavaTimeModule()); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - JsonNullableModule jnm = new JsonNullableModule(); - mapper.registerModule(jnm); - - this.webClient = buildWebClient(mapper); + this.objectMapper = createDefaultObjectMapper(this.dateFormat); + this.webClient = buildWebClient(this.objectMapper); this.init(); } + public ApiClient(WebClient webClient) { + this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient()), createDefaultDateFormat()); + } + public ApiClient(ObjectMapper mapper, DateFormat format) { this(buildWebClient(mapper.copy()), format); } public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) { - this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy())), format); + this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient(mapper.copy())), format); } private ApiClient(WebClient webClient, DateFormat format) { this.webClient = webClient; this.dateFormat = format; + this.objectMapper = createDefaultObjectMapper(format); this.init(); } - public DateFormat createDefaultDateFormat() { + public static DateFormat createDefaultDateFormat() { DateFormat dateFormat = new RFC3339DateFormat(); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat; } + public static ObjectMapper createDefaultObjectMapper(@Nullable DateFormat dateFormat) { + if (null == dateFormat) { + dateFormat = createDefaultDateFormat(); + } + ObjectMapper mapper = new ObjectMapper(); + mapper.setDateFormat(dateFormat); + mapper.registerModule(new JavaTimeModule()); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JsonNullableModule jnm = new JsonNullableModule(); + mapper.registerModule(jnm); + return mapper; + } + protected void init() { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); @@ -136,20 +152,43 @@ protected void init() { } /** - * Build the WebClient used to make HTTP requests. + * Build the WebClientBuilder used to make WebClient. * @return WebClient */ - public static WebClient buildWebClient(ObjectMapper mapper) { + public static WebClient.Builder buildWebClientBuilder(ObjectMapper mapper) { ExchangeStrategies strategies = ExchangeStrategies .builder() .codecs(clientDefaultCodecsConfigurer -> { clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON)); clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON)); }).build(); - WebClient.Builder webClient = WebClient.builder().exchangeStrategies(strategies); - return webClient.build(); + WebClient.Builder webClientBuilder = WebClient.builder().exchangeStrategies(strategies); + return webClientBuilder; } + /** + * Build the WebClientBuilder used to make WebClient. + * @return WebClient + */ + public static WebClient.Builder buildWebClientBuilder() { + return buildWebClientBuilder(createDefaultObjectMapper(null)); + } + + /** + * Build the WebClient used to make HTTP requests. + * @return WebClient + */ + public static WebClient buildWebClient(ObjectMapper mapper) { + return buildWebClientBuilder(mapper).build(); + } + + /** + * Build the WebClient used to make HTTP requests. + * @return WebClient + */ + public static WebClient buildWebClient() { + return buildWebClientBuilder(createDefaultObjectMapper(null)).build(); + } /** * Get the current base path @@ -337,6 +376,22 @@ public String formatDate(Date date) { return dateFormat.format(date); } + /** + * Get the ObjectMapper used to make HTTP requests. + * @return ObjectMapper objectMapper + */ + public ObjectMapper getObjectMapper() { + return objectMapper; + } + + /** + * Get the WebClient used to make HTTP requests. + * @return WebClient webClient + */ + public WebClient getWebClient() { + return webClient; + } + /** * Format the given parameter object into string. * @param param the object to convert @@ -640,10 +695,10 @@ public String collectionPathParameterToString(CollectionFormat collectionFormat, } // collectionFormat is assumed to be "csv" by default - if(collectionFormat == null) { - collectionFormat = CollectionFormat.CSV; - } + if(collectionFormat == null) { + collectionFormat = CollectionFormat.CSV; + } - return collectionFormat.collectionToString(values); + return collectionFormat.collectionToString(values); } } From ecd695cf5157aefa733b8964907ed8fd7932b92b Mon Sep 17 00:00:00 2001 From: UnleashSpirit Date: Mon, 26 Apr 2021 11:34:55 +0200 Subject: [PATCH 2/2] docs: add params for webclient & builder --- .../main/resources/Java/libraries/webclient/ApiClient.mustache | 2 ++ 1 file changed, 2 insertions(+) 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 d6abce16ea3f..6491013f5a84 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 @@ -166,6 +166,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { /** * Build the WebClientBuilder used to make WebClient. + * @param mapper ObjectMapper used for serialize/deserialize * @return WebClient */ public static WebClient.Builder buildWebClientBuilder(ObjectMapper mapper) { @@ -189,6 +190,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { /** * Build the WebClient used to make HTTP requests. + * @param mapper ObjectMapper used for serialize/deserialize * @return WebClient */ public static WebClient buildWebClient(ObjectMapper mapper) {