Skip to content

Commit c9c918a

Browse files
committed
Add okhttp4-gson codegen
Add support for okhttp4-gson as a replacement for okhttp-gson okhttp-gson is based on okhttp 2.x which is no longer supported. okhttp 3.x is also [not supported anymore](https://square.github.io/okhttp/security/security/) Creating a new target for codegen since okhttp 4.x is a breaking change over the okhttp 2.x with the changed package names. The overall change is similar to the existing code with a few updates - updates due to deprecations documented in https://square.github.io/okhttp/changelogs/changelog_3x/#version-300-rc1 - kept the utility methods in the existing code as-is tested `mvn package`, ran the petstore generation scripts and the integration tests that are generated Fixes #9606
1 parent d7d5b9a commit c9c918a

File tree

263 files changed

+34494
-3
lines changed

Some content is hidden

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

263 files changed

+34494
-3
lines changed

Diff for: bin/java-petstore-all.sh

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
./bin/java-petstore-feign.sh
77
./bin/java-petstore-okhttp-gson.sh
88
./bin/java-petstore-okhttp-gson-parcelable.sh
9+
./bin/java-petstore-okhttp4-gson.sh
10+
./bin/java-petstore-okhttp4-gson-parcelable.sh
911
./bin/java-petstore-retrofit.sh
1012
./bin/java-petstore-retrofit2.sh
1113
./bin/java-petstore-retrofit2rx.sh

Diff for: bin/java-petstore-okhttp4-gson-parcelable.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/Java/libraries/okhttp4-gson -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l java -c bin/java-petstore-okhttp4-gson.json -o samples/client/petstore/java/okhttp4-gson-parcelableModel -DhideGenerationTimestamp=true,parcelableModel=true"
30+
31+
rm -rf samples/client/petstore/java/okhttp4-gson-parcelableModel/src/main
32+
find samples/client/petstore/java/okhttp4-gson-parcelableModel -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
33+
java $JAVA_OPTS -jar $executable $ags

Diff for: bin/java-petstore-okhttp4-gson.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"library": "okhttp4-gson",
3+
"artifactId": "swagger-petstore-okhttp4-gson"
4+
}

Diff for: bin/java-petstore-okhttp4-gson.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/Java/libraries/okhttp4-gson -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l java -c bin/java-petstore-okhttp4-gson.json -o samples/client/petstore/java/okhttp4-gson -DhideGenerationTimestamp=true"
30+
31+
rm -rf samples/client/petstore/java/okhttp4-gson/src/main
32+
find samples/client/petstore/java/okhttp4-gson -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
33+
java $JAVA_OPTS -jar $executable $ags

Diff for: bin/security/java-petstore-okhttp4-gson.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/Java/libraries/okhttp4-gson -i modules/swagger-codegen/src/test/resources/2_0/petstore-security-test.yaml -l java -c bin/java-petstore-okhttp4-gson.json -o samples/client/petstore-security-test/java/okhttp4-gson -DhideGenerationTimestamp=true"
30+
31+
rm -rf samples/client/petstore-security-test/java/okhttp4-gson/src/main
32+
find samples/client/petstore-security-test/java/okhttp4-gson -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
33+
java $JAVA_OPTS -jar $executable $ags

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public JavaClientCodegen() {
7171

7272
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
7373
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA2, "Whether to use the RxJava2 adapter with the retrofit2 library."));
74-
cliOptions.add(CliOption.newBoolean(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson library."));
74+
cliOptions.add(CliOption.newBoolean(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson or okhttp4-gson library."));
7575
cliOptions.add(CliOption.newBoolean(USE_PLAY_WS, "Use Play! Async HTTP client (Play WS API)"));
7676
cliOptions.add(CliOption.newString(PLAY_VERSION, "Version of Play! Framework (possible values \"play24\", \"play25\")"));
7777
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
@@ -85,6 +85,7 @@ public JavaClientCodegen() {
8585
supportedLibraries.put("feign", "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.11.4");
8686
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.29.1. JSON processing: Jackson 2.11.4");
8787
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.8.1. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
88+
supportedLibraries.put("okhttp4-gson", "HTTP client: OkHttp 4.10.0. JSON processing: Gson 2.8.1. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
8889
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.");
8990
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.8.0. JSON processing: Gson 2.6.1 (Retrofit 2.3.0). Enable the RxJava adapter using '-DuseRxJava[2]=true'. (RxJava 1.x or 2.x)");
9091
supportedLibraries.put("resttemplate", "HTTP client: Spring RestTemplate 4.3.9-RELEASE. JSON processing: Jackson 2.11.4");
@@ -235,8 +236,8 @@ public void processOpts() {
235236
additionalProperties.put("jackson", "true");
236237
supportingFiles.add(new SupportingFile("ParamExpander.mustache", invokerFolder, "ParamExpander.java"));
237238
supportingFiles.add(new SupportingFile("EncodingUtils.mustache", invokerFolder, "EncodingUtils.java"));
238-
} else if ("okhttp-gson".equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) {
239-
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
239+
} else if ("okhttp-gson".equals(getLibrary()) || "okhttp4-gson".equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) {
240+
// the "okhttp-gson" and "okhttp4-gson" library template requires "ApiCallback.mustache" for async call
240241
supportingFiles.add(new SupportingFile("ApiCallback.mustache", invokerFolder, "ApiCallback.java"));
241242
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
242243
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{{>licenseInfo}}
2+
3+
package {{invokerPackage}};
4+
5+
import java.io.IOException;
6+
7+
import java.util.Map;
8+
import java.util.List;
9+
10+
/**
11+
* Callback for asynchronous API call.
12+
*
13+
* @param <T> The return type
14+
*/
15+
public interface ApiCallback<T> {
16+
/**
17+
* This is called when the API call fails.
18+
*
19+
* @param e The exception causing the failure
20+
* @param statusCode Status code of the response if available, otherwise it would be 0
21+
* @param responseHeaders Headers of the response if available, otherwise it would be null
22+
*/
23+
void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders);
24+
25+
/**
26+
* This is called when the API call succeeded.
27+
*
28+
* @param result The result deserialized from response
29+
* @param statusCode Status code of the response
30+
* @param responseHeaders Headers of the response
31+
*/
32+
void onSuccess(T result, int statusCode, Map<String, List<String>> responseHeaders);
33+
34+
/**
35+
* This is called when the API upload processing.
36+
*
37+
* @param bytesWritten bytes Written
38+
* @param contentLength content length of request body
39+
* @param done write end
40+
*/
41+
void onUploadProgress(long bytesWritten, long contentLength, boolean done);
42+
43+
/**
44+
* This is called when the API downlond processing.
45+
*
46+
* @param bytesRead bytes Read
47+
* @param contentLength content lenngth of the response
48+
* @param done Read end
49+
*/
50+
void onDownloadProgress(long bytesRead, long contentLength, boolean done);
51+
}

0 commit comments

Comments
 (0)