Skip to content

Commit

Permalink
[kotlin-client][jvm-spring-restclient] Extract data from PartConfig f…
Browse files Browse the repository at this point in the history
…or multipart/form-data requests (OpenAPITools#20598)
  • Loading branch information
zakupower authored Feb 6, 2025
1 parent 9a9c108 commit c74dfa3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,23 @@ import org.springframework.util.LinkedMultiValueMap
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) body(requestConfig.body) }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val parts = LinkedMultiValueMap<String, Any>()
(requestConfig.body as Map<String, PartConfig<*>>).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 <reified T: Any> parseDateToQueryString(value : T): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) {
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) body(requestConfig.body) }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val parts = LinkedMultiValueMap<String, Any>()
(requestConfig.body as Map<String, PartConfig<*>>).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 <reified T: Any> parseDateToQueryString(value : T): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,23 @@ open class ApiClient(protected val client: RestClient) {
private fun <I> RestClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) =
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } }

private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
apply { if (requestConfig.body != null) body(requestConfig.body) }
private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
when {
requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
val parts = LinkedMultiValueMap<String, Any>()
(requestConfig.body as Map<String, PartConfig<*>>).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 <reified T: Any> parseDateToQueryString(value : T): String {
Expand Down

0 comments on commit c74dfa3

Please sign in to comment.