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 bc0027e7405..296a4489ebc 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 @@ -114,48 +114,6 @@ import com.squareup.moshi.adapter return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -167,18 +125,21 @@ import com.squareup.moshi.adapter // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() diff --git a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e59fa0b5a48..0b323c1c6a8 100644 --- a/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 e59fa0b5a48..0b323c1c6a8 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 e59fa0b5a48..0b323c1c6a8 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 e59fa0b5a48..0b323c1c6a8 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 e59fa0b5a48..0b323c1c6a8 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 e59fa0b5a48..0b323c1c6a8 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() diff --git a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index b76315bd9e8..ebe19a23904 100644 --- a/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -85,48 +85,6 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ public open class ApiClient(public val baseUrl: String, public val client: Call. // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 1911b1d4788..97cbbafeffc 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 44095e73f25..78393b4e1f0 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 70539f9c852..0fefe31762b 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 @@ -87,48 +87,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -140,18 +98,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 5a8f81566cf..1665b2b38ed 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 @@ -88,48 +88,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -141,18 +99,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() diff --git a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 0867fa0c317..abfd0da3bad 100644 --- a/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 0867fa0c317..abfd0da3bad 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 0867fa0c317..abfd0da3bad 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() diff --git a/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e59fa0b5a48..0b323c1c6a8 100644 --- a/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 ad481e08ba5..0e872a9db2f 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 @@ -85,48 +85,6 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ internal open class ApiClient(val baseUrl: String, val client: Call.Factory = de // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 0867fa0c317..abfd0da3bad 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 0867fa0c317..abfd0da3bad 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 83cf91d8928..b80c6309767 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 09a59bf60d7..721cd7955d9 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 @@ -86,48 +86,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -139,18 +97,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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() 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 0867fa0c317..abfd0da3bad 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 @@ -85,48 +85,6 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie return contentType ?: "application/octet-stream" } - /** - * Adds a File to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is a File - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param file The file that will be added as the field value - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, file: File) { - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"; filename=\"${file.name}\"") - val fileMediaType = guessContentTypeFromFile(file).toMediaTypeOrNull() - addPart( - partHeaders.toHeaders(), - file.asRequestBody(fileMediaType) - ) - } - - /** - * Adds any type to a MultipartBody.Builder - * Defined a helper in the requestBody method to not duplicate code - * It will be used when the content is a FormDataMediaType and the body of the PartConfig is not a File. - * - * @param name The field name to add in the request - * @param headers The headers that are in the PartConfig - * @param obj The field name to add in the request - * @return The method returns Unit but the new Part is added to the Builder that the extension function is applying on - * @see requestBody - */ - protected fun MultipartBody.Builder.addPartToMultiPart(name: String, headers: Map, obj: T?) { - if (obj == null) return - val partHeaders = headers.toMutableMap() + - ("Content-Disposition" to "form-data; name=\"$name\"") - addPart( - partHeaders.toHeaders(), - parameterToString(obj).toRequestBody(null) - ) - } - protected inline fun requestBody(content: T, mediaType: String?): RequestBody = when { content is ByteArray -> content.toRequestBody((mediaType ?: guessContentTypeFromByteArray(content)).toMediaTypeOrNull()) @@ -138,18 +96,21 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie // content's type *must* be Map> @Suppress("UNCHECKED_CAST") (content as Map>).forEach { (name, part) -> - when (part.body) { - is File -> addPartToMultiPart(name, part.headers, part.body) - is List<*> -> { - part.body.forEach { - if (it is File) { - addPartToMultiPart(name, part.headers, it) - } else { - addPartToMultiPart(name, part.headers, it) - } - } - } - else -> addPartToMultiPart(name, part.headers, part.body) + 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()