Skip to content

Commit 7ddd693

Browse files
authored
Improve consumption of primitive types as body (#23257)
* Adapt textual content body to support primitive types. * Regenerate source codes. * Move `content` variable into `when`. * Accept also `Char` as a possible text body.
1 parent 49e2031 commit 7ddd693

28 files changed

Lines changed: 252 additions & 56 deletions

File tree

  • modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure
  • samples/client
    • echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure
    • others
      • kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-jvm-okhttp-path-comments/src/main/kotlin/org/openapitools/client/infrastructure
    • petstore
      • kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-allOf-discriminator/src/main/kotlin/org/openapitools/client/infrastructure
      • kotlin-array-integer-enum/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

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,15 @@ import com.squareup.moshi.adapter
323323
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
324324
}
325325
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
326-
mediaType == TEXT_MEDIA_TYPE && content is String ->
327-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
326+
mediaType == TEXT_MEDIA_TYPE -> {
327+
val textualContent = when (content) {
328+
is Char, is CharSequence -> content.toString()
329+
is Number -> content.toString()
330+
is Boolean -> content.toString()
331+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
332+
}
333+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
334+
}
328335
// TODO: this should be extended with other serializers
329336
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
330337
}

samples/client/echo_api/kotlin-jvm-okhttp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

samples/client/others/kotlin-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

samples/client/others/kotlin-jvm-okhttp-non-ascii-headers/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

samples/client/petstore/kotlin-allOf-discriminator-kotlinx-serialization/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
273273
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
274274
}
275275
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
276-
mediaType == TEXT_MEDIA_TYPE && content is String ->
277-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
276+
mediaType == TEXT_MEDIA_TYPE -> {
277+
val textualContent = when (content) {
278+
is Char, is CharSequence -> content.toString()
279+
is Number -> content.toString()
280+
is Boolean -> content.toString()
281+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
282+
}
283+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
284+
}
278285
// TODO: this should be extended with other serializers
279286
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
280287
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

samples/client/petstore/kotlin-array-integer-enum/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,15 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
270270
.toRequestBody((mediaType ?: JSON_MEDIA_TYPE).toMediaTypeOrNull())
271271
}
272272
mediaType == XML_MEDIA_TYPE -> throw UnsupportedOperationException("xml not currently supported.")
273-
mediaType == TEXT_MEDIA_TYPE && content is String ->
274-
content.toRequestBody(TEXT_MEDIA_TYPE.toMediaTypeOrNull())
273+
mediaType == TEXT_MEDIA_TYPE -> {
274+
val textualContent = when (content) {
275+
is Char, is CharSequence -> content.toString()
276+
is Number -> content.toString()
277+
is Boolean -> content.toString()
278+
else -> throw UnsupportedOperationException("requestBody currently only supports text body containing primitive types: characters, numbers, or booleans.")
279+
}
280+
textualContent.toRequestBody(mediaType.toMediaTypeOrNull())
281+
}
275282
// TODO: this should be extended with other serializers
276283
else -> throw UnsupportedOperationException("requestBody currently only supports JSON body, text body, byte body and File body.")
277284
}

0 commit comments

Comments
 (0)