diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-restclient/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-restclient/infrastructure/ApiClient.kt.mustache index 62876703d700..ec9ac2f6f2ef 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-restclient/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-restclient/infrastructure/ApiClient.kt.mustache @@ -46,8 +46,23 @@ import org.springframework.util.LinkedMultiValueMap private fun RestClient.RequestBodySpec.headers(requestConfig: RequestConfig) = apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } - private fun RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig) = - apply { if (requestConfig.body != null) body(requestConfig.body) } + private fun RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig): RestClient.RequestBodySpec { + when { + requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> { + val parts = LinkedMultiValueMap() + (requestConfig.body as Map>).forEach { (name, part) -> + if (part.body != null) { + parts.add(name, part.body) + } + } + return apply { body(parts) } + } + + else -> { + return apply { if (requestConfig.body != null) body(requestConfig.body) } + } + } + } } {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}inline fun parseDateToQueryString(value : T): String { diff --git a/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 64f843ed93be..426ad04918f9 100644 --- a/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/echo_api/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) { private fun RestClient.RequestBodySpec.headers(requestConfig: RequestConfig) = apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } - private fun RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig) = - apply { if (requestConfig.body != null) body(requestConfig.body) } + private fun RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig): RestClient.RequestBodySpec { + when { + requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> { + val parts = LinkedMultiValueMap() + (requestConfig.body as Map>).forEach { (name, part) -> + if (part.body != null) { + parts.add(name, part.body) + } + } + return apply { body(parts) } + } + + else -> { + return apply { if (requestConfig.body != null) body(requestConfig.body) } + } + } + } } inline fun parseDateToQueryString(value : T): String { diff --git a/samples/client/petstore/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 64f843ed93be..426ad04918f9 100644 --- a/samples/client/petstore/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-spring-3-restclient/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) { private fun RestClient.RequestBodySpec.headers(requestConfig: RequestConfig) = apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } - private fun RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig) = - apply { if (requestConfig.body != null) body(requestConfig.body) } + private fun RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig): RestClient.RequestBodySpec { + when { + requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> { + val parts = LinkedMultiValueMap() + (requestConfig.body as Map>).forEach { (name, part) -> + if (part.body != null) { + parts.add(name, part.body) + } + } + return apply { body(parts) } + } + + else -> { + return apply { if (requestConfig.body != null) body(requestConfig.body) } + } + } + } } inline fun parseDateToQueryString(value : T): String {