Skip to content

Commit d14eab8

Browse files
authored
[kotlin-client][jvm-spring-webclient] Extract data from PartConfig for multipart/form-data requests (#19811)
1 parent 627c0f4 commit d14eab8

File tree

4 files changed

+76
-8
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-webclient/infrastructure
  • samples/client
    • echo_api/kotlin-jvm-spring-3-webclient/src/main/kotlin/org/openapitools/client/infrastructure
    • petstore
      • kotlin-jvm-spring-2-webclient/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jvm-spring-3-webclient/src/main/kotlin/org/openapitools/client/infrastructure

4 files changed

+76
-8
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
66
import org.springframework.http.MediaType
77
import org.springframework.web.reactive.function.client.WebClient
88
import org.springframework.http.ResponseEntity
9+
import org.springframework.http.client.MultipartBodyBuilder
910
import org.springframework.util.LinkedMultiValueMap
1011
import reactor.core.publisher.Mono
1112

@@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
4748
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
4849
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
4950

50-
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
51-
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
51+
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
52+
when {
53+
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
54+
val builder = MultipartBodyBuilder()
55+
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
56+
if (part.body != null) {
57+
val partBuilder = builder.part(name, part.body)
58+
val partHeaders = part.headers
59+
partHeaders.forEach { partBuilder.header(it.key, it.value) }
60+
}
61+
}
62+
return apply { bodyValue(builder.build()) }
63+
}
64+
else -> {
65+
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
66+
}
67+
}
68+
}
5269
}
5370

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

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
66
import org.springframework.http.MediaType
77
import org.springframework.web.reactive.function.client.WebClient
88
import org.springframework.http.ResponseEntity
9+
import org.springframework.http.client.MultipartBodyBuilder
910
import org.springframework.util.LinkedMultiValueMap
1011
import reactor.core.publisher.Mono
1112

@@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
4748
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
4849
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
4950

50-
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
51-
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
51+
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
52+
when {
53+
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
54+
val builder = MultipartBodyBuilder()
55+
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
56+
if (part.body != null) {
57+
val partBuilder = builder.part(name, part.body)
58+
val partHeaders = part.headers
59+
partHeaders.forEach { partBuilder.header(it.key, it.value) }
60+
}
61+
}
62+
return apply { bodyValue(builder.build()) }
63+
}
64+
else -> {
65+
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
66+
}
67+
}
68+
}
5269
}
5370

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

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
66
import org.springframework.http.MediaType
77
import org.springframework.web.reactive.function.client.WebClient
88
import org.springframework.http.ResponseEntity
9+
import org.springframework.http.client.MultipartBodyBuilder
910
import org.springframework.util.LinkedMultiValueMap
1011
import reactor.core.publisher.Mono
1112

@@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
4748
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
4849
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
4950

50-
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
51-
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
51+
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
52+
when {
53+
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
54+
val builder = MultipartBodyBuilder()
55+
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
56+
if (part.body != null) {
57+
val partBuilder = builder.part(name, part.body)
58+
val partHeaders = part.headers
59+
partHeaders.forEach { partBuilder.header(it.key, it.value) }
60+
}
61+
}
62+
return apply { bodyValue(builder.build()) }
63+
}
64+
else -> {
65+
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
66+
}
67+
}
68+
}
5269
}
5370

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

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
66
import org.springframework.http.MediaType
77
import org.springframework.web.reactive.function.client.WebClient
88
import org.springframework.http.ResponseEntity
9+
import org.springframework.http.client.MultipartBodyBuilder
910
import org.springframework.util.LinkedMultiValueMap
1011
import reactor.core.publisher.Mono
1112

@@ -47,8 +48,24 @@ open class ApiClient(protected val client: WebClient) {
4748
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
4849
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }
4950

50-
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
51-
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
51+
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
52+
when {
53+
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
54+
val builder = MultipartBodyBuilder()
55+
(requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
56+
if (part.body != null) {
57+
val partBuilder = builder.part(name, part.body)
58+
val partHeaders = part.headers
59+
partHeaders.forEach { partBuilder.header(it.key, it.value) }
60+
}
61+
}
62+
return apply { bodyValue(builder.build()) }
63+
}
64+
else -> {
65+
return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
66+
}
67+
}
68+
}
5269
}
5370

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

0 commit comments

Comments
 (0)