-
Notifications
You must be signed in to change notification settings - Fork 6k
[Java][Retrofit] Add RxJava2 support #4708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"; | ||
|
|
||
|
|
@@ -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; | ||
|
|
@@ -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.")); | ||
|
|
@@ -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); | ||
|
|
@@ -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())); | ||
| } | ||
| if (additionalProperties.containsKey(DO_NOT_USE_RX)) { | ||
| this.setDoNotUseRx(Boolean.valueOf(additionalProperties.get(DO_NOT_USE_RX).toString())); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
| if (additionalProperties.containsKey(USE_PLAY24_WS)) { | ||
| this.setUsePlay24WS(Boolean.valueOf(additionalProperties.get(USE_PLAY24_WS).toString())); | ||
| } | ||
|
|
@@ -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) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
| <version>1.0.0</version> | ||
| </dependency> | ||
| {{/useRxJava2}} | ||
|
|
||
| {{#usePlay24WS}} | ||
| <!-- JSON processing: jackson --> | ||
|
|
@@ -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}} | ||
|
|
||
There was a problem hiding this comment.
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.