Skip to content

Commit

Permalink
fixed error handling (now retrofit2.HttpException is thrown instead o…
Browse files Browse the repository at this point in the history
…f RuntimeException on API error) (swagger-api#6302)

value adapter now supports both F.Promise<Model> and F.Promise<Response<Model>> in retrofit interface
switched dataLibrary to java8
  • Loading branch information
lukoyanov authored and wing328 committed Aug 15, 2017
1 parent e31cef8 commit 936c586
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 599 deletions.
2 changes: 1 addition & 1 deletion bin/java-petstore-retrofit2-play24.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"useBeanValidation":"true","enableBuilderSupport":"true","library":"retrofit2", "usePlay24WS":"true"}
{"useBeanValidation":"true","enableBuilderSupport":"true","library":"retrofit2","usePlay24WS":"true","dateLibrary":"java8"}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
supportingFiles.add(new SupportingFile("CollectionFormats.mustache", invokerFolder, "CollectionFormats.java"));
additionalProperties.put("gson", "true");
if ("retrofit2".equals(getLibrary())) {
if ("retrofit2".equals(getLibrary()) && !usePlay24WS) {
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
}
} else if ("jersey2".equals(getLibrary()) || "resteasy".equals(getLibrary())) {
Expand All @@ -228,7 +228,7 @@ public void processOpts() {
LOGGER.error("Unknown library option (-l/--library): " + getLibrary());
}

if (Boolean.TRUE.equals(additionalProperties.get(USE_PLAY24_WS))) {
if (usePlay24WS) {
// remove unsupported auth
Iterator<SupportingFile> iter = supportingFiles.iterator();
while (iter.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,39 @@ public class Play24CallAdapterFactory extends CallAdapter.Factory {
return createAdapter((ParameterizedType) returnType);
}

private Type getTypeParam(ParameterizedType type) {
Type[] types = type.getActualTypeArguments();
private CallAdapter<?, F.Promise<?>> createAdapter(ParameterizedType returnType) {
Type[] types = returnType.getActualTypeArguments();
if (types.length != 1) {
throw new IllegalStateException("Must be exactly one type parameter");
}

Type paramType = types[0];
if (paramType instanceof WildcardType) {
return ((WildcardType) paramType).getUpperBounds()[0];
Type resultType = types[0];
Class<?> rawTypeParam = getRawType(resultType);

boolean includeResponse = false;
if (rawTypeParam == Response.class) {
if (!(resultType instanceof ParameterizedType)) {
throw new IllegalStateException("Response must be parameterized"
+ " as Response<T>");
}
resultType = ((ParameterizedType) resultType).getActualTypeArguments()[0];
includeResponse = true;
}

return paramType;
}

private CallAdapter<?, F.Promise<?>> createAdapter(ParameterizedType returnType) {
Type parameterType = getTypeParam(returnType);
return new ValueAdapter(parameterType);
return new ValueAdapter(resultType, includeResponse);
}

/**
* Adpater that coverts values returned by API interface into Play promises
* Adpater that coverts values returned by API interface into CompletionStage
*/
static final class ValueAdapter<R> implements CallAdapter<R, F.Promise<R>> {
private static final class ValueAdapter<R> implements CallAdapter<R, F.Promise<R>> {
private final Type responseType;
private final boolean includeResponse;
ValueAdapter(Type responseType) {
ValueAdapter(Type responseType, boolean includeResponse) {
this.responseType = responseType;
this.includeResponse = includeResponse;
}

@Override
Expand All @@ -71,9 +76,13 @@ public class Play24CallAdapterFactory extends CallAdapter.Factory {
@Override
public void onResponse(Call<R> call, Response<R> response) {
if (response.isSuccessful()) {
promise.success(response.body());
if (includeResponse) {
promise.success((R) response);
} else {
promise.success(response.body());
}
} else {
promise.failure(new Exception(response.errorBody().toString()));
promise.failure(new HttpException(response));
}
}

Expand All @@ -87,4 +96,5 @@ public class Play24CallAdapterFactory extends CallAdapter.Factory {
return promise;
}
}

}
12 changes: 5 additions & 7 deletions samples/client/petstore/java/retrofit2-play24/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ if(hasProperty('target') && target == 'android') {
targetSdkVersion 25
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

// Rename the aar correctly
Expand Down Expand Up @@ -78,8 +78,8 @@ if(hasProperty('target') && target == 'android') {
apply plugin: 'java'
apply plugin: 'maven'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

install {
repositories.mavenInstaller {
Expand All @@ -100,7 +100,6 @@ ext {
play_version = "2.4.11"
swagger_annotations_version = "1.5.15"
junit_version = "4.12"
threetenbp_version = "1.3.5"
}

dependencies {
Expand All @@ -109,12 +108,11 @@ dependencies {
compile "com.squareup.retrofit2:converter-gson:$retrofit_version"
compile "io.swagger:swagger-annotations:$swagger_annotations_version"
compile "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:$oltu_version"
compile "org.threeten:threetenbp:$threetenbp_version"
compile "com.typesafe.play:play-java-ws_2.11:$play_version"
compile "com.squareup.retrofit2:converter-jackson:$retrofit_version"
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jackson_version"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"

testCompile "junit:junit:$junit_version"
}
1 change: 0 additions & 1 deletion samples/client/petstore/java/retrofit2-play24/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ lazy val root = (project in file(".")).
"com.fasterxml.jackson.core" % "jackson-databind" % "2.8.9" % "compile",
"io.swagger" % "swagger-annotations" % "1.5.15" % "compile",
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1" % "compile",
"org.threeten" % "threetenbp" % "1.3.5" % "compile",
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.11" % "test"
)
Expand Down
10 changes: 2 additions & 8 deletions samples/client/petstore/java/retrofit2-play24/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,6 @@
<artifactId>org.apache.oltu.oauth2.client</artifactId>
<version>${oltu-version}</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>${threetenbp-version}</version>
</dependency>

<!-- JSON processing: jackson -->
<dependency>
Expand All @@ -223,7 +218,7 @@
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
Expand All @@ -243,14 +238,13 @@
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<swagger-core-version>1.5.15</swagger-core-version>
<jackson-version>2.8.9</jackson-version>
<play-version>2.4.11</play-version>
<retrofit-version>2.3.0</retrofit-version>
<threetenbp-version>1.3.5</threetenbp-version>
<oltu-version>1.0.1</oltu-version>
<junit-version>4.12</junit-version>
</properties>
Expand Down
Loading

0 comments on commit 936c586

Please sign in to comment.