From 3325edc5c535fda0b1755aae5d3a191fa9f591f6 Mon Sep 17 00:00:00 2001 From: Jens Wetterich Date: Wed, 12 Jul 2023 04:17:53 +0200 Subject: [PATCH] [kotlin][spring-webclient] Add parseDateToQueryString (#16069) --- .../infrastructure/ApiClient.kt.mustache | 17 +++++++++++++++++ .../client/infrastructure/ApiClient.kt | 10 ++++++++++ .../client/infrastructure/ApiClient.kt | 10 ++++++++++ 3 files changed, 37 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-webclient/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-webclient/infrastructure/ApiClient.kt.mustache index 452d09ecf43..85f5a40469d 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-webclient/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-webclient/infrastructure/ApiClient.kt.mustache @@ -53,3 +53,20 @@ open class ApiClient(protected val client: WebClient) { private fun WebClient.RequestBodySpec.body(requestConfig: RequestConfig) = apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } } + +inline fun parseDateToQueryString(value : T): String { + {{#toJson}} + /* + .replace("\"", "") converts the json object string to an actual string for the query parameter. + The moshi or gson adapter allows a more generic solution instead of trying to use a native + formatter. It also easily allows to provide a simple way to define a custom date format pattern + inside a gson/moshi adapter. + */ + {{#jackson}} + return Serializer.jacksonObjectMapper.writeValueAsString(value).replace("\"", "") + {{/jackson}} + {{/toJson}} + {{^toJson}} + return value.toString() + {{/toJson}} + } diff --git a/samples/client/petstore/kotlin-jvm-spring-2-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-spring-2-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 453ff321f1f..2011dd43cd1 100644 --- a/samples/client/petstore/kotlin-jvm-spring-2-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-spring-2-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -53,3 +53,13 @@ open class ApiClient(protected val client: WebClient) { private fun WebClient.RequestBodySpec.body(requestConfig: RequestConfig) = apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } } + +inline fun parseDateToQueryString(value : T): String { + /* + .replace("\"", "") converts the json object string to an actual string for the query parameter. + The moshi or gson adapter allows a more generic solution instead of trying to use a native + formatter. It also easily allows to provide a simple way to define a custom date format pattern + inside a gson/moshi adapter. + */ + return Serializer.jacksonObjectMapper.writeValueAsString(value).replace("\"", "") + } diff --git a/samples/client/petstore/kotlin-jvm-spring-3-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-spring-3-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 453ff321f1f..2011dd43cd1 100644 --- a/samples/client/petstore/kotlin-jvm-spring-3-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-spring-3-webclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -53,3 +53,13 @@ open class ApiClient(protected val client: WebClient) { private fun WebClient.RequestBodySpec.body(requestConfig: RequestConfig) = apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } } + +inline fun parseDateToQueryString(value : T): String { + /* + .replace("\"", "") converts the json object string to an actual string for the query parameter. + The moshi or gson adapter allows a more generic solution instead of trying to use a native + formatter. It also easily allows to provide a simple way to define a custom date format pattern + inside a gson/moshi adapter. + */ + return Serializer.jacksonObjectMapper.writeValueAsString(value).replace("\"", "") + }