diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/ApiClient.java index 9de69dde1e8..d1a5c2102a8 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/ApiClient.java @@ -2,6 +2,7 @@ import com.google.gson.Gson; import com.google.gson.JsonParseException; +import com.google.gson.JsonElement; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.RequestBody; @@ -25,6 +26,7 @@ import java.text.DateFormat; import java.util.LinkedHashMap; import java.util.Map; +import java.util.HashMap; public class ApiClient { diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/JSON.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/JSON.java index 590495331b6..9fadecfb138 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/JSON.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/JSON.java @@ -20,10 +20,15 @@ import com.google.gson.internal.bind.util.ISO8601Utils; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; +import com.google.gson.JsonElement; +import io.gsonfire.GsonFireBuilder; +import io.gsonfire.TypeSelector; import org.threeten.bp.LocalDate; import org.threeten.bp.OffsetDateTime; import org.threeten.bp.format.DateTimeFormatter; +import io.swagger.client.model.*; + import java.io.IOException; import java.io.StringReader; import java.lang.reflect.Type; @@ -31,6 +36,8 @@ import java.text.ParseException; import java.text.ParsePosition; import java.util.Date; +import java.util.Map; +import java.util.HashMap; public class JSON { private Gson gson; @@ -39,8 +46,25 @@ public class JSON { private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + public static GsonBuilder createGson() { + GsonFireBuilder fireBuilder = new GsonFireBuilder() + .registerTypeSelector(Animal.class, new TypeSelector() { + @Override + public Class getClassForElement(JsonElement readElement) { + Map classByDiscriminatorValue = new HashMap(); + classByDiscriminatorValue.put("Cat".toUpperCase(), Cat.class); + classByDiscriminatorValue.put("Dog".toUpperCase(), Dog.class); + String discriminatorField = "className"; + String discriminatorValue = readElement.getAsJsonObject().get(discriminatorField).getAsString(); + return (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase()); + } + }) + ; + return fireBuilder.createGsonBuilder(); + } + public JSON() { - gson = new GsonBuilder() + gson = createGson() .registerTypeAdapter(Date.class, dateTypeAdapter) .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter) .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter) diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeApi.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeApi.java index 70293f5985b..d169b8111b7 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeApi.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeApi.java @@ -6,6 +6,7 @@ import retrofit2.http.*; import okhttp3.RequestBody; +import okhttp3.ResponseBody; import java.math.BigDecimal; import io.swagger.client.model.Client; diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java index 8435dbc4cd6..c31c31c9233 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java @@ -6,6 +6,7 @@ import retrofit2.http.*; import okhttp3.RequestBody; +import okhttp3.ResponseBody; import io.swagger.client.model.Client; diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/PetApi.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/PetApi.java index 06dff092eec..8d48189ce93 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/PetApi.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/PetApi.java @@ -6,6 +6,7 @@ import retrofit2.http.*; import okhttp3.RequestBody; +import okhttp3.ResponseBody; import java.io.File; import io.swagger.client.model.ModelApiResponse; diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/StoreApi.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/StoreApi.java index 0795776d1fb..99367e1a0c3 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/StoreApi.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/StoreApi.java @@ -6,6 +6,7 @@ import retrofit2.http.*; import okhttp3.RequestBody; +import okhttp3.ResponseBody; import io.swagger.client.model.Order; diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/UserApi.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/UserApi.java index 8610a28e3a6..ab2074c4e80 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/UserApi.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/api/UserApi.java @@ -6,6 +6,7 @@ import retrofit2.http.*; import okhttp3.RequestBody; +import okhttp3.ResponseBody; import io.swagger.client.model.User;