From 1dec9ebfb81c8d28cd677f10397ccad8173b63ca Mon Sep 17 00:00:00 2001 From: Alex Facciorusso Date: Tue, 20 Oct 2020 02:48:14 +0100 Subject: [PATCH] [kotlin][jvm-retrofit2] Better code for OkHttp client passed to Retrofit builder (#7556) * Better code for building Retrofit By removing the nullable `usedClient` passed to Retrofit, the code is compatible with newer Retrofit versions which don't allow null as client. * Updated Kotlin retrofit2 samples ApiClient --- .../jvm-retrofit2/infrastructure/ApiClient.kt.mustache | 5 ++--- .../org/openapitools/client/infrastructure/ApiClient.kt | 5 ++--- .../org/openapitools/client/infrastructure/ApiClient.kt | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache index 5026c3f0e07..d556f0e9869 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/infrastructure/ApiClient.kt.mustache @@ -287,8 +287,7 @@ import retrofit2.converter.moshi.MoshiConverterFactory } fun createService(serviceClass: Class): S { - var usedClient: OkHttpClient? = null - this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()} + val usedClient = this.okHttpClient ?: clientBuilder.build() return retrofitBuilder.client(usedClient).build().create(serviceClass) } @@ -313,4 +312,4 @@ import retrofit2.converter.moshi.MoshiConverterFactory System.getProperties().getProperty("{{packageName}}.baseUrl", "{{{basePath}}}") } } -} \ No newline at end of file +} diff --git a/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index e3962e0c0f2..f23b3308a41 100644 --- a/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-retrofit2-rx3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -175,8 +175,7 @@ class ApiClient( } fun createService(serviceClass: Class): S { - var usedClient: OkHttpClient? = null - this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()} + val usedClient = this.okHttpClient ?: clientBuilder.build() return retrofitBuilder.client(usedClient).build().create(serviceClass) } @@ -201,4 +200,4 @@ class ApiClient( System.getProperties().getProperty("org.openapitools.client.baseUrl", "http://petstore.swagger.io/v2") } } -} \ No newline at end of file +} diff --git a/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 8f822585a6e..c8078936eb5 100644 --- a/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-retrofit2/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -172,8 +172,7 @@ class ApiClient( } fun createService(serviceClass: Class): S { - var usedClient: OkHttpClient? = null - this.okHttpClient?.let { usedClient = it } ?: run {usedClient = clientBuilder.build()} + val usedClient = this.okHttpClient ?: clientBuilder.build() return retrofitBuilder.client(usedClient).build().create(serviceClass) } @@ -198,4 +197,4 @@ class ApiClient( System.getProperties().getProperty("org.openapitools.client.baseUrl", "http://petstore.swagger.io/v2") } } -} \ No newline at end of file +}