From b9d71581dd6106f97173cc47ff84c04a3525bb49 Mon Sep 17 00:00:00 2001 From: Bruno Coelho <4brunu@users.noreply.github.com> Date: Sat, 17 Sep 2022 15:08:07 +0100 Subject: [PATCH] [kotlin][client] fix file upload with okhttp (#13435) * [kotlin][client] fix file upload with okhttp * [kotlin][client] fix file upload with okhttp * [kotlin][client] update sample projects * [kotlin][client] fix file upload with okhttp3 * [kotlin][client] update sample projects --- .../infrastructure/ApiClient.kt.mustache | 56 +++++++++++++------ .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- .../client/infrastructure/ApiClient.kt | 29 +++++----- 21 files changed, 358 insertions(+), 278 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache index 915fa58d22e..b0c5f045795 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache @@ -116,6 +116,12 @@ import com.squareup.moshi.adapter protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + {{#jvm-okhttp3}} + content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) + {{/jvm-okhttp3}} + {{#jvm-okhttp4}} + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) + {{/jvm-okhttp4}} mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -123,27 +129,41 @@ import com.squareup.moshi.adapter // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart({{#jvm-okhttp3}}Headers.of(headers){{/jvm-okhttp3}}{{#jvm-okhttp4}}headers.toHeaders(){{/jvm-okhttp4}}, - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + {{#jvm-okhttp3}} + val fileMediaType = MediaType.parse(guessContentTypeFromFile(part.body)) + addPart( + Headers.of(partHeaders), + RequestBody.create(fileMediaType, part.body) + ) + {{/jvm-okhttp3}} + {{#jvm-okhttp4}} + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + {{/jvm-okhttp4}} + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + {{#jvm-okhttp3}} + addPart( + Headers.of(partHeaders), + RequestBody.create(null, parameterToString(part.body)) + ) + {{/jvm-okhttp3}} + {{#jvm-okhttp4}} + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) + {{/jvm-okhttp4}} } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - {{#jvm-okhttp3}} - content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) - {{/jvm-okhttp3}} - {{#jvm-okhttp4}} - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) - {{/jvm-okhttp4}} mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fc978529811..39227c9df0b 100644 --- a/samples/client/petstore/kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 68b873ff5fd..42e24d04773 100644 --- a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -65,6 +65,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -72,22 +73,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(Headers.of(headers), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = MediaType.parse(guessContentTypeFromFile(part.body)) + addPart( + Headers.of(partHeaders), + RequestBody.create(fileMediaType, part.body) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + Headers.of(partHeaders), + RequestBody.create(null, parameterToString(part.body)) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fc978529811..39227c9df0b 100644 --- a/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fc978529811..39227c9df0b 100644 --- a/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 68b873ff5fd..42e24d04773 100644 --- a/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-default-values-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -65,6 +65,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -72,22 +73,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(Headers.of(headers), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = MediaType.parse(guessContentTypeFromFile(part.body)) + addPart( + Headers.of(partHeaders), + RequestBody.create(fileMediaType, part.body) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + Headers.of(partHeaders), + RequestBody.create(null, parameterToString(part.body)) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fc978529811..39227c9df0b 100644 --- a/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fc978529811..39227c9df0b 100644 --- a/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index ec0a443253c..8b98ee5fe52 100644 --- a/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 404ba01dd99..de055e08795 100644 --- a/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 916312c0478..40e0198c1a4 100644 --- a/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -70,6 +70,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -77,22 +78,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 452d68570bd..06bc7af8cfd 100644 --- a/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -71,6 +71,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -78,22 +79,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fa5c0777627..7fea922b53a 100644 --- a/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fa5c0777627..7fea922b53a 100644 --- a/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 3a26e6b545f..026b024b61d 100644 --- a/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fa5c0777627..7fea922b53a 100644 --- a/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index f7997adfe2e..dadae38a87c 100644 --- a/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -65,6 +65,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -72,22 +73,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(Headers.of(headers), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = MediaType.parse(guessContentTypeFromFile(part.body)) + addPart( + Headers.of(partHeaders), + RequestBody.create(fileMediaType, part.body) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + Headers.of(partHeaders), + RequestBody.create(null, parameterToString(part.body)) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> RequestBody.create(MediaType.parse(mediaType ?: guessContentTypeFromFile(content)), content) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fa5c0777627..7fea922b53a 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 8fdd4f5cbfb..c2fb4091927 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index ac7394b1577..3f4c82be8ad 100644 --- a/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -69,6 +69,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -76,22 +77,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map> diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index fa5c0777627..7fea922b53a 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -68,6 +68,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { + content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormDataMediaType -> MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -75,22 +76,24 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - val contentType = part.headers.remove("Content-Type") - val bodies = if (part.body is Iterable<*>) part.body else listOf(part.body) - bodies.forEach { body -> - val headers = part.headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"" + if (body is File) "; filename=\"${body.name}\"" else "") - addPart(headers.toHeaders(), - requestSingleBody(body, contentType)) + if (part.body is File) { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${part.body.name}\"") + val fileMediaType = guessContentTypeFromFile(part.body).toMediaTypeOrNull() + addPart( + partHeaders.toHeaders(), + part.body.asRequestBody(fileMediaType) + ) + } else { + val partHeaders = part.headers.toMutableMap() + + ("Content-Disposition" to "form-data; name=\"$name\"") + addPart( + partHeaders.toHeaders(), + parameterToString(part.body).toRequestBody(null) + ) } } }.build() - else -> requestSingleBody(content, mediaType) - } - - protected inline fun requestSingleBody(content: T, mediaType: String?): RequestBody = - when { - content is File -> content.asRequestBody((mediaType ?: guessContentTypeFromFile(content)).toMediaTypeOrNull()) mediaType == FormUrlEncMediaType -> { FormBody.Builder().apply { // content's type *must* be Map>