Skip to content

Commit 5723ed6

Browse files
committed
feat: support text/plain in kotlin okhttp client
1 parent 4298c38 commit 5723ed6

File tree

23 files changed

+134
-45
lines changed
  • modules/openapi-generator/src/main
  • samples/client
    • others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure
    • petstore
      • kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jackson/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-json-request-string/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-kotlinx-datetime/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-modelMutable/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-moshi-codegen/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-name-parameter-mappings/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-nonpublic/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-nullable/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-uppercase-enum/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin/src/main/kotlin/org/openapitools/client/infrastructure

23 files changed

+134
-45
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,8 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
963963
return "multipart/form-data".equals(mediaType)
964964
|| "application/x-www-form-urlencoded".equals(mediaType)
965965
|| (mediaType.startsWith("application/") && mediaType.endsWith("json"))
966-
|| "application/octet-stream".equals(mediaType);
966+
|| "application/octet-stream".equals(mediaType)
967+
|| "text/plain".equals(mediaType);
967968
};
968969
operation.consumes = operation.consumes == null ? null : operation.consumes.stream()
969970
.filter(isSerializable)

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import com.squareup.moshi.adapter
7070
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
7171
protected const val XmlMediaType: String = "application/xml"
7272
protected const val OctetMediaType: String = "application/octet-stream"
73+
protected const val TextMediaType: String = "text/plain"
7374
7475
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKey: MutableMap<String, String> = mutableMapOf()
7576
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -211,8 +212,10 @@ import com.squareup.moshi.adapter
211212
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
212213
mediaType == OctetMediaType && content is ByteArray ->
213214
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
215+
mediaType == TextMediaType && content is String ->
216+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
214217
// TODO: this should be extended with other serializers
215-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
218+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
216219
}
217220

218221
{{#moshi}}
@@ -298,7 +301,8 @@ import com.squareup.moshi.adapter
298301
}}{{#kotlinx_serialization}}Serializer.kotlinxSerializationJson.decodeFromString<T>(bodyContent){{/kotlinx_serialization}}
299302
}
300303
mediaType == OctetMediaType -> body.bytes() as? T
301-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
304+
mediaType == TextMediaType -> body.string() as? T
305+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
302306
}
303307
}
304308

samples/client/others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
val apiKey: MutableMap<String, String> = mutableMapOf()
4647
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
@OptIn(ExperimentalStdlibApi::class)
@@ -243,7 +246,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
243246
Serializer.moshi.adapter<T>().fromJson(bodyContent)
244247
}
245248
mediaType == OctetMediaType -> body.bytes() as? T
246-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
249+
mediaType == TextMediaType -> body.string() as? T
250+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
247251
}
248252
}
249253

samples/client/petstore/kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
val apiKey: MutableMap<String, String> = mutableMapOf()
4647
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
@OptIn(ExperimentalStdlibApi::class)
@@ -243,7 +246,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
243246
Serializer.moshi.adapter<T>().fromJson(bodyContent)
244247
}
245248
mediaType == OctetMediaType -> body.bytes() as? T
246-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
249+
mediaType == TextMediaType -> body.string() as? T
250+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
247251
}
248252
}
249253

samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
val apiKey: MutableMap<String, String> = mutableMapOf()
4647
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
@OptIn(ExperimentalStdlibApi::class)
@@ -243,7 +246,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
243246
Serializer.moshi.adapter<T>().fromJson(bodyContent)
244247
}
245248
mediaType == OctetMediaType -> body.bytes() as? T
246-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
249+
mediaType == TextMediaType -> body.string() as? T
250+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
247251
}
248252
}
249253

samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
val apiKey: MutableMap<String, String> = mutableMapOf()
4647
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
@OptIn(ExperimentalStdlibApi::class)
@@ -243,7 +246,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
243246
Serializer.moshi.adapter<T>().fromJson(bodyContent)
244247
}
245248
mediaType == OctetMediaType -> body.bytes() as? T
246-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
249+
mediaType == TextMediaType -> body.string() as? T
250+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
247251
}
248252
}
249253

samples/client/petstore/kotlin-default-values-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
val apiKey: MutableMap<String, String> = mutableMapOf()
4647
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
@OptIn(ExperimentalStdlibApi::class)
@@ -243,7 +246,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
243246
Serializer.moshi.adapter<T>().fromJson(bodyContent)
244247
}
245248
mediaType == OctetMediaType -> body.bytes() as? T
246-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
249+
mediaType == TextMediaType -> body.string() as? T
250+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
247251
}
248252
}
249253

samples/client/petstore/kotlin-enum-default-value/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
val apiKey: MutableMap<String, String> = mutableMapOf()
4647
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
@OptIn(ExperimentalStdlibApi::class)
@@ -243,7 +246,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
243246
Serializer.moshi.adapter<T>().fromJson(bodyContent)
244247
}
245248
mediaType == OctetMediaType -> body.bytes() as? T
246-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
249+
mediaType == TextMediaType -> body.string() as? T
250+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
247251
}
248252
}
249253

samples/client/petstore/kotlin-explicit/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
public val apiKey: MutableMap<String, String> = mutableMapOf()
4647
public val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
@OptIn(ExperimentalStdlibApi::class)
@@ -243,7 +246,8 @@ public open class ApiClient(public val baseUrl: String, public val client: Call.
243246
Serializer.moshi.adapter<T>().fromJson(bodyContent)
244247
}
245248
mediaType == OctetMediaType -> body.bytes() as? T
246-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
249+
mediaType == TextMediaType -> body.string() as? T
250+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
247251
}
248252
}
249253

samples/client/petstore/kotlin-gson/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
4141
protected const val FormUrlEncMediaType: String = "application/x-www-form-urlencoded"
4242
protected const val XmlMediaType: String = "application/xml"
4343
protected const val OctetMediaType: String = "application/octet-stream"
44+
protected const val TextMediaType: String = "text/plain"
4445

4546
val apiKey: MutableMap<String, String> = mutableMapOf()
4647
val apiKeyPrefix: MutableMap<String, String> = mutableMapOf()
@@ -171,8 +172,10 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
171172
mediaType == XmlMediaType -> throw UnsupportedOperationException("xml not currently supported.")
172173
mediaType == OctetMediaType && content is ByteArray ->
173174
content.toRequestBody(OctetMediaType.toMediaTypeOrNull())
175+
mediaType == TextMediaType && content is String ->
176+
content.toRequestBody(TextMediaType.toMediaTypeOrNull())
174177
// TODO: this should be extended with other serializers
175-
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, byte body and File body.")
178+
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
176179
}
177180

178181
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JsonMediaType): T? {
@@ -242,7 +245,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
242245
Serializer.gson.fromJson(bodyContent, (object: TypeToken<T>(){}).getType())
243246
}
244247
mediaType == OctetMediaType -> body.bytes() as? T
245-
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body.")
248+
mediaType == TextMediaType -> body.string() as? T
249+
else -> throw UnsupportedOperationException("responseBody currently only supports JSON body, text body and byte body.")
246250
}
247251
}
248252

0 commit comments

Comments
 (0)