Skip to content

Commit b031358

Browse files
authored
Merge pull request #3312 from wing328/java_okthtp_default
[Java] Set Java default library to "okhttp-gson"
2 parents d402f35 + c5cc0bb commit b031358

File tree

343 files changed

+11115
-3301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

343 files changed

+11115
-3301
lines changed

bin/java-petstore-all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22
# update java petstore for all supported http libraries
33

4-
./bin/java-petstore.sh
4+
./bin/java-petstore-jersey1.sh
55
./bin/java-petstore-jersey2.sh
66
./bin/java-petstore-feign.sh
77
./bin/java-petstore-okhttp-gson.sh

bin/java-petstore.sh renamed to bin/java-petstore-jersey1.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fi
2626

2727
# if you've executed sbt assembly previously it will use that instead.
2828
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29-
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l java -o samples/client/petstore/java/default -DhideGenerationTimestamp=true"
29+
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l java -o samples/client/petstore/java/jersey1 -DhideGenerationTimestamp=true --library=jersey1"
3030

31-
echo "Removing files and folders under samples/client/petstore/java/default/src/main"
32-
rm -rf samples/client/petstore/java/default/src/main
33-
find samples/client/petstore/java/default -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
31+
echo "Removing files and folders under samples/client/petstore/java/jersey1/src/main"
32+
rm -rf samples/client/petstore/java/jersey1/src/main
33+
find samples/client/petstore/java/jersey1 -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
3434
java $JAVA_OPTS -jar $executable $ags

bin/java8-petstore-jersey2.sh

100644100755
File mode changed.

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ public JavaClientCodegen() {
3232

3333
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
3434

35-
supportedLibraries.put(DEFAULT_LIBRARY, "HTTP client: Jersey client 1.19.1. JSON processing: Jackson 2.7.0");
35+
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.1. JSON processing: Jackson 2.7.0");
3636
supportedLibraries.put("feign", "HTTP client: Netflix Feign 8.16.0. JSON processing: Jackson 2.7.0");
3737
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.22.2. JSON processing: Jackson 2.7.0");
3838
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2");
3939
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)");
4040
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)");
4141

42-
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
43-
library.setEnum(supportedLibraries);
44-
library.setDefault(DEFAULT_LIBRARY);
45-
cliOptions.add(library);
42+
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
43+
libraryOption.setEnum(supportedLibraries);
44+
libraryOption.setDefault("okhttp-gson");
45+
cliOptions.add(libraryOption);
46+
47+
setLibrary("okhttp-gson");
4648

4749
}
4850

@@ -76,6 +78,7 @@ public void processOpts() {
7678
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
7779
writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md"));
7880
writeOptional(outputFolder, new SupportingFile("build.gradle.mustache", "", "build.gradle"));
81+
writeOptional(outputFolder, new SupportingFile("build.sbt.mustache", "", "build.sbt"));
7982
writeOptional(outputFolder, new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));
8083
writeOptional(outputFolder, new SupportingFile("gradle.properties.mustache", "", "gradle.properties"));
8184
writeOptional(outputFolder, new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
@@ -95,11 +98,6 @@ public void processOpts() {
9598
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
9699
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
97100

98-
if (!StringUtils.isEmpty(getLibrary())) {
99-
//TODO: add sbt support to default client
100-
supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));
101-
}
102-
103101
//TODO: add doc to retrofit1 and feign
104102
if ( "feign".equals(getLibrary()) || "retrofit".equals(getLibrary()) ){
105103
modelDocTemplateFiles.remove("model_doc.mustache");
@@ -116,7 +114,7 @@ public void processOpts() {
116114
if ("feign".equals(getLibrary())) {
117115
supportingFiles.add(new SupportingFile("FormAwareEncoder.mustache", invokerFolder, "FormAwareEncoder.java"));
118116
additionalProperties.put("jackson", "true");
119-
} else if ("okhttp-gson".equals(getLibrary())) {
117+
} else if ("okhttp-gson".equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) {
120118
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
121119
supportingFiles.add(new SupportingFile("ApiCallback.mustache", invokerFolder, "ApiCallback.java"));
122120
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
@@ -131,8 +129,10 @@ public void processOpts() {
131129
} else if("jersey2".equals(getLibrary())) {
132130
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
133131
additionalProperties.put("jackson", "true");
134-
} else if(StringUtils.isEmpty(getLibrary())) {
132+
} else if("jersey1".equals(getLibrary())) {
135133
additionalProperties.put("jackson", "true");
134+
} else {
135+
LOGGER.error("Unknown library option (-l/--library): " + getLibrary());
136136
}
137137
}
138138

@@ -175,7 +175,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
175175
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
176176
super.postProcessModelProperty(model, property);
177177
if(!BooleanUtils.toBoolean(model.isEnum)) {
178-
final String lib = getLibrary();
178+
//final String lib = getLibrary();
179179
//Needed imports for Jackson based libraries
180180
if(additionalProperties.containsKey("jackson")) {
181181
model.imports.add("JsonProperty");

modules/swagger-codegen/src/main/resources/Java/build.sbt.mustache

Whitespace-only changes.

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ public class ApiClient {
790790
* @throws ApiException If fail to deserialize response body, i.e. cannot read response body
791791
* or the Content-Type of the response is not supported.
792792
*/
793+
@SuppressWarnings("unchecked")
793794
public <T> T deserialize(Response response, Type returnType) throws ApiException {
794795
if (response == null || returnType == null) {
795796
return null;
@@ -984,6 +985,7 @@ public class ApiClient {
984985
* @param returnType Return type
985986
* @param callback ApiCallback
986987
*/
988+
@SuppressWarnings("unchecked")
987989
public <T> void executeAsync(Call call, final Type returnType, final ApiCallback<T> callback) {
988990
call.enqueue(new Callback() {
989991
@Override

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public class JSON {
9292
* @param returnType The type to deserialize inot
9393
* @return The deserialized Java object
9494
*/
95+
@SuppressWarnings("unchecked")
9596
public <T> T deserialize(String body, Type returnType) {
9697
try {
9798
if (apiClient.isLenientOnJson()) {
@@ -287,4 +288,4 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
287288
}
288289
}
289290
}
290-
{{/java8}}
291+
{{/java8}}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public void testSkipOverwrite() throws Exception {
163163

164164
final Swagger swagger = new SwaggerParser().read("src/test/resources/petstore.json");
165165
CodegenConfig codegenConfig = new JavaClientCodegen();
166+
codegenConfig.setLibrary("jersey1");
166167
codegenConfig.setOutputDir(output.getAbsolutePath());
167168

168169
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);

modules/swagger-generator/src/test/java/io/swagger/generator/online/OnlineJavaClientOptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public void getOptionsTest() throws ApiException {
2222
assertNotNull(options);
2323
final CliOption opt = options.get(CodegenConstants.LIBRARY);
2424
assertNotNull(opt);
25-
assertEquals(opt.getDefault(), JavaClientCodegen.DEFAULT_LIBRARY);
25+
assertEquals(opt.getDefault(), "okhttp-gson");
2626
}
2727
}

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,15 @@
283283
</modules>
284284
</profile>
285285
<profile>
286-
<id>java-client</id>
286+
<id>java-client-jersey1</id>
287287
<activation>
288288
<property>
289289
<name>env</name>
290290
<value>java</value>
291291
</property>
292292
</activation>
293293
<modules>
294-
<module>samples/client/petstore/java/default</module>
294+
<module>samples/client/petstore/java/jersey1</module>
295295
</modules>
296296
</profile>
297297
<profile>
@@ -615,7 +615,7 @@
615615
<module>samples/client/petstore/typescript-node/npm</module>
616616
<module>samples/client/petstore/android/volley</module>
617617
<module>samples/client/petstore/clojure</module>
618-
<module>samples/client/petstore/java/default</module>
618+
<module>samples/client/petstore/java/jersey1</module>
619619
<module>samples/client/petstore/java/feign</module>
620620
<module>samples/client/petstore/java/jersey2</module>
621621
<module>samples/client/petstore/java/okhttp-gson</module>

0 commit comments

Comments
 (0)