Skip to content

Commit

Permalink
Merge pull request #828 from swagger-api/codegen-issue-10312
Browse files Browse the repository at this point in the history
added java rx version 3 support
  • Loading branch information
HugoMario authored Dec 23, 2020
2 parents c7db921 + 8b57a33 commit 42f6021
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValida

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 DO_NOT_USE_RX = "doNotUseRx";
public static final String USE_PLAY_WS = "usePlayWS";
public static final String PLAY_VERSION = "playVersion";
Expand All @@ -55,6 +56,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValida
protected String gradleWrapperPackage = "gradle.wrapper";
protected boolean useRxJava = false;
protected boolean useRxJava2 = false;
protected boolean useRxJava3 = 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 usePlayWS = false;
protected String playVersion = PLAY_25;
Expand All @@ -76,6 +78,7 @@ public JavaClientCodegen() {

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(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson library."));
cliOptions.add(CliOption.newBoolean(USE_PLAY_WS, "Use Play! Async HTTP client (Play WS API)"));
cliOptions.add(CliOption.newString(PLAY_VERSION, "Version of Play! Framework (possible values \"play24\", \"play25\")"));
Expand Down Expand Up @@ -122,15 +125,17 @@ public String getHelp() {
public void processOpts() {
super.processOpts();

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)) {
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()));
}
if (!useRxJava && !useRxJava2) {
if (additionalProperties.containsKey(USE_RX_JAVA3)) {
this.setUseRxJava3(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA3).toString()));
}

if (!useRxJava && !useRxJava2 && !useRxJava3) {
additionalProperties.put(DO_NOT_USE_RX, true);
}
if (additionalProperties.containsKey(USE_PLAY_WS)) {
Expand Down Expand Up @@ -553,6 +558,11 @@ public void setUseRxJava2(boolean useRxJava2) {
doNotUseRx = false;
}

public void setUseRxJava3(boolean useRxJava3) {
this.useRxJava3 = useRxJava3;
doNotUseRx = false;
}

public void setDoNotUseRx(boolean doNotUseRx) {
this.doNotUseRx = doNotUseRx;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
{{#useRxJava2}}
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
{{/useRxJava2}}
{{#useRxJava3}}
import hu.akarnokd.rxjava3.retrofit.RxJava3CallAdapterFactory;
{{/useRxJava3}}
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import {{invokerPackage}}.auth.HttpBasicAuth;
Expand Down Expand Up @@ -142,7 +145,9 @@ public class ApiClient {
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
{{/useRxJava}}{{#useRxJava2}}
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
{{/useRxJava2}}
{{/useRxJava2}}{{#useRxJava3}}
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
{{/useRxJava3}}
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonCustomConverterFactory.create(json.getGson()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import rx.Observable;
{{#useRxJava2}}
import io.reactivex.Observable;
{{/useRxJava2}}
{{#useRxJava3}}
import io.reactivex.rxjava3.core.Observable;
{{/useRxJava3}}
{{#doNotUseRx}}
import retrofit2.Call;
{{/doNotUseRx}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@
<version>${retrofit-version}</version>
</dependency>
{{/useRxJava2}}
{{#useRxJava3}}
<dependency>
<groupId>io.reactivex.rxjava3</groupId>
<artifactId>rxjava</artifactId>
<version>${rxjava-version}</version>
</dependency>
<dependency>
<groupId>com.github.akarnokd</groupId>
<artifactId>rxjava3-retrofit-adapter</artifactId>
<version>3.0.0</version>
</dependency>
{{/useRxJava3}}

{{#usePlayWS}}
<!-- JSON processing: jackson -->
Expand Down

0 comments on commit 42f6021

Please sign in to comment.