Skip to content

Commit 26f998b

Browse files
authored
[kotlin][client] define kotlin coroutines context (#9936)
* [kotlin][client] define coroutines context * [kotlin][client] update sample projects * [kotlin][client] add missing kotlinx coroutines dependency * [kotlin][client] update sample projects
1 parent 7573234 commit 26f998b

File tree

6 files changed

+60
-42
lines changed

6 files changed

+60
-42
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/build.gradle.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ test {
5353

5454
dependencies {
5555
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
56+
{{^doNotUseRxAndCoroutines}}
57+
{{#useCoroutines}}
58+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"
59+
{{/useCoroutines}}
60+
{{/doNotUseRxAndCoroutines}}
5661
{{#moshi}}
5762
{{^moshiCodeGen}}
5863
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ package {{apiPackage}}
44
{{#imports}}import {{import}}
55
{{/imports}}
66

7+
{{^doNotUseRxAndCoroutines}}
8+
{{#useCoroutines}}
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.withContext
11+
{{/useCoroutines}}
12+
{{/doNotUseRxAndCoroutines}}
713
import {{packageName}}.infrastructure.ApiClient
814
import {{packageName}}.infrastructure.ClientException
915
import {{packageName}}.infrastructure.ClientError
@@ -40,7 +46,7 @@ import {{packageName}}.infrastructure.toMultiValue
4046
{{#isDeprecated}}
4147
@Deprecated(message = "This operation is deprecated.")
4248
{{/isDeprecated}}
43-
{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
49+
{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) : {{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{^doNotUseRxAndCoroutines}}{{#useCoroutines}} = withContext(Dispatchers.IO){{/useCoroutines}}{{/doNotUseRxAndCoroutines}} {
4450
{{#isDeprecated}}
4551
@Suppress("DEPRECATION")
4652
{{/isDeprecated}}
@@ -50,7 +56,7 @@ import {{packageName}}.infrastructure.toMultiValue
5056
localVariableConfig
5157
)
5258

53-
return when (localVarResponse.responseType) {
59+
return{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}@withContext{{/useCoroutines}}{{/doNotUseRxAndCoroutines}} when (localVarResponse.responseType) {
5460
ResponseType.Success -> {{#returnType}}(localVarResponse as Success<*>).data as {{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
5561
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
5662
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")

samples/client/petstore/kotlin-jvm-okhttp4-coroutines/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ test {
2929

3030
dependencies {
3131
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
32+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"
3233
implementation "com.google.code.gson:gson:2.8.7"
3334
implementation "com.squareup.okhttp3:okhttp:4.9.1"
3435
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"

samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/PetApi.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ package org.openapitools.client.apis
1414
import org.openapitools.client.models.ApiResponse
1515
import org.openapitools.client.models.Pet
1616

17+
import kotlinx.coroutines.Dispatchers
18+
import kotlinx.coroutines.withContext
1719
import org.openapitools.client.infrastructure.ApiClient
1820
import org.openapitools.client.infrastructure.ClientException
1921
import org.openapitools.client.infrastructure.ClientError
@@ -44,14 +46,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
4446
* @throws ServerException If the API returns a server error response
4547
*/
4648
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
47-
suspend fun addPet(body: Pet) : Unit {
49+
suspend fun addPet(body: Pet) : Unit = withContext(Dispatchers.IO) {
4850
val localVariableConfig = addPetRequestConfig(body = body)
4951

5052
val localVarResponse = request<Pet, Unit>(
5153
localVariableConfig
5254
)
5355

54-
return when (localVarResponse.responseType) {
56+
return@withContext when (localVarResponse.responseType) {
5557
ResponseType.Success -> Unit
5658
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
5759
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -97,14 +99,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
9799
* @throws ServerException If the API returns a server error response
98100
*/
99101
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
100-
suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?) : Unit {
102+
suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?) : Unit = withContext(Dispatchers.IO) {
101103
val localVariableConfig = deletePetRequestConfig(petId = petId, apiKey = apiKey)
102104

103105
val localVarResponse = request<Unit, Unit>(
104106
localVariableConfig
105107
)
106108

107-
return when (localVarResponse.responseType) {
109+
return@withContext when (localVarResponse.responseType) {
108110
ResponseType.Success -> Unit
109111
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
110112
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -152,14 +154,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
152154
*/
153155
@Suppress("UNCHECKED_CAST")
154156
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
155-
suspend fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> {
157+
suspend fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> = withContext(Dispatchers.IO) {
156158
val localVariableConfig = findPetsByStatusRequestConfig(status = status)
157159

158160
val localVarResponse = request<Unit, kotlin.collections.List<Pet>>(
159161
localVariableConfig
160162
)
161163

162-
return when (localVarResponse.responseType) {
164+
return@withContext when (localVarResponse.responseType) {
163165
ResponseType.Success -> (localVarResponse as Success<*>).data as kotlin.collections.List<Pet>
164166
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
165167
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -209,15 +211,15 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
209211
@Suppress("UNCHECKED_CAST")
210212
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
211213
@Deprecated(message = "This operation is deprecated.")
212-
suspend fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> {
214+
suspend fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> = withContext(Dispatchers.IO) {
213215
@Suppress("DEPRECATION")
214216
val localVariableConfig = findPetsByTagsRequestConfig(tags = tags)
215217

216218
val localVarResponse = request<Unit, kotlin.collections.List<Pet>>(
217219
localVariableConfig
218220
)
219221

220-
return when (localVarResponse.responseType) {
222+
return@withContext when (localVarResponse.responseType) {
221223
ResponseType.Success -> (localVarResponse as Success<*>).data as kotlin.collections.List<Pet>
222224
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
223225
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -267,14 +269,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
267269
*/
268270
@Suppress("UNCHECKED_CAST")
269271
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
270-
suspend fun getPetById(petId: kotlin.Long) : Pet {
272+
suspend fun getPetById(petId: kotlin.Long) : Pet = withContext(Dispatchers.IO) {
271273
val localVariableConfig = getPetByIdRequestConfig(petId = petId)
272274

273275
val localVarResponse = request<Unit, Pet>(
274276
localVariableConfig
275277
)
276278

277-
return when (localVarResponse.responseType) {
279+
return@withContext when (localVarResponse.responseType) {
278280
ResponseType.Success -> (localVarResponse as Success<*>).data as Pet
279281
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
280282
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -319,14 +321,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
319321
* @throws ServerException If the API returns a server error response
320322
*/
321323
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
322-
suspend fun updatePet(body: Pet) : Unit {
324+
suspend fun updatePet(body: Pet) : Unit = withContext(Dispatchers.IO) {
323325
val localVariableConfig = updatePetRequestConfig(body = body)
324326

325327
val localVarResponse = request<Pet, Unit>(
326328
localVariableConfig
327329
)
328330

329-
return when (localVarResponse.responseType) {
331+
return@withContext when (localVarResponse.responseType) {
330332
ResponseType.Success -> Unit
331333
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
332334
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -373,14 +375,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
373375
* @throws ServerException If the API returns a server error response
374376
*/
375377
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
376-
suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : Unit {
378+
suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?) : Unit = withContext(Dispatchers.IO) {
377379
val localVariableConfig = updatePetWithFormRequestConfig(petId = petId, name = name, status = status)
378380

379381
val localVarResponse = request<Map<String, Any?>, Unit>(
380382
localVariableConfig
381383
)
382384

383-
return when (localVarResponse.responseType) {
385+
return@withContext when (localVarResponse.responseType) {
384386
ResponseType.Success -> Unit
385387
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
386388
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -430,14 +432,14 @@ class PetApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath) {
430432
*/
431433
@Suppress("UNCHECKED_CAST")
432434
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
433-
suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : ApiResponse {
435+
suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: java.io.File?) : ApiResponse = withContext(Dispatchers.IO) {
434436
val localVariableConfig = uploadFileRequestConfig(petId = petId, additionalMetadata = additionalMetadata, file = file)
435437

436438
val localVarResponse = request<Map<String, Any?>, ApiResponse>(
437439
localVariableConfig
438440
)
439441

440-
return when (localVarResponse.responseType) {
442+
return@withContext when (localVarResponse.responseType) {
441443
ResponseType.Success -> (localVarResponse as Success<*>).data as ApiResponse
442444
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
443445
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")

samples/client/petstore/kotlin-jvm-okhttp4-coroutines/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ package org.openapitools.client.apis
1313

1414
import org.openapitools.client.models.Order
1515

16+
import kotlinx.coroutines.Dispatchers
17+
import kotlinx.coroutines.withContext
1618
import org.openapitools.client.infrastructure.ApiClient
1719
import org.openapitools.client.infrastructure.ClientException
1820
import org.openapitools.client.infrastructure.ClientError
@@ -43,14 +45,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
4345
* @throws ServerException If the API returns a server error response
4446
*/
4547
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
46-
suspend fun deleteOrder(orderId: kotlin.String) : Unit {
48+
suspend fun deleteOrder(orderId: kotlin.String) : Unit = withContext(Dispatchers.IO) {
4749
val localVariableConfig = deleteOrderRequestConfig(orderId = orderId)
4850

4951
val localVarResponse = request<Unit, Unit>(
5052
localVariableConfig
5153
)
5254

53-
return when (localVarResponse.responseType) {
55+
return@withContext when (localVarResponse.responseType) {
5456
ResponseType.Success -> Unit
5557
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
5658
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -95,14 +97,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
9597
*/
9698
@Suppress("UNCHECKED_CAST")
9799
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
98-
suspend fun getInventory() : kotlin.collections.Map<kotlin.String, kotlin.Int> {
100+
suspend fun getInventory() : kotlin.collections.Map<kotlin.String, kotlin.Int> = withContext(Dispatchers.IO) {
99101
val localVariableConfig = getInventoryRequestConfig()
100102

101103
val localVarResponse = request<Unit, kotlin.collections.Map<kotlin.String, kotlin.Int>>(
102104
localVariableConfig
103105
)
104106

105-
return when (localVarResponse.responseType) {
107+
return@withContext when (localVarResponse.responseType) {
106108
ResponseType.Success -> (localVarResponse as Success<*>).data as kotlin.collections.Map<kotlin.String, kotlin.Int>
107109
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
108110
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -147,14 +149,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
147149
*/
148150
@Suppress("UNCHECKED_CAST")
149151
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
150-
suspend fun getOrderById(orderId: kotlin.Long) : Order {
152+
suspend fun getOrderById(orderId: kotlin.Long) : Order = withContext(Dispatchers.IO) {
151153
val localVariableConfig = getOrderByIdRequestConfig(orderId = orderId)
152154

153155
val localVarResponse = request<Unit, Order>(
154156
localVariableConfig
155157
)
156158

157-
return when (localVarResponse.responseType) {
159+
return@withContext when (localVarResponse.responseType) {
158160
ResponseType.Success -> (localVarResponse as Success<*>).data as Order
159161
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
160162
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
@@ -200,14 +202,14 @@ class StoreApi(basePath: kotlin.String = defaultBasePath) : ApiClient(basePath)
200202
*/
201203
@Suppress("UNCHECKED_CAST")
202204
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
203-
suspend fun placeOrder(body: Order) : Order {
205+
suspend fun placeOrder(body: Order) : Order = withContext(Dispatchers.IO) {
204206
val localVariableConfig = placeOrderRequestConfig(body = body)
205207

206208
val localVarResponse = request<Order, Order>(
207209
localVariableConfig
208210
)
209211

210-
return when (localVarResponse.responseType) {
212+
return@withContext when (localVarResponse.responseType) {
211213
ResponseType.Success -> (localVarResponse as Success<*>).data as Order
212214
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
213215
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")

0 commit comments

Comments
 (0)