Skip to content

Commit 7186490

Browse files
committed
[kotlin-client][jvm-spring-restclient] Extract data from PartConfig for multipart/form-data requests
1 parent 8b52b0a commit 7186490

File tree

3 files changed

+51
-6
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-restclient/infrastructure
  • samples/client
    • echo_api/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure
    • petstore/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure

3 files changed

+51
-6
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-restclient/infrastructure/ApiClient.kt.mustache

+17-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,23 @@ import org.springframework.util.LinkedMultiValueMap
4646
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
4747
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
4848

49-
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
50-
apply { if (requestConfig.body != null) body(requestConfig.body) }
49+
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
50+
when {
51+
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
52+
val parts = LinkedMultiValueMap<String, Any>()
53+
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
54+
if (part.body != null) {
55+
parts.add(name, part.body)
56+
}
57+
}
58+
return apply { body(parts) }
59+
}
60+
61+
else -> {
62+
return apply { if (requestConfig.body != null) body(requestConfig.body) }
63+
}
64+
}
65+
}
5166
}
5267

5368
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}inline fun <reified T: Any> parseDateToQueryString(value : T): String {

samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

+17-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) {
4646
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
4747
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
4848

49-
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
50-
apply { if (requestConfig.body != null) body(requestConfig.body) }
49+
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
50+
when {
51+
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
52+
val parts = LinkedMultiValueMap<String, Any>()
53+
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
54+
if (part.body != null) {
55+
parts.add(name, part.body)
56+
}
57+
}
58+
return apply { body(parts) }
59+
}
60+
61+
else -> {
62+
return apply { if (requestConfig.body != null) body(requestConfig.body) }
63+
}
64+
}
65+
}
5166
}
5267

5368
inline fun <reified T: Any> parseDateToQueryString(value : T): String {

samples/client/petstore/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

+17-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) {
4646
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
4747
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
4848

49-
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
50-
apply { if (requestConfig.body != null) body(requestConfig.body) }
49+
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
50+
when {
51+
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
52+
val parts = LinkedMultiValueMap<String, Any>()
53+
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
54+
if (part.body != null) {
55+
parts.add(name, part.body)
56+
}
57+
}
58+
return apply { body(parts) }
59+
}
60+
61+
else -> {
62+
return apply { if (requestConfig.body != null) body(requestConfig.body) }
63+
}
64+
}
65+
}
5166
}
5267

5368
inline fun <reified T: Any> parseDateToQueryString(value : T): String {

0 commit comments

Comments
 (0)