Skip to content

Commit 1440388

Browse files
committed
[kotlin] Fix a leak in the ApiClient
1 parent 640ef9d commit 1440388

File tree

21 files changed

+609
-567
lines changed
  • modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure
  • samples/client
    • others/kotlin-jvm-okhttp-parameter-tests/src/main/kotlin/org/openapitools/client/infrastructure
    • petstore
      • kotlin/src/main/kotlin/org/openapitools/client/infrastructure
      • 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-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

21 files changed

+609
-567
lines changed

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -413,33 +413,35 @@ import com.squareup.moshi.adapter
413413

414414
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
415415
@Suppress("UNNECESSARY_SAFE_CALL")
416-
return when {
417-
response.isRedirect -> Redirection(
418-
response.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
419-
response.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
420-
)
421-
response.isInformational -> Informational(
422-
response.message{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
423-
response.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
424-
response.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
425-
)
426-
response.isSuccessful -> Success(
427-
responseBody(response.body{{#jvm-okhttp3}}(){{/jvm-okhttp3}}, accept),
428-
response.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
429-
response.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
430-
)
431-
response.isClientError -> ClientError(
432-
response.message{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
433-
response.body{{#jvm-okhttp3}}(){{/jvm-okhttp3}}?.string(),
434-
response.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
435-
response.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
436-
)
437-
else -> ServerError(
438-
response.message{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
439-
response.body{{#jvm-okhttp3}}(){{/jvm-okhttp3}}?.string(),
440-
response.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
441-
response.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
442-
)
416+
return response.use {
417+
when {
418+
it.isRedirect -> Redirection(
419+
it.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
420+
it.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
421+
)
422+
it.isInformational -> Informational(
423+
it.message{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
424+
it.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
425+
it.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
426+
)
427+
it.isSuccessful -> Success(
428+
responseBody(it.body{{#jvm-okhttp3}}(){{/jvm-okhttp3}}, accept),
429+
it.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
430+
it.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
431+
)
432+
it.isClientError -> ClientError(
433+
it.message{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
434+
it.body{{#jvm-okhttp3}}(){{/jvm-okhttp3}}?.string(),
435+
it.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
436+
it.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
437+
)
438+
else -> ServerError(
439+
it.message{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
440+
it.body{{#jvm-okhttp3}}(){{/jvm-okhttp3}}?.string(),
441+
it.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}},
442+
it.headers{{#jvm-okhttp3}}(){{/jvm-okhttp3}}.toMultimap()
443+
)
444+
}
443445
}
444446
}
445447

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,35 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
202202

203203
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
204204
@Suppress("UNNECESSARY_SAFE_CALL")
205-
return when {
206-
response.isRedirect -> Redirection(
207-
response.code,
208-
response.headers.toMultimap()
209-
)
210-
response.isInformational -> Informational(
211-
response.message,
212-
response.code,
213-
response.headers.toMultimap()
214-
)
215-
response.isSuccessful -> Success(
216-
responseBody(response.body, accept),
217-
response.code,
218-
response.headers.toMultimap()
219-
)
220-
response.isClientError -> ClientError(
221-
response.message,
222-
response.body?.string(),
223-
response.code,
224-
response.headers.toMultimap()
225-
)
226-
else -> ServerError(
227-
response.message,
228-
response.body?.string(),
229-
response.code,
230-
response.headers.toMultimap()
231-
)
205+
return response.use {
206+
when {
207+
it.isRedirect -> Redirection(
208+
it.code,
209+
it.headers.toMultimap()
210+
)
211+
it.isInformational -> Informational(
212+
it.message,
213+
it.code,
214+
it.headers.toMultimap()
215+
)
216+
it.isSuccessful -> Success(
217+
responseBody(it.body, accept),
218+
it.code,
219+
it.headers.toMultimap()
220+
)
221+
it.isClientError -> ClientError(
222+
it.message,
223+
it.body?.string(),
224+
it.code,
225+
it.headers.toMultimap()
226+
)
227+
else -> ServerError(
228+
it.message,
229+
it.body?.string(),
230+
it.code,
231+
it.headers.toMultimap()
232+
)
233+
}
232234
}
233235
}
234236

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,35 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
202202

203203
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
204204
@Suppress("UNNECESSARY_SAFE_CALL")
205-
return when {
206-
response.isRedirect -> Redirection(
207-
response.code,
208-
response.headers.toMultimap()
209-
)
210-
response.isInformational -> Informational(
211-
response.message,
212-
response.code,
213-
response.headers.toMultimap()
214-
)
215-
response.isSuccessful -> Success(
216-
responseBody(response.body, accept),
217-
response.code,
218-
response.headers.toMultimap()
219-
)
220-
response.isClientError -> ClientError(
221-
response.message,
222-
response.body?.string(),
223-
response.code,
224-
response.headers.toMultimap()
225-
)
226-
else -> ServerError(
227-
response.message,
228-
response.body?.string(),
229-
response.code,
230-
response.headers.toMultimap()
231-
)
205+
return response.use {
206+
when {
207+
it.isRedirect -> Redirection(
208+
it.code,
209+
it.headers.toMultimap()
210+
)
211+
it.isInformational -> Informational(
212+
it.message,
213+
it.code,
214+
it.headers.toMultimap()
215+
)
216+
it.isSuccessful -> Success(
217+
responseBody(it.body, accept),
218+
it.code,
219+
it.headers.toMultimap()
220+
)
221+
it.isClientError -> ClientError(
222+
it.message,
223+
it.body?.string(),
224+
it.code,
225+
it.headers.toMultimap()
226+
)
227+
else -> ServerError(
228+
it.message,
229+
it.body?.string(),
230+
it.code,
231+
it.headers.toMultimap()
232+
)
233+
}
232234
}
233235
}
234236

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,35 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
202202

203203
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
204204
@Suppress("UNNECESSARY_SAFE_CALL")
205-
return when {
206-
response.isRedirect -> Redirection(
207-
response.code,
208-
response.headers.toMultimap()
209-
)
210-
response.isInformational -> Informational(
211-
response.message,
212-
response.code,
213-
response.headers.toMultimap()
214-
)
215-
response.isSuccessful -> Success(
216-
responseBody(response.body, accept),
217-
response.code,
218-
response.headers.toMultimap()
219-
)
220-
response.isClientError -> ClientError(
221-
response.message,
222-
response.body?.string(),
223-
response.code,
224-
response.headers.toMultimap()
225-
)
226-
else -> ServerError(
227-
response.message,
228-
response.body?.string(),
229-
response.code,
230-
response.headers.toMultimap()
231-
)
205+
return response.use {
206+
when {
207+
it.isRedirect -> Redirection(
208+
it.code,
209+
it.headers.toMultimap()
210+
)
211+
it.isInformational -> Informational(
212+
it.message,
213+
it.code,
214+
it.headers.toMultimap()
215+
)
216+
it.isSuccessful -> Success(
217+
responseBody(it.body, accept),
218+
it.code,
219+
it.headers.toMultimap()
220+
)
221+
it.isClientError -> ClientError(
222+
it.message,
223+
it.body?.string(),
224+
it.code,
225+
it.headers.toMultimap()
226+
)
227+
else -> ServerError(
228+
it.message,
229+
it.body?.string(),
230+
it.code,
231+
it.headers.toMultimap()
232+
)
233+
}
232234
}
233235
}
234236

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,35 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
202202

203203
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
204204
@Suppress("UNNECESSARY_SAFE_CALL")
205-
return when {
206-
response.isRedirect -> Redirection(
207-
response.code,
208-
response.headers.toMultimap()
209-
)
210-
response.isInformational -> Informational(
211-
response.message,
212-
response.code,
213-
response.headers.toMultimap()
214-
)
215-
response.isSuccessful -> Success(
216-
responseBody(response.body, accept),
217-
response.code,
218-
response.headers.toMultimap()
219-
)
220-
response.isClientError -> ClientError(
221-
response.message,
222-
response.body?.string(),
223-
response.code,
224-
response.headers.toMultimap()
225-
)
226-
else -> ServerError(
227-
response.message,
228-
response.body?.string(),
229-
response.code,
230-
response.headers.toMultimap()
231-
)
205+
return response.use {
206+
when {
207+
it.isRedirect -> Redirection(
208+
it.code,
209+
it.headers.toMultimap()
210+
)
211+
it.isInformational -> Informational(
212+
it.message,
213+
it.code,
214+
it.headers.toMultimap()
215+
)
216+
it.isSuccessful -> Success(
217+
responseBody(it.body, accept),
218+
it.code,
219+
it.headers.toMultimap()
220+
)
221+
it.isClientError -> ClientError(
222+
it.message,
223+
it.body?.string(),
224+
it.code,
225+
it.headers.toMultimap()
226+
)
227+
else -> ServerError(
228+
it.message,
229+
it.body?.string(),
230+
it.code,
231+
it.headers.toMultimap()
232+
)
233+
}
232234
}
233235
}
234236

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,35 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
202202

203203
// TODO: handle specific mapping types. e.g. Map<int, Class<?>>
204204
@Suppress("UNNECESSARY_SAFE_CALL")
205-
return when {
206-
response.isRedirect -> Redirection(
207-
response.code,
208-
response.headers.toMultimap()
209-
)
210-
response.isInformational -> Informational(
211-
response.message,
212-
response.code,
213-
response.headers.toMultimap()
214-
)
215-
response.isSuccessful -> Success(
216-
responseBody(response.body, accept),
217-
response.code,
218-
response.headers.toMultimap()
219-
)
220-
response.isClientError -> ClientError(
221-
response.message,
222-
response.body?.string(),
223-
response.code,
224-
response.headers.toMultimap()
225-
)
226-
else -> ServerError(
227-
response.message,
228-
response.body?.string(),
229-
response.code,
230-
response.headers.toMultimap()
231-
)
205+
return response.use {
206+
when {
207+
it.isRedirect -> Redirection(
208+
it.code,
209+
it.headers.toMultimap()
210+
)
211+
it.isInformational -> Informational(
212+
it.message,
213+
it.code,
214+
it.headers.toMultimap()
215+
)
216+
it.isSuccessful -> Success(
217+
responseBody(it.body, accept),
218+
it.code,
219+
it.headers.toMultimap()
220+
)
221+
it.isClientError -> ClientError(
222+
it.message,
223+
it.body?.string(),
224+
it.code,
225+
it.headers.toMultimap()
226+
)
227+
else -> ServerError(
228+
it.message,
229+
it.body?.string(),
230+
it.code,
231+
it.headers.toMultimap()
232+
)
233+
}
232234
}
233235
}
234236

0 commit comments

Comments
 (0)