Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bin/configs/kotlin-jvm-retrofit2-rx3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-retrofit2-rx3
library: jvm-retrofit2
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-petstore-retrofit2-rx3
useRxJava3: "true"
1 change: 1 addition & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sidebar_label: kotlin
|useCoroutines|Whether to use the Coroutines adapter with the retrofit2 library.| |false|
|useRxJava|Whether to use the RxJava adapter with the retrofit2 library.| |false|
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library.| |false|
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library.| |false|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {

public static final String USE_RX_JAVA = "useRxJava";
public static final String USE_RX_JAVA2 = "useRxJava2";
public static final String USE_RX_JAVA3 = "useRxJava3";
public static final String USE_COROUTINES = "useCoroutines";
public static final String DO_NOT_USE_RX_AND_COROUTINES = "doNotUseRxAndCoroutines";

Expand All @@ -65,6 +66,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
protected String collectionType = CollectionType.LIST.value;
protected boolean useRxJava = false;
protected boolean useRxJava2 = false;
protected boolean useRxJava3 = false;
protected boolean useCoroutines = false;
// backwards compatibility for openapi configs that specify neither rx1 nor rx2
// (mustache does not allow for boolean operators so we need this extra field)
Expand Down Expand Up @@ -198,6 +200,7 @@ public KotlinClientCodegen() {

cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA2, "Whether to use the RxJava2 adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA3, "Whether to use the RxJava3 adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_COROUTINES, "Whether to use the Coroutines adapter with the retrofit2 library."));
}

Expand All @@ -216,6 +219,7 @@ public String getHelp() {
public void setUseRxJava(boolean useRxJava) {
if (useRxJava) {
this.useRxJava2 = false;
this.useRxJava3 = false;
this.doNotUseRxAndCoroutines = false;
this.useCoroutines = false;
}
Expand All @@ -225,16 +229,28 @@ public void setUseRxJava(boolean useRxJava) {
public void setUseRxJava2(boolean useRxJava2) {
if (useRxJava2) {
this.useRxJava = false;
this.useRxJava3 = false;
this.doNotUseRxAndCoroutines = false;
this.useCoroutines = false;
}
this.useRxJava2 = useRxJava2;
}

public void setUseRxJava3(boolean useRxJava3) {
if (useRxJava3) {
this.useRxJava = false;
this.useRxJava2 = false;
this.doNotUseRxAndCoroutines = false;
this.useCoroutines = false;
}
this.useRxJava3 = useRxJava3;
}

public void setDoNotUseRxAndCoroutines(boolean doNotUseRxAndCoroutines) {
if (doNotUseRxAndCoroutines) {
this.useRxJava = false;
this.useRxJava2 = false;
this.useRxJava3 = false;
this.useCoroutines = false;
}
this.doNotUseRxAndCoroutines = doNotUseRxAndCoroutines;
Expand All @@ -244,6 +260,7 @@ public void setUseCoroutines(boolean useCoroutines) {
if (useCoroutines) {
this.useRxJava = false;
this.useRxJava2 = false;
this.useRxJava3 = false;
this.doNotUseRxAndCoroutines = false;
}
this.useCoroutines = useCoroutines;
Expand Down Expand Up @@ -273,6 +290,7 @@ public void processOpts() {

boolean hasRx = additionalProperties.containsKey(USE_RX_JAVA);
boolean hasRx2 = additionalProperties.containsKey(USE_RX_JAVA2);
boolean hasRx3 = additionalProperties.containsKey(USE_RX_JAVA3);
boolean hasCoroutines = additionalProperties.containsKey(USE_COROUTINES);
int optionCount = 0;
if (hasRx) {
Expand All @@ -281,23 +299,28 @@ public void processOpts() {
if (hasRx2) {
optionCount++;
}
if (hasRx3) {
optionCount++;
}
if (hasCoroutines) {
optionCount++;
}
boolean hasConflict = optionCount > 1;

// RxJava & Coroutines
if (hasConflict) {
LOGGER.warn("You specified both RxJava versions 1 and 2 or Coroutines together, please choose one them.");
LOGGER.warn("You specified RxJava versions 1 and 2 and 3 or Coroutines together, please choose one of them.");
} else if (hasRx) {
this.setUseRxJava(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA).toString()));
} else if (hasRx2) {
this.setUseRxJava2(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA2).toString()));
} else if (hasRx3) {
this.setUseRxJava3(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA3).toString()));
} else if (hasCoroutines) {
this.setUseCoroutines(Boolean.valueOf(additionalProperties.get(USE_COROUTINES).toString()));
}

if (!hasRx && !hasRx2 && !hasCoroutines) {
if (!hasRx && !hasRx2 && !hasRx3 && !hasCoroutines) {
setDoNotUseRxAndCoroutines(true);
additionalProperties.put(DO_NOT_USE_RX_AND_COROUTINES, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ public class ApiClient {
private OkHttpClient.Builder okBuilder;
private Retrofit.Builder adapterBuilder;
private JSON json;
private OkHttpClient okHttpClient;

public ApiClient() {
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okBuilder = new OkHttpClient.Builder();
}

public ApiClient(OkHttpClient client){
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okHttpClient = client;
}

public ApiClient(String[] authNames) {
Expand Down Expand Up @@ -141,7 +149,6 @@ public class ApiClient {
{{/hasOAuthMethods}}
public void createDefaultAdapter() {
json = new JSON();
okBuilder = new OkHttpClient.Builder();

String baseUrl = "{{{basePath}}}";
if (!baseUrl.endsWith("/"))
Expand All @@ -164,10 +171,11 @@ public class ApiClient {
}

public <S> S createService(Class<S> serviceClass) {
return adapterBuilder
.client(okBuilder.build())
.build()
.create(serviceClass);
if (okHttpClient != null) {
return adapterBuilder.client(okHttpClient).build().create(serviceClass);
} else {
return adapterBuilder.client(okBuilder.build()).build().create(serviceClass);
}
}

public ApiClient setDateFormat(DateFormat dateFormat) {
Expand Down Expand Up @@ -357,7 +365,11 @@ public class ApiClient {
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
}
apiAuthorizations.put(authName, authorization);
if(okBuilder == null){
throw new RuntimeException("The ApiClient was created with a built OkHttpClient so it's not possible to add an authorization interceptor to it");
}
okBuilder.addInterceptor(authorization);

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ buildscript {
{{#useRxJava2}}
ext.rxJava2Version = '2.2.17'
{{/useRxJava2}}
{{#useRxJava3}}
ext.rxJava3Version = '3.0.4'
{{/useRxJava3}}

repositories {
maven { url "https://repo1.maven.org/maven2" }
Expand Down Expand Up @@ -98,6 +101,10 @@ dependencies {
compile "io.reactivex.rxjava2:rxjava:$rxJava2Version"
compile "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"
{{/useRxJava2}}
{{#useRxJava3}}
compile "io.reactivex.rxjava3:rxjava:$rxJava3Version"
compile "com.github.akarnokd:rxjava3-retrofit-adapter:3.0.0"
{{/useRxJava3}}
compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
{{#gson}}
compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ import rx.Observable
{{#useRxJava2}}
import io.reactivex.Single
{{/useRxJava2}}
{{#useRxJava3}}
import io.reactivex.rxjava3.core.Single;
{{/useRxJava3}}
{{^returnType}}
{{#useRxJava2}}
import io.reactivex.Completable
{{/useRxJava2}}
{{#useRxJava3}}
import io.reactivex.rxjava3.core.Completable;
{{/useRxJava3}}
{{/returnType}}
{{/doNotUseRxAndCoroutines}}

Expand Down Expand Up @@ -66,7 +72,7 @@ interface {{classname}} {
{{/prioritizedContentTypes}}
{{/formParams}}
@{{httpMethod}}("{{{path}}}")
{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{^allParams}}){{/allParams}}{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/allParams}}: {{^doNotUseRxAndCoroutines}}{{#useRxJava}}Observable<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/useRxJava}}{{#useRxJava2}}{{#returnType}}Single<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}>{{/returnType}}{{^returnType}}Completable{{/returnType}}{{/useRxJava2}}{{#useCoroutines}}Response<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/useCoroutines}}{{/doNotUseRxAndCoroutines}}{{#doNotUseRxAndCoroutines}}Call<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/doNotUseRxAndCoroutines}}
{{^doNotUseRxAndCoroutines}}{{#useCoroutines}}suspend {{/useCoroutines}}{{/doNotUseRxAndCoroutines}}fun {{operationId}}({{^allParams}}){{/allParams}}{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/allParams}}: {{^doNotUseRxAndCoroutines}}{{#useRxJava}}Observable<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/useRxJava}}{{#useRxJava2}}{{#returnType}}Single<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}>{{/returnType}}{{^returnType}}Completable{{/returnType}}{{/useRxJava2}}{{#useRxJava3}}{{#returnType}}Single<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{{returnType}}}{{/isResponseFile}}>{{/returnType}}{{^returnType}}Completable{{/returnType}}{{/useRxJava3}}{{#useCoroutines}}Response<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/useCoroutines}}{{/doNotUseRxAndCoroutines}}{{#doNotUseRxAndCoroutines}}Call<{{#isResponseFile}}ResponseBody{{/isResponseFile}}{{^isResponseFile}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}{{/isResponseFile}}>{{/doNotUseRxAndCoroutines}}

{{/operation}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
{{#useRxJava2}}
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
{{/useRxJava2}}
{{#useRxJava3}}
import hu.akarnokd.rxjava3.retrofit.RxJava3CallAdapterFactory;
{{/useRxJava3}}
{{#gson}}
import com.google.gson.Gson
import com.google.gson.GsonBuilder
Expand Down Expand Up @@ -62,7 +65,9 @@ import retrofit2.converter.moshi.MoshiConverterFactory
{{/useRxJava}}
{{#useRxJava2}}
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
{{/useRxJava2}}
{{/useRxJava2}}{{#useRxJava3}}
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
{{/useRxJava3}}
{{#moshi}}
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
{{/moshi}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ public class ApiClient {
private OkHttpClient.Builder okBuilder;
private Retrofit.Builder adapterBuilder;
private JSON json;
private OkHttpClient okHttpClient;

public ApiClient() {
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okBuilder = new OkHttpClient.Builder();
}

public ApiClient(OkHttpClient client){
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okHttpClient = client;
}

public ApiClient(String[] authNames) {
Expand Down Expand Up @@ -114,7 +122,6 @@ public ApiClient(String authName, String clientId, String secret, String usernam

public void createDefaultAdapter() {
json = new JSON();
okBuilder = new OkHttpClient.Builder();

String baseUrl = "http://petstore.swagger.io:80/v2";
if (!baseUrl.endsWith("/"))
Expand All @@ -128,10 +135,11 @@ public void createDefaultAdapter() {
}

public <S> S createService(Class<S> serviceClass) {
return adapterBuilder
.client(okBuilder.build())
.build()
.create(serviceClass);
if (okHttpClient != null) {
return adapterBuilder.client(okHttpClient).build().create(serviceClass);
} else {
return adapterBuilder.client(okBuilder.build()).build().create(serviceClass);
}
}

public ApiClient setDateFormat(DateFormat dateFormat) {
Expand Down Expand Up @@ -303,7 +311,11 @@ public ApiClient addAuthorization(String authName, Interceptor authorization) {
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
}
apiAuthorizations.put(authName, authorization);
if(okBuilder == null){
throw new RuntimeException("The ApiClient was created with a built OkHttpClient so it's not possible to add an authorization interceptor to it");
}
okBuilder.addInterceptor(authorization);

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ public class ApiClient {
private OkHttpClient.Builder okBuilder;
private Retrofit.Builder adapterBuilder;
private JSON json;
private OkHttpClient okHttpClient;

public ApiClient() {
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okBuilder = new OkHttpClient.Builder();
}

public ApiClient(OkHttpClient client){
apiAuthorizations = new LinkedHashMap<String, Interceptor>();
createDefaultAdapter();
okHttpClient = client;
}

public ApiClient(String[] authNames) {
Expand Down Expand Up @@ -115,7 +123,6 @@ public ApiClient(String authName, String clientId, String secret, String usernam

public void createDefaultAdapter() {
json = new JSON();
okBuilder = new OkHttpClient.Builder();

String baseUrl = "http://petstore.swagger.io:80/v2";
if (!baseUrl.endsWith("/"))
Expand All @@ -130,10 +137,11 @@ public void createDefaultAdapter() {
}

public <S> S createService(Class<S> serviceClass) {
return adapterBuilder
.client(okBuilder.build())
.build()
.create(serviceClass);
if (okHttpClient != null) {
return adapterBuilder.client(okHttpClient).build().create(serviceClass);
} else {
return adapterBuilder.client(okBuilder.build()).build().create(serviceClass);
}
}

public ApiClient setDateFormat(DateFormat dateFormat) {
Expand Down Expand Up @@ -305,7 +313,11 @@ public ApiClient addAuthorization(String authName, Interceptor authorization) {
throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations");
}
apiAuthorizations.put(authName, authorization);
if(okBuilder == null){
throw new RuntimeException("The ApiClient was created with a built OkHttpClient so it's not possible to add an authorization interceptor to it");
}
okBuilder.addInterceptor(authorization);

return this;
}

Expand Down
Loading