Skip to content

Commit 72fe970

Browse files
authored
fix [Kotlin]: handle nullable response.body in jvm-okhttp ApiClient template (#23515)
Signed-off-by: Oyvind Mo <oymo76@gmail.com>
1 parent cf4ced7 commit 72fe970

File tree

28 files changed

+28
-28
lines changed
  • 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

28 files changed

+28
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ import com.squareup.moshi.adapter
340340
@OptIn(ExperimentalStdlibApi::class)
341341
{{/moshi}}
342342
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
343-
val body = response.body
343+
val body = response.body ?: return null
344344
345345
if (T::class.java == Unit::class.java) {
346346
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
287287
}
288288

289289
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
290-
val body = response.body
290+
val body = response.body ?: return null
291291

292292
if (T::class.java == Unit::class.java) {
293293
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
285285

286286
@OptIn(ExperimentalStdlibApi::class)
287287
protected inline fun <reified T: Any?> responseBody(response: Response, mediaType: String? = JSON_MEDIA_TYPE): T? {
288-
val body = response.body
288+
val body = response.body ?: return null
289289

290290
if (T::class.java == Unit::class.java) {
291291
// No need to parse the body when we're not interested in the body

0 commit comments

Comments
 (0)