diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java index ed459d5a86e..0639d27c6fb 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java @@ -849,7 +849,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen { ).trim(); return "multipart/form-data".equals(mediaType) || "application/x-www-form-urlencoded".equals(mediaType) - || (mediaType.startsWith("application/") && mediaType.endsWith("json")); + || (mediaType.startsWith("application/") && (mediaType.endsWith("json") || mediaType.endsWith("octet-stream"))); }; operation.consumes = operation.consumes == null ? null : operation.consumes.stream() .filter(isSerializable) 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 ea711753abd..650703ba5e5 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 @@ -86,6 +86,7 @@ import com.squareup.moshi.adapter protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -214,8 +215,15 @@ import com.squareup.moshi.adapter } {{/jvm-okhttp4}} mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + {{#jvm-okhttp3}} + RequestBody.create(MediaType.parse(OctetMediaType), content) + {{/jvm-okhttp3}} + {{#jvm-okhttp4}} + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) + {{/jvm-okhttp4}} // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } {{#moshi}} @@ -247,16 +255,19 @@ import com.squareup.moshi.adapter } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } {{#moshi}}Serializer.moshi.adapter().fromJson(bodyContent){{/moshi}}{{! }}{{#gson}}Serializer.gson.fromJson(bodyContent, (object: TypeToken(){}).getType()){{/gson}}{{! }}{{#jackson}}Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference() {}){{/jackson}}{{! }}{{#kotlinx_serialization}}Serializer.kotlinxSerializationJson.decodeFromString(bodyContent){{/kotlinx_serialization}} + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 ea4b7b65935..8e899898f01 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 ea4b7b65935..8e899898f01 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 047c68d10ce..81cbdad3ec3 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 @@ -35,6 +35,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -109,8 +110,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie ) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + RequestBody.create(MediaType.parse(OctetMediaType), content) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -130,13 +133,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 ea4b7b65935..8e899898f01 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 ea4b7b65935..8e899898f01 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 047c68d10ce..81cbdad3ec3 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 @@ -35,6 +35,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -109,8 +110,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie ) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + RequestBody.create(MediaType.parse(OctetMediaType), content) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -130,13 +133,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 ea4b7b65935..8e899898f01 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 ea4b7b65935..8e899898f01 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 0f1ca0fcb9c..a01ca44bbbb 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } protected inline fun responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { @@ -131,13 +134,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.gson.fromJson(bodyContent, (object: TypeToken(){}).getType()) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 5eb13189410..45b072f8278 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } protected inline fun responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { @@ -131,13 +134,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.jacksonObjectMapper.readValue(bodyContent, object: TypeReference() {}) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 235c92379b5..5f684f2428a 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 @@ -40,6 +40,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -113,8 +114,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } protected inline fun responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { @@ -137,13 +140,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.kotlinxSerializationJson.decodeFromString(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 67676016c1f..77aa25ca806 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 @@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -114,8 +115,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } protected inline fun responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { @@ -134,13 +137,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.gson.fromJson(bodyContent, (object: TypeToken(){}).getType()) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 81f05a77af3..5b2c0ad376a 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 81f05a77af3..5b2c0ad376a 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 ea4b7b65935..8e899898f01 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 16c2bf9bc65..e8f2e7bb489 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 @@ -38,6 +38,7 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ internal open class ApiClient(val baseUrl: String, val client: OkHttpClient = de } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 81f05a77af3..5b2c0ad376a 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 34db45767ad..0f1fae4e64a 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 @@ -35,6 +35,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -109,8 +110,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie ) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + RequestBody.create(MediaType.parse(OctetMediaType), content) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -130,13 +133,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 81f05a77af3..5b2c0ad376a 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 2592a3baad3..7a9960d7e1f 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 6459e6aff54..00e451fa20e 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 @@ -39,6 +39,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -112,8 +113,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } protected inline fun responseBody(body: ResponseBody?, mediaType: String? = JsonMediaType): T? { @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.kotlinxSerializationJson.decodeFromString(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } } 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 81f05a77af3..5b2c0ad376a 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 @@ -38,6 +38,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie protected const val FormDataMediaType = "multipart/form-data" protected const val FormUrlEncMediaType = "application/x-www-form-urlencoded" protected const val XmlMediaType = "application/xml" + protected const val OctetMediaType = "application/octet-stream" val apiKey: MutableMap = mutableMapOf() val apiKeyPrefix: MutableMap = mutableMapOf() @@ -111,8 +112,10 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie .toRequestBody((mediaType ?: JsonMediaType).toMediaTypeOrNull()) } mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.") + mediaType == OctetMediaType && content is ByteArray -> + content.toRequestBody(OctetMediaType.toMediaTypeOrNull()) // TODO: this should be extended with other serializers - else -> throw UnsupportedOperationException("requestBody currently only supports JSON body and File body.") + else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.") } @OptIn(ExperimentalStdlibApi::class) @@ -132,13 +135,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie } return tempFile as T } - val bodyContent = body.string() - if (bodyContent.isEmpty()) { - return null - } + return when { - mediaType==null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> + mediaType == null || (mediaType.startsWith("application/") && mediaType.endsWith("json")) -> { + val bodyContent = body.string() + if (bodyContent.isEmpty()) { + return null + } Serializer.moshi.adapter().fromJson(bodyContent) + } + mediaType == OctetMediaType -> body.bytes() as? T else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.") } }