Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);

public static final String USE_RX_JAVA = "useRxJava";
public static final String USE_RX_JAVA2 = "useRxJava2";
public static final String DO_NOT_USE_RX = "doNotUseRx";
public static final String USE_PLAY24_WS = "usePlay24WS";
public static final String PARCELABLE_MODEL = "parcelableModel";

Expand All @@ -28,6 +30,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen

protected String gradleWrapperPackage = "gradle.wrapper";
protected boolean useRxJava = false;
protected boolean useRxJava2 = false;
protected boolean doNotUseRx = true; // backwards compatibility for swagger configs that specify neither rx1 nor rx2 (mustache does not allow for boolean operators so we need this extra field)
protected boolean usePlay24WS = false;
protected boolean parcelableModel = false;
protected boolean useBeanValidation = false;
Expand All @@ -43,6 +47,7 @@ public JavaClientCodegen() {
modelPackage = "io.swagger.client.model";

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(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson library."));
cliOptions.add(CliOption.newBoolean(USE_PLAY24_WS, "Use Play! 2.4 Async HTTP client (Play WS API)"));
cliOptions.add(CliOption.newBoolean(SUPPORT_JAVA6, "Whether to support Java6 with the Jersey1 library."));
Expand All @@ -54,7 +59,7 @@ public JavaClientCodegen() {
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.22.2. JSON processing: Jackson 2.7.0");
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2. Enable Parcelable modles on Android using '-DparcelableModel=true'");
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0). IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.");
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.2.0. JSON processing: Gson 2.6.1 (Retrofit 2.0.2). Enable the RxJava adapter using '-DuseRxJava=true'. (RxJava 1.1.3)");
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.2.0. JSON processing: Gson 2.6.1 (Retrofit 2.0.2). Enable the RxJava adapter using '-DuseRxJava[2]=true'. (RxJava 1.x or 2.x)");

CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
libraryOption.setEnum(supportedLibraries);
Expand Down Expand Up @@ -84,9 +89,17 @@ public String getHelp() {
public void processOpts() {
super.processOpts();

if (additionalProperties.containsKey(USE_RX_JAVA)) {
if (additionalProperties.containsKey(USE_RX_JAVA) && additionalProperties.containsKey(USE_RX_JAVA2)) {
LOGGER.warn("You specified both RxJava versions 1 and 2 but they are mutually exclusive. Defaulting to v2.");
} else if (additionalProperties.containsKey(USE_RX_JAVA)) {
this.setUseRxJava(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA).toString()));
}
if (additionalProperties.containsKey(USE_RX_JAVA2)) {
this.setUseRxJava2(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA2).toString()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both USE_RX_JAVA and USE_RX_JAVA2 are incorrectly set to true, maybe it would be a good idea to log a warning/error and only set one of them to true only.

if (additionalProperties.containsKey(DO_NOT_USE_RX)) {
this.setDoNotUseRx(Boolean.valueOf(additionalProperties.get(DO_NOT_USE_RX).toString()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these 3 lines can be removed as we no longer allow setting DO_NOT_USE_RX via option.

And after processing all the RX-related option, we will need to update "doNotUseRx" in additionalProperties so that it has a proper value (true or false)

I tested without any RX-related option and got the following compilation errors:

[ERROR] /private/var/tmp/java/useRxJava20/src/main/java/io/swagger/client/api/PetApi.java:[127,3] cannot find symbol
[ERROR] symbol:   class Observable
[ERROR] location: interface io.swagger.client.api.PetApi

if (additionalProperties.containsKey(USE_PLAY24_WS)) {
this.setUsePlay24WS(Boolean.valueOf(additionalProperties.get(USE_PLAY24_WS).toString()));
}
Expand Down Expand Up @@ -335,6 +348,16 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {

public void setUseRxJava(boolean useRxJava) {
this.useRxJava = useRxJava;
doNotUseRx = false;
}

public void setUseRxJava2(boolean useRxJava2) {
this.useRxJava2 = useRxJava2;
doNotUseRx = false;
}

public void setDoNotUseRx(boolean doNotUseRx) {
this.doNotUseRx = doNotUseRx;
}

public void setUsePlay24WS(boolean usePlay24WS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.time.format.DateTimeFormatter;
import retrofit2.Converter;
import retrofit2.Retrofit;
{{#useRxJava}}import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;{{/useRxJava}}
{{#useRxJava2}}import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;{{/useRxJava2}}
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

Expand Down Expand Up @@ -140,6 +141,7 @@ public class ApiClient {
.Builder()
.baseUrl(baseUrl)
{{#useRxJava}}.addCallAdapterFactory(RxJavaCallAdapterFactory.create()){{/useRxJava}}
{{#useRxJava2}}.addCallAdapterFactory(RxJava2CallAdapterFactory.create()){{/useRxJava2}}
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonCustomConverterFactory.create(gson));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package {{package}};
import {{invokerPackage}}.CollectionFormats.*;

{{#useRxJava}}import rx.Observable;{{/useRxJava}}
{{^useRxJava}}import retrofit2.Call;{{/useRxJava}}
{{#useRxJava2}}import io.reactivex.Observable;{{/useRxJava2}}
{{#doNotUseRx}}import retrofit2.Call;{{/doNotUseRx}}
import retrofit2.http.*;

import okhttp3.RequestBody;
Expand Down Expand Up @@ -46,7 +47,7 @@ public interface {{classname}} {
{{/prioritizedContentTypes}}
{{/formParams}}
@{{httpMethod}}("{{path}}")
{{^usePlay24WS}}{{#useRxJava}}Observable{{/useRxJava}}{{^useRxJava}}Call{{/useRxJava}}{{/usePlay24WS}}{{#usePlay24WS}}F.Promise<Response{{/usePlay24WS}}<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>{{#usePlay24WS}}>{{/usePlay24WS}} {{operationId}}({{^allParams}});{{/allParams}}
{{^usePlay24WS}}{{^doNotUseRx}}Observable{{/doNotUseRx}}{{#doNotUseRx}}Call{{/doNotUseRx}}{{/usePlay24WS}}{{#usePlay24WS}}F.Promise<Response{{/usePlay24WS}}<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Object{{/returnType}}>{{#usePlay24WS}}>{{/usePlay24WS}} {{operationId}}({{^allParams}});{{/allParams}}
{{#allParams}}{{>libraries/retrofit2/queryParams}}{{>libraries/retrofit2/pathParams}}{{>libraries/retrofit2/headerParams}}{{>libraries/retrofit2/bodyParams}}{{>libraries/retrofit2/formParams}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}
);{{/hasMore}}{{/allParams}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ ext {
{{#useRxJava}}
rx_java_version = "1.1.3"
{{/useRxJava}}
{{#useRxJava2}}
rx_java_version = "2.0.5"
{{/useRxJava2}}
{{^java8}}
jodatime_version = "2.9.3"
{{/java8}}
Expand All @@ -114,6 +117,10 @@ dependencies {
compile "com.squareup.retrofit2:adapter-rxjava:$retrofit_version"
compile "io.reactivex:rxjava:$rx_java_version"
{{/useRxJava}}
{{#useRxJava2}}
compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
compile "io.reactivex.rxjava2:rxjava:$rx_java_version"
{{/useRxJava2}}
compile "io.swagger:swagger-annotations:$swagger_annotations_version"
compile "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:$oltu_version"
{{^java8}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ lazy val root = (project in file(".")).
"com.squareup.retrofit2" % "adapter-rxjava" % "2.0.2" % "compile",
"io.reactivex" % "rxjava" % "1.1.3" % "compile",
{{/useRxJava}}
{{#useRxJava2}}
"com.jakewharton.retrofit" % "retrofit2-rxjava2-adapter" % "1.0.0" % "compile",
"io.reactivex.rxjava2" % "rxjava" % "2.0.5" % "compile",
{{/useRxJava2}}
"io.swagger" % "swagger-annotations" % "1.5.8" % "compile",
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1" % "compile",
{{^java8}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@
<version>${retrofit-version}</version>
</dependency>
{{/useRxJava}}
{{#useRxJava2}}
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>${rxjava-version}</version>
</dependency>
<dependency>
<groupId>com.jakewharton.retrofit</groupId>
<artifactId>com.jakewharton.retrofit</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be <artifactId>retrofit2-rxjava2-adapter</artifactId> instead.

<version>1.0.0</version>
</dependency>
{{/useRxJava2}}

{{#usePlay24WS}}
<!-- JSON processing: jackson -->
Expand Down Expand Up @@ -269,6 +281,9 @@
{{#useRxJava}}
<rxjava-version>1.1.6</rxjava-version>
{{/useRxJava}}
{{#useRxJava2}}
<rxjava-version>2.0.5</rxjava-version>
{{/useRxJava2}}
{{^java8}}
<jodatime-version>2.9.4</jodatime-version>
{{/java8}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public Map<String, String> createOptions() {
Map<String, String> options = new HashMap<String, String>(super.createOptions());
options.put(CodegenConstants.LIBRARY, DEFAULT_LIBRARY_VALUE);
options.put(JavaClientCodegen.USE_RX_JAVA, "false");
options.put(JavaClientCodegen.USE_RX_JAVA2, "false");
options.put(JavaClientCodegen.USE_PLAY24_WS, "false");
options.put(JavaClientCodegen.PARCELABLE_MODEL, "false");
options.put(JavaClientCodegen.SUPPORT_JAVA6, "false");
Expand Down