Skip to content

Commit ce1cf46

Browse files
authored
[Java][jersey1] add Java6 support to Java (jersey1) API client (#3919)
* add java6 support to java jersey1 * properly handle boolean value in mustache tag * add test for supportJava6 option
1 parent 03ed192 commit ce1cf46

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public class JavaClientCodegen extends AbstractJavaCodegen {
1515

1616
public static final String USE_RX_JAVA = "useRxJava";
1717
public static final String PARCELABLE_MODEL = "parcelableModel";
18+
public static final String SUPPORT_JAVA6 = "supportJava6";
1819

1920
public static final String RETROFIT_1 = "retrofit";
2021
public static final String RETROFIT_2 = "retrofit2";
2122

2223
protected String gradleWrapperPackage = "gradle.wrapper";
2324
protected boolean useRxJava = false;
2425
protected boolean parcelableModel = false;
26+
protected boolean supportJava6= false;
2527

2628
public JavaClientCodegen() {
2729
super();
@@ -34,8 +36,9 @@ public JavaClientCodegen() {
3436

3537
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
3638
cliOptions.add(CliOption.newBoolean(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson library."));
39+
cliOptions.add(CliOption.newBoolean(SUPPORT_JAVA6, "Whether to support Java6 with the Jersey1 library. (Default: false)"));
3740

38-
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.1. JSON processing: Jackson 2.7.0");
41+
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.1. JSON processing: Jackson 2.7.0. Enable Java6 support using '-DsupportJava6=true'.");
3942
supportedLibraries.put("feign", "HTTP client: Netflix Feign 8.16.0. JSON processing: Jackson 2.7.0");
4043
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.22.2. JSON processing: Jackson 2.7.0");
4144
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2. Enable Parcelable modles on Android using '-DparcelableModel=true'");
@@ -44,9 +47,9 @@ public JavaClientCodegen() {
4447

4548
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
4649
libraryOption.setEnum(supportedLibraries);
50+
// set okhttp-gson as the default
4751
libraryOption.setDefault("okhttp-gson");
4852
cliOptions.add(libraryOption);
49-
5053
setLibrary("okhttp-gson");
5154

5255
}
@@ -79,6 +82,11 @@ public void processOpts() {
7982
// put the boolean value back to PARCELABLE_MODEL in additionalProperties
8083
additionalProperties.put(PARCELABLE_MODEL, parcelableModel);
8184

85+
if (additionalProperties.containsKey(SUPPORT_JAVA6)) {
86+
this.setSupportJava6(Boolean.valueOf(additionalProperties.get(SUPPORT_JAVA6).toString()));
87+
}
88+
additionalProperties.put(SUPPORT_JAVA6, supportJava6);
89+
8290
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
8391
final String authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
8492

@@ -97,11 +105,11 @@ public void processOpts() {
97105
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
98106
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
99107
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
100-
supportingFiles.add( new SupportingFile( "gradlew.mustache", "", "gradlew") );
101-
supportingFiles.add( new SupportingFile( "gradlew.bat.mustache", "", "gradlew.bat") );
102-
supportingFiles.add( new SupportingFile( "gradle-wrapper.properties.mustache",
108+
supportingFiles.add(new SupportingFile( "gradlew.mustache", "", "gradlew") );
109+
supportingFiles.add(new SupportingFile( "gradlew.bat.mustache", "", "gradlew.bat") );
110+
supportingFiles.add(new SupportingFile( "gradle-wrapper.properties.mustache",
103111
gradleWrapperPackage.replace( ".", File.separator ), "gradle-wrapper.properties") );
104-
supportingFiles.add( new SupportingFile( "gradle-wrapper.jar",
112+
supportingFiles.add(new SupportingFile( "gradle-wrapper.jar",
105113
gradleWrapperPackage.replace( ".", File.separator ), "gradle-wrapper.jar") );
106114
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
107115
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
@@ -232,4 +240,9 @@ public void setUseRxJava(boolean useRxJava) {
232240
public void setParcelableModel(boolean parcelableModel) {
233241
this.parcelableModel = parcelableModel;
234242
}
243+
244+
public void setSupportJava6(boolean value) {
245+
this.supportJava6 = value;
246+
}
247+
235248
}

modules/swagger-codegen/src/main/resources/Java/pojo.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
7979

8080
{{/vars}}
8181

82+
{{^supportJava6}}
8283
@Override
8384
public boolean equals(java.lang.Object o) {
8485
if (this == o) {
@@ -99,6 +100,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcela
99100
return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
100101
}
101102

103+
{{/supportJava6}}
102104
@Override
103105
public String toString() {
104106
StringBuilder sb = new StringBuilder();

modules/swagger-codegen/src/test/java/io/swagger/codegen/java/JavaClientOptionsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ protected void setExpectations() {
5656
times = 1;
5757
clientCodegen.setFullJavaUtil(Boolean.valueOf(JavaOptionsProvider.FULL_JAVA_UTIL_VALUE));
5858
times = 1;
59+
//clientCodegen.setSupportJava6(Boolean.valueOf(JavaOptionsProvider.SUPPORT_JAVA6));
60+
//times = 1;
5961
}};
6062
}
6163
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/JavaClientOptionsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public Map<String, String> createOptions() {
1717
options.put(CodegenConstants.LIBRARY, DEFAULT_LIBRARY_VALUE);
1818
options.put(JavaClientCodegen.USE_RX_JAVA, "false");
1919
options.put(JavaClientCodegen.PARCELABLE_MODEL, "false");
20+
options.put(JavaClientCodegen.SUPPORT_JAVA6, "false");
2021

2122
return options;
2223
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/JavaOptionsProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class JavaOptionsProvider implements OptionsProvider {
1919
public static final String SERIALIZABLE_MODEL_VALUE = "false";
2020
public static final String FULL_JAVA_UTIL_VALUE = "true";
2121
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
22+
//public static final String SUPPORT_JAVA6 = "true";
2223

2324
private ImmutableMap<String, String> options;
2425

@@ -42,14 +43,15 @@ public JavaOptionsProvider() {
4243
.put(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, "true")
4344
.put(JavaClientCodegen.DATE_LIBRARY, "joda")
4445
.put("hideGenerationTimestamp", "true")
46+
//.put("supportJava6", "true")
4547
.build();
4648
}
4749

4850
/**
4951
* Use the default options, but override the ones found in additionalOptions.
5052
*/
5153
public JavaOptionsProvider(Map<String, String> additionalOptions) {
52-
options = new ImmutableMap.Builder<String, String>()
54+
options = new ImmutableMap.Builder<String, String>()
5355
.putAll(options)
5456
.putAll(additionalOptions)
5557
.build();

0 commit comments

Comments
 (0)