Skip to content

Commit

Permalink
#3904 Inheritance support java client retrofit gson (#4729)
Browse files Browse the repository at this point in the history
* merge with master

* remove zoo specific artifact names

* fix duplicate doublequote

* #3904 add samples for retrofit2

* #3904 clean json field retrieval

* #3904 allow non-abstract parent class
  • Loading branch information
konopski authored and wing328 committed Oct 8, 2017
1 parent 1dc3fb4 commit 73b38dd
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ public List<File> generate() {
return files;
}


private File processTemplateToFile(Map<String, Object> templateData, String templateName, String outputFilename) throws IOException {
String adjustedOutputFilename = outputFilename.replaceAll("//", "/").replace('/', File.separatorChar);
if (ignoreProcessor.allowsFile(new File(adjustedOutputFilename))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.swagger.codegen.languages;

import com.google.common.collect.LinkedListMultimap;
import io.swagger.codegen.*;
import io.swagger.codegen.languages.features.BeanValidationFeatures;
import io.swagger.codegen.languages.features.GzipFeatures;
Expand Down Expand Up @@ -432,6 +433,24 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
Map<String, Object> allProcessedModels = super.postProcessAllModels(objs);
if(!additionalProperties.containsKey("gsonFactoryMethod")) {
List<Object> allModels = new ArrayList<Object>();
for (String name: allProcessedModels.keySet()) {
Map<String, Object> models = (Map<String, Object>)allProcessedModels.get(name);
try {
allModels.add(((List<Object>) models.get("models")).get(0));
} catch (Exception e){
e.printStackTrace();
}
}
additionalProperties.put("parent", modelInheritanceSupportInGson(allModels));
}
return allProcessedModels;
}

@Override
public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
objs = super.postProcessModelsEnum(objs);
Expand All @@ -454,6 +473,34 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
return objs;
}

private List<Map<String, Object>> modelInheritanceSupportInGson(List<?> allModels) {
LinkedListMultimap<CodegenModel, CodegenModel> byParent = LinkedListMultimap.create();
for (Object m : allModels) {
Map entry = (Map) m;
CodegenModel parent = ((CodegenModel)entry.get("model")).parentModel;
if(null!= parent) {
byParent.put(parent, ((CodegenModel)entry.get("model")));
}
}
List<Map<String, Object>> parentsList = new ArrayList<>();
for (CodegenModel parentModel : byParent.keySet()) {
List<Map<String, Object>> childrenList = new ArrayList<>();
Map<String, Object> parent = new HashMap<>();
parent.put("classname", parentModel.classname);
List<CodegenModel> childrenModels = byParent.get(parentModel);
for (CodegenModel model : childrenModels) {
Map<String, Object> child = new HashMap<>();
child.put("name", model.name);
child.put("classname", model.classname);
childrenList.add(child);
}
parent.put("children", childrenList);
parent.put("discriminator", parentModel.discriminator);
parentsList.add(parent);
}
return parentsList;
}

public void setUseRxJava(boolean useRxJava) {
this.useRxJava = useRxJava;
doNotUseRx = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package {{invokerPackage}};

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonElement;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
Expand Down Expand Up @@ -39,6 +40,7 @@ import java.time.format.DateTimeFormatter;
{{/java8}}
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.HashMap;

public class ApiClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ 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 com.google.gson.JsonElement;
import io.gsonfire.GsonFireBuilder;
import io.gsonfire.TypeSelector;
{{#joda}}
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
Expand All @@ -22,6 +25,8 @@ import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;
{{/threetenbp}}

import {{modelPackage}}.*;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
Expand All @@ -34,6 +39,8 @@ import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
{{/java8}}
import java.util.Date;
import java.util.Map;
import java.util.HashMap;

public class JSON {
private Gson gson;
Expand All @@ -48,8 +55,45 @@ public class JSON {
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
{{/jsr310}}

public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
{{#parent}}
.registerTypeSelector({{classname}}.class, new TypeSelector() {
@Override
public Class getClassForElement(JsonElement readElement) {
Map classByDiscriminatorValue = new HashMap();
{{#children}}
classByDiscriminatorValue.put("{{name}}".toUpperCase(), {{classname}}.class);
{{/children}}
classByDiscriminatorValue.put("{{classname}}".toUpperCase(), {{classname}}.class);
return getClassByDiscriminator(
classByDiscriminatorValue,
getDiscriminatorValue(readElement, "{{discriminator}}"));
}
})
{{/parent}}
;
return fireBuilder.createGsonBuilder();
}

private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
JsonElement element = readElement.getAsJsonObject().get(discriminatorField);
if(null == element) {
throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">");
}
return element.getAsString();
}

private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase());
if(null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
return clazz;
}

public JSON() {
gson = new GsonBuilder()
gson = createGson()
.registerTypeAdapter(Date.class, dateTypeAdapter)
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
{{#joda}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ ext {
{{#threetenbp}}
threetenbp_version = "1.3.5"
{{/threetenbp}}
json_fire_version = "1.8.0"
}

dependencies {
Expand All @@ -142,6 +143,7 @@ dependencies {
{{/useRxJava2}}
compile "io.swagger:swagger-annotations:$swagger_annotations_version"
compile "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:$oltu_version"
compile "io.gsonfire:gson-fire:$json_fire_version"
{{#joda}}
compile "joda-time:joda-time:$jodatime_version"
{{/joda}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ lazy val root = (project in file(".")).
{{#threetenbp}}
"org.threeten" % "threetenbp" % "1.3.5" % "compile",
{{/threetenbp}}
"io.gsonfire" % "gson-fire" % "1.8.0" % "compile",
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.11" % "test"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@
<artifactId>org.apache.oltu.oauth2.client</artifactId>
<version>${oltu-version}</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>${gson-fire-version}</version>
</dependency>
{{#joda}}
<dependency>
<groupId>joda-time</groupId>
Expand Down Expand Up @@ -300,6 +305,7 @@
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
<swagger-core-version>1.5.15</swagger-core-version>
{{#usePlayWS}}
{{#play24}}
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/java/retrofit2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ ext {
swagger_annotations_version = "1.5.15"
junit_version = "4.12"
threetenbp_version = "1.3.5"
json_fire_version = "1.8.0"
}

dependencies {
Expand All @@ -107,6 +108,7 @@ 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 "io.gsonfire:gson-fire:$json_fire_version"
compile "org.threeten:threetenbp:$threetenbp_version"

testCompile "junit:junit:$junit_version"
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/retrofit2/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ lazy val root = (project in file(".")).
"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",
"io.gsonfire" % "gson-fire" % "1.8.0" % "compile",
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.11" % "test"
)
Expand Down
6 changes: 6 additions & 0 deletions samples/client/petstore/java/retrofit2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@
<artifactId>org.apache.oltu.oauth2.client</artifactId>
<version>${oltu-version}</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>${gson-fire-version}</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
Expand All @@ -215,6 +220,7 @@
<java.version>1.7</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
<swagger-core-version>1.5.15</swagger-core-version>
<retrofit-version>2.3.0</retrofit-version>
<threetenbp-version>1.3.5</threetenbp-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@
import java.text.DateFormat;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.HashMap;

public class ApiClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@
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;
import java.text.DateFormat;
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;
Expand All @@ -39,8 +46,42 @@ 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);
classByDiscriminatorValue.put("Animal".toUpperCase(), Animal.class);
return getClassByDiscriminator(
classByDiscriminatorValue,
getDiscriminatorValue(readElement, "className"));
}
})
;
return fireBuilder.createGsonBuilder();
}

private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
JsonElement element = readElement.getAsJsonObject().get(discriminatorField);
if(null == element) {
throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">");
}
return element.getAsString();
}

private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase());
if(null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
return clazz;
}

public JSON() {
gson = new GsonBuilder()
gson = createGson()
.registerTypeAdapter(Date.class, dateTypeAdapter)
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
Expand Down

0 comments on commit 73b38dd

Please sign in to comment.