mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-07 02:55:19 +00:00
fixed error handling (now retrofit2.HttpException is thrown instead of RuntimeException on API error) (#6302)
value adapter now supports both F.Promise<Model> and F.Promise<Response<Model>> in retrofit interface switched dataLibrary to java8
This commit is contained in:
parent
e31cef8c71
commit
936c586df3
@ -1 +1 @@
|
||||
{"useBeanValidation":"true","enableBuilderSupport":"true","library":"retrofit2", "usePlay24WS":"true"}
|
||||
{"useBeanValidation":"true","enableBuilderSupport":"true","library":"retrofit2","usePlay24WS":"true","dateLibrary":"java8"}
|
||||
|
@ -204,7 +204,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
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())) {
|
||||
@ -228,7 +228,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
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()) {
|
||||
|
@ -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;
|
||||
return new ValueAdapter(resultType, includeResponse);
|
||||
}
|
||||
|
||||
private CallAdapter<?, F.Promise<?>> createAdapter(ParameterizedType returnType) {
|
||||
Type parameterType = getTypeParam(returnType);
|
||||
return new ValueAdapter(parameterType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,4 +96,5 @@ public class Play24CallAdapterFactory extends CallAdapter.Factory {
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
@ -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 {
|
||||
@ -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 {
|
||||
@ -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"
|
||||
}
|
||||
|
@ -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"
|
||||
)
|
||||
|
@ -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>
|
||||
@ -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>
|
||||
@ -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>
|
||||
|
@ -1,232 +0,0 @@
|
||||
package io.swagger.client;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonTokenId;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.datatype.threetenbp.DateTimeUtils;
|
||||
import com.fasterxml.jackson.datatype.threetenbp.DecimalUtils;
|
||||
import com.fasterxml.jackson.datatype.threetenbp.deser.ThreeTenDateTimeDeserializerBase;
|
||||
import com.fasterxml.jackson.datatype.threetenbp.function.BiFunction;
|
||||
import com.fasterxml.jackson.datatype.threetenbp.function.Function;
|
||||
import org.threeten.bp.DateTimeException;
|
||||
import org.threeten.bp.Instant;
|
||||
import org.threeten.bp.OffsetDateTime;
|
||||
import org.threeten.bp.ZoneId;
|
||||
import org.threeten.bp.ZonedDateTime;
|
||||
import org.threeten.bp.format.DateTimeFormatter;
|
||||
import org.threeten.bp.temporal.Temporal;
|
||||
import org.threeten.bp.temporal.TemporalAccessor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Deserializer for ThreeTen temporal {@link Instant}s, {@link OffsetDateTime}, and {@link ZonedDateTime}s.
|
||||
* Adapted from the jackson threetenbp InstantDeserializer to add support for deserializing rfc822 format.
|
||||
*
|
||||
* @author Nick Williams
|
||||
*/
|
||||
public class CustomInstantDeserializer<T extends Temporal>
|
||||
extends ThreeTenDateTimeDeserializerBase<T> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final CustomInstantDeserializer<Instant> INSTANT = new CustomInstantDeserializer<Instant>(
|
||||
Instant.class, DateTimeFormatter.ISO_INSTANT,
|
||||
new Function<TemporalAccessor, Instant>() {
|
||||
@Override
|
||||
public Instant apply(TemporalAccessor temporalAccessor) {
|
||||
return Instant.from(temporalAccessor);
|
||||
}
|
||||
},
|
||||
new Function<FromIntegerArguments, Instant>() {
|
||||
@Override
|
||||
public Instant apply(FromIntegerArguments a) {
|
||||
return Instant.ofEpochMilli(a.value);
|
||||
}
|
||||
},
|
||||
new Function<FromDecimalArguments, Instant>() {
|
||||
@Override
|
||||
public Instant apply(FromDecimalArguments a) {
|
||||
return Instant.ofEpochSecond(a.integer, a.fraction);
|
||||
}
|
||||
},
|
||||
null
|
||||
);
|
||||
|
||||
public static final CustomInstantDeserializer<OffsetDateTime> OFFSET_DATE_TIME = new CustomInstantDeserializer<OffsetDateTime>(
|
||||
OffsetDateTime.class, DateTimeFormatter.ISO_OFFSET_DATE_TIME,
|
||||
new Function<TemporalAccessor, OffsetDateTime>() {
|
||||
@Override
|
||||
public OffsetDateTime apply(TemporalAccessor temporalAccessor) {
|
||||
return OffsetDateTime.from(temporalAccessor);
|
||||
}
|
||||
},
|
||||
new Function<FromIntegerArguments, OffsetDateTime>() {
|
||||
@Override
|
||||
public OffsetDateTime apply(FromIntegerArguments a) {
|
||||
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(a.value), a.zoneId);
|
||||
}
|
||||
},
|
||||
new Function<FromDecimalArguments, OffsetDateTime>() {
|
||||
@Override
|
||||
public OffsetDateTime apply(FromDecimalArguments a) {
|
||||
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(a.integer, a.fraction), a.zoneId);
|
||||
}
|
||||
},
|
||||
new BiFunction<OffsetDateTime, ZoneId, OffsetDateTime>() {
|
||||
@Override
|
||||
public OffsetDateTime apply(OffsetDateTime d, ZoneId z) {
|
||||
return d.withOffsetSameInstant(z.getRules().getOffset(d.toLocalDateTime()));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
public static final CustomInstantDeserializer<ZonedDateTime> ZONED_DATE_TIME = new CustomInstantDeserializer<ZonedDateTime>(
|
||||
ZonedDateTime.class, DateTimeFormatter.ISO_ZONED_DATE_TIME,
|
||||
new Function<TemporalAccessor, ZonedDateTime>() {
|
||||
@Override
|
||||
public ZonedDateTime apply(TemporalAccessor temporalAccessor) {
|
||||
return ZonedDateTime.from(temporalAccessor);
|
||||
}
|
||||
},
|
||||
new Function<FromIntegerArguments, ZonedDateTime>() {
|
||||
@Override
|
||||
public ZonedDateTime apply(FromIntegerArguments a) {
|
||||
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(a.value), a.zoneId);
|
||||
}
|
||||
},
|
||||
new Function<FromDecimalArguments, ZonedDateTime>() {
|
||||
@Override
|
||||
public ZonedDateTime apply(FromDecimalArguments a) {
|
||||
return ZonedDateTime.ofInstant(Instant.ofEpochSecond(a.integer, a.fraction), a.zoneId);
|
||||
}
|
||||
},
|
||||
new BiFunction<ZonedDateTime, ZoneId, ZonedDateTime>() {
|
||||
@Override
|
||||
public ZonedDateTime apply(ZonedDateTime zonedDateTime, ZoneId zoneId) {
|
||||
return zonedDateTime.withZoneSameInstant(zoneId);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
protected final Function<FromIntegerArguments, T> fromMilliseconds;
|
||||
|
||||
protected final Function<FromDecimalArguments, T> fromNanoseconds;
|
||||
|
||||
protected final Function<TemporalAccessor, T> parsedToValue;
|
||||
|
||||
protected final BiFunction<T, ZoneId, T> adjust;
|
||||
|
||||
protected CustomInstantDeserializer(Class<T> supportedType,
|
||||
DateTimeFormatter parser,
|
||||
Function<TemporalAccessor, T> parsedToValue,
|
||||
Function<FromIntegerArguments, T> fromMilliseconds,
|
||||
Function<FromDecimalArguments, T> fromNanoseconds,
|
||||
BiFunction<T, ZoneId, T> adjust) {
|
||||
super(supportedType, parser);
|
||||
this.parsedToValue = parsedToValue;
|
||||
this.fromMilliseconds = fromMilliseconds;
|
||||
this.fromNanoseconds = fromNanoseconds;
|
||||
this.adjust = adjust == null ? new BiFunction<T, ZoneId, T>() {
|
||||
@Override
|
||||
public T apply(T t, ZoneId zoneId) {
|
||||
return t;
|
||||
}
|
||||
} : adjust;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected CustomInstantDeserializer(CustomInstantDeserializer<T> base, DateTimeFormatter f) {
|
||||
super((Class<T>) base.handledType(), f);
|
||||
parsedToValue = base.parsedToValue;
|
||||
fromMilliseconds = base.fromMilliseconds;
|
||||
fromNanoseconds = base.fromNanoseconds;
|
||||
adjust = base.adjust;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonDeserializer<T> withDateFormat(DateTimeFormatter dtf) {
|
||||
if (dtf == _formatter) {
|
||||
return this;
|
||||
}
|
||||
return new CustomInstantDeserializer<T>(this, dtf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T deserialize(JsonParser parser, DeserializationContext context) throws IOException {
|
||||
//NOTE: Timestamps contain no timezone info, and are always in configured TZ. Only
|
||||
//string values have to be adjusted to the configured TZ.
|
||||
switch (parser.getCurrentTokenId()) {
|
||||
case JsonTokenId.ID_NUMBER_FLOAT: {
|
||||
BigDecimal value = parser.getDecimalValue();
|
||||
long seconds = value.longValue();
|
||||
int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds);
|
||||
return fromNanoseconds.apply(new FromDecimalArguments(
|
||||
seconds, nanoseconds, getZone(context)));
|
||||
}
|
||||
|
||||
case JsonTokenId.ID_NUMBER_INT: {
|
||||
long timestamp = parser.getLongValue();
|
||||
if (context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) {
|
||||
return this.fromNanoseconds.apply(new FromDecimalArguments(
|
||||
timestamp, 0, this.getZone(context)
|
||||
));
|
||||
}
|
||||
return this.fromMilliseconds.apply(new FromIntegerArguments(
|
||||
timestamp, this.getZone(context)
|
||||
));
|
||||
}
|
||||
|
||||
case JsonTokenId.ID_STRING: {
|
||||
String string = parser.getText().trim();
|
||||
if (string.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
if (string.endsWith("+0000")) {
|
||||
string = string.substring(0, string.length() - 5) + "Z";
|
||||
}
|
||||
T value;
|
||||
try {
|
||||
TemporalAccessor acc = _formatter.parse(string);
|
||||
value = parsedToValue.apply(acc);
|
||||
if (context.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)) {
|
||||
return adjust.apply(value, this.getZone(context));
|
||||
}
|
||||
} catch (DateTimeException e) {
|
||||
throw _peelDTE(e);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
throw context.mappingException("Expected type float, integer, or string.");
|
||||
}
|
||||
|
||||
private ZoneId getZone(DeserializationContext context) {
|
||||
// Instants are always in UTC, so don't waste compute cycles
|
||||
return (_valueClass == Instant.class) ? null : DateTimeUtils.timeZoneToZoneId(context.getTimeZone());
|
||||
}
|
||||
|
||||
private static class FromIntegerArguments {
|
||||
public final long value;
|
||||
public final ZoneId zoneId;
|
||||
|
||||
private FromIntegerArguments(long value, ZoneId zoneId) {
|
||||
this.value = value;
|
||||
this.zoneId = zoneId;
|
||||
}
|
||||
}
|
||||
|
||||
private static class FromDecimalArguments {
|
||||
public final long integer;
|
||||
public final int fraction;
|
||||
public final ZoneId zoneId;
|
||||
|
||||
private FromDecimalArguments(long integer, int fraction, ZoneId zoneId) {
|
||||
this.integer = integer;
|
||||
this.fraction = fraction;
|
||||
this.zoneId = zoneId;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,289 +0,0 @@
|
||||
/*
|
||||
* Swagger Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
*
|
||||
* OpenAPI spec version: 1.0.0
|
||||
* Contact: apiteam@swagger.io
|
||||
*
|
||||
* NOTE: This class is auto generated by the swagger code generator program.
|
||||
* https://github.com/swagger-api/swagger-codegen.git
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
|
||||
package io.swagger.client;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.internal.bind.util.ISO8601Utils;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import org.threeten.bp.LocalDate;
|
||||
import org.threeten.bp.OffsetDateTime;
|
||||
import org.threeten.bp.format.DateTimeFormatter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Date;
|
||||
|
||||
public class JSON {
|
||||
private Gson gson;
|
||||
private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
|
||||
private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
|
||||
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
|
||||
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
|
||||
|
||||
public JSON() {
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Date.class, dateTypeAdapter)
|
||||
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
|
||||
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
|
||||
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
|
||||
.create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Gson.
|
||||
*
|
||||
* @return Gson
|
||||
*/
|
||||
public Gson getGson() {
|
||||
return gson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Gson.
|
||||
*
|
||||
* @param gson Gson
|
||||
* @return JSON
|
||||
*/
|
||||
public JSON setGson(Gson gson) {
|
||||
this.gson = gson;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gson TypeAdapter for JSR310 OffsetDateTime type
|
||||
*/
|
||||
public static class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
|
||||
|
||||
private DateTimeFormatter formatter;
|
||||
|
||||
public OffsetDateTimeTypeAdapter() {
|
||||
this(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
}
|
||||
|
||||
public OffsetDateTimeTypeAdapter(DateTimeFormatter formatter) {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
public void setFormat(DateTimeFormatter dateFormat) {
|
||||
this.formatter = dateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, OffsetDateTime date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.format(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
if (date.endsWith("+0000")) {
|
||||
date = date.substring(0, date.length()-5) + "Z";
|
||||
}
|
||||
return OffsetDateTime.parse(date, formatter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gson TypeAdapter for JSR310 LocalDate type
|
||||
*/
|
||||
public class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private DateTimeFormatter formatter;
|
||||
|
||||
public LocalDateTypeAdapter() {
|
||||
this(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
}
|
||||
|
||||
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
public void setFormat(DateTimeFormatter dateFormat) {
|
||||
this.formatter = dateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
out.value(formatter.format(date));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
return LocalDate.parse(date, formatter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSON setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
|
||||
offsetDateTimeTypeAdapter.setFormat(dateFormat);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSON setLocalDateFormat(DateTimeFormatter dateFormat) {
|
||||
localDateTypeAdapter.setFormat(dateFormat);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gson TypeAdapter for java.sql.Date type
|
||||
* If the dateFormat is null, a simple "yyyy-MM-dd" format will be used
|
||||
* (more efficient than SimpleDateFormat).
|
||||
*/
|
||||
public static class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
|
||||
|
||||
private DateFormat dateFormat;
|
||||
|
||||
public SqlDateTypeAdapter() {
|
||||
}
|
||||
|
||||
public SqlDateTypeAdapter(DateFormat dateFormat) {
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
public void setFormat(DateFormat dateFormat) {
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, java.sql.Date date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
String value;
|
||||
if (dateFormat != null) {
|
||||
value = dateFormat.format(date);
|
||||
} else {
|
||||
value = date.toString();
|
||||
}
|
||||
out.value(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.sql.Date read(JsonReader in) throws IOException {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
try {
|
||||
if (dateFormat != null) {
|
||||
return new java.sql.Date(dateFormat.parse(date).getTime());
|
||||
}
|
||||
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
|
||||
} catch (ParseException e) {
|
||||
throw new JsonParseException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gson TypeAdapter for java.util.Date type
|
||||
* If the dateFormat is null, ISO8601Utils will be used.
|
||||
*/
|
||||
public static class DateTypeAdapter extends TypeAdapter<Date> {
|
||||
|
||||
private DateFormat dateFormat;
|
||||
|
||||
public DateTypeAdapter() {
|
||||
}
|
||||
|
||||
public DateTypeAdapter(DateFormat dateFormat) {
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
public void setFormat(DateFormat dateFormat) {
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, Date date) throws IOException {
|
||||
if (date == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
String value;
|
||||
if (dateFormat != null) {
|
||||
value = dateFormat.format(date);
|
||||
} else {
|
||||
value = ISO8601Utils.format(date, true);
|
||||
}
|
||||
out.value(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date read(JsonReader in) throws IOException {
|
||||
try {
|
||||
switch (in.peek()) {
|
||||
case NULL:
|
||||
in.nextNull();
|
||||
return null;
|
||||
default:
|
||||
String date = in.nextString();
|
||||
try {
|
||||
if (dateFormat != null) {
|
||||
return dateFormat.parse(date);
|
||||
}
|
||||
return ISO8601Utils.parse(date, new ParsePosition(0));
|
||||
} catch (ParseException e) {
|
||||
throw new JsonParseException(e);
|
||||
}
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new JsonParseException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSON setDateFormat(DateFormat dateFormat) {
|
||||
dateTypeAdapter.setFormat(dateFormat);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSON setSqlDateFormat(DateFormat dateFormat) {
|
||||
sqlDateTypeAdapter.setFormat(dateFormat);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
return new ValueAdapter(resultType, includeResponse);
|
||||
}
|
||||
|
||||
private CallAdapter<?, F.Promise<?>> createAdapter(ParameterizedType returnType) {
|
||||
Type parameterType = getTypeParam(returnType);
|
||||
return new ValueAdapter(parameterType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,4 +96,5 @@ public class Play24CallAdapterFactory extends CallAdapter.Factory {
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ import okhttp3.RequestBody;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import io.swagger.client.model.Client;
|
||||
import org.threeten.bp.LocalDate;
|
||||
import org.threeten.bp.OffsetDateTime;
|
||||
import java.time.LocalDate;
|
||||
import java.time.OffsetDateTime;
|
||||
import io.swagger.client.model.OuterComposite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -43,7 +43,7 @@ public class AdditionalPropertiesClass {
|
||||
|
||||
public AdditionalPropertiesClass putMapPropertyItem(String key, String mapPropertyItem) {
|
||||
if (this.mapProperty == null) {
|
||||
this.mapProperty = new HashMap<String, String>();
|
||||
this.mapProperty = new HashMap<>();
|
||||
}
|
||||
this.mapProperty.put(key, mapPropertyItem);
|
||||
return this;
|
||||
@ -69,7 +69,7 @@ public class AdditionalPropertiesClass {
|
||||
|
||||
public AdditionalPropertiesClass putMapOfMapPropertyItem(String key, Map<String, String> mapOfMapPropertyItem) {
|
||||
if (this.mapOfMapProperty == null) {
|
||||
this.mapOfMapProperty = new HashMap<String, Map<String, String>>();
|
||||
this.mapOfMapProperty = new HashMap<>();
|
||||
}
|
||||
this.mapOfMapProperty.put(key, mapOfMapPropertyItem);
|
||||
return this;
|
||||
|
@ -40,7 +40,7 @@ public class ArrayOfArrayOfNumberOnly {
|
||||
|
||||
public ArrayOfArrayOfNumberOnly addArrayArrayNumberItem(List<BigDecimal> arrayArrayNumberItem) {
|
||||
if (this.arrayArrayNumber == null) {
|
||||
this.arrayArrayNumber = new ArrayList<List<BigDecimal>>();
|
||||
this.arrayArrayNumber = new ArrayList<>();
|
||||
}
|
||||
this.arrayArrayNumber.add(arrayArrayNumberItem);
|
||||
return this;
|
||||
|
@ -40,7 +40,7 @@ public class ArrayOfNumberOnly {
|
||||
|
||||
public ArrayOfNumberOnly addArrayNumberItem(BigDecimal arrayNumberItem) {
|
||||
if (this.arrayNumber == null) {
|
||||
this.arrayNumber = new ArrayList<BigDecimal>();
|
||||
this.arrayNumber = new ArrayList<>();
|
||||
}
|
||||
this.arrayNumber.add(arrayNumberItem);
|
||||
return this;
|
||||
|
@ -46,7 +46,7 @@ public class ArrayTest {
|
||||
|
||||
public ArrayTest addArrayOfStringItem(String arrayOfStringItem) {
|
||||
if (this.arrayOfString == null) {
|
||||
this.arrayOfString = new ArrayList<String>();
|
||||
this.arrayOfString = new ArrayList<>();
|
||||
}
|
||||
this.arrayOfString.add(arrayOfStringItem);
|
||||
return this;
|
||||
@ -72,7 +72,7 @@ public class ArrayTest {
|
||||
|
||||
public ArrayTest addArrayArrayOfIntegerItem(List<Long> arrayArrayOfIntegerItem) {
|
||||
if (this.arrayArrayOfInteger == null) {
|
||||
this.arrayArrayOfInteger = new ArrayList<List<Long>>();
|
||||
this.arrayArrayOfInteger = new ArrayList<>();
|
||||
}
|
||||
this.arrayArrayOfInteger.add(arrayArrayOfIntegerItem);
|
||||
return this;
|
||||
@ -99,7 +99,7 @@ public class ArrayTest {
|
||||
|
||||
public ArrayTest addArrayArrayOfModelItem(List<ReadOnlyFirst> arrayArrayOfModelItem) {
|
||||
if (this.arrayArrayOfModel == null) {
|
||||
this.arrayArrayOfModel = new ArrayList<List<ReadOnlyFirst>>();
|
||||
this.arrayArrayOfModel = new ArrayList<>();
|
||||
}
|
||||
this.arrayArrayOfModel.add(arrayArrayOfModelItem);
|
||||
return this;
|
||||
|
@ -130,7 +130,7 @@ public class EnumArrays {
|
||||
|
||||
public EnumArrays addArrayEnumItem(ArrayEnumEnum arrayEnumItem) {
|
||||
if (this.arrayEnum == null) {
|
||||
this.arrayEnum = new ArrayList<ArrayEnumEnum>();
|
||||
this.arrayEnum = new ArrayList<>();
|
||||
}
|
||||
this.arrayEnum.add(arrayEnumItem);
|
||||
return this;
|
||||
|
@ -13,18 +13,15 @@
|
||||
|
||||
package io.swagger.client.model;
|
||||
|
||||
import java.util.Objects;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
import org.threeten.bp.LocalDate;
|
||||
import org.threeten.bp.OffsetDateTime;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* FormatTest
|
||||
|
@ -78,7 +78,7 @@ public class MapTest {
|
||||
|
||||
public MapTest putMapMapOfStringItem(String key, Map<String, String> mapMapOfStringItem) {
|
||||
if (this.mapMapOfString == null) {
|
||||
this.mapMapOfString = new HashMap<String, Map<String, String>>();
|
||||
this.mapMapOfString = new HashMap<>();
|
||||
}
|
||||
this.mapMapOfString.put(key, mapMapOfStringItem);
|
||||
return this;
|
||||
@ -105,7 +105,7 @@ public class MapTest {
|
||||
|
||||
public MapTest putMapOfEnumStringItem(String key, InnerEnum mapOfEnumStringItem) {
|
||||
if (this.mapOfEnumString == null) {
|
||||
this.mapOfEnumString = new HashMap<String, InnerEnum>();
|
||||
this.mapOfEnumString = new HashMap<>();
|
||||
}
|
||||
this.mapOfEnumString.put(key, mapOfEnumStringItem);
|
||||
return this;
|
||||
|
@ -20,11 +20,11 @@ import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import io.swagger.client.model.Animal;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.threeten.bp.OffsetDateTime;
|
||||
import javax.validation.constraints.*;
|
||||
import javax.validation.Valid;
|
||||
|
||||
@ -87,7 +87,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
|
||||
|
||||
public MixedPropertiesAndAdditionalPropertiesClass putMapItem(String key, Animal mapItem) {
|
||||
if (this.map == null) {
|
||||
this.map = new HashMap<String, Animal>();
|
||||
this.map = new HashMap<>();
|
||||
}
|
||||
this.map.put(key, mapItem);
|
||||
return this;
|
||||
|
@ -19,7 +19,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import org.threeten.bp.OffsetDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import javax.validation.constraints.*;
|
||||
import javax.validation.Valid;
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class Pet {
|
||||
private String name = null;
|
||||
|
||||
@JsonProperty("photoUrls")
|
||||
private List<String> photoUrls = new ArrayList<String>();
|
||||
private List<String> photoUrls = new ArrayList<>();
|
||||
|
||||
@JsonProperty("tags")
|
||||
private List<Tag> tags = null;
|
||||
@ -173,7 +173,7 @@ public class Pet {
|
||||
|
||||
public Pet addTagsItem(Tag tagsItem) {
|
||||
if (this.tags == null) {
|
||||
this.tags = new ArrayList<Tag>();
|
||||
this.tags = new ArrayList<>();
|
||||
}
|
||||
this.tags.add(tagsItem);
|
||||
return this;
|
||||
|
Loading…
Reference in New Issue
Block a user