Skip to content

Commit

Permalink
Fix issue swagger-api#2449: SubClass annotations are missing from the…
Browse files Browse the repository at this point in the history
… base class.

Added a children property to CodegenModel and write out the necessary annotations.
  • Loading branch information
Yael Aharon committed Mar 29, 2016
1 parent f0c3870 commit ff0cefe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class CodegenModel {
// References to parent and interface CodegenModels. Only set when code generator supports inheritance.
public CodegenModel parentModel;
public List<CodegenModel> interfaceModels;
public List<CodegenModel> children;

public String name, classname, description, classVarName, modelJson, dataType;
public String classFilename; // store the class file name, mainly used for import
Expand All @@ -26,7 +27,7 @@ public class CodegenModel {
public Set<String> allMandatory;

public Set<String> imports = new TreeSet<String>();
public Boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum;
public Boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, hasChildren, hasMoreChildren;
public ExternalDocs externalDocs;

public Map<String, Object> vendorExtensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
}
}
}
// Let parent know about all its children
for (String name : allModels.keySet()) {
CodegenModel cm = allModels.get(name);
if (cm.parent == null) continue;
CodegenModel parent = allModels.get(cm.parent);
if (parent.children == null) {
parent.children = new ArrayList<CodegenModel>();
} else {
parent.children.get(parent.children.size() - 1).hasMoreChildren = Boolean.TRUE;
}
parent.children.add(cm);
parent.hasChildren = true;
}
}
return objs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {

public JavaClientCodegen() {
super();
supportsInheritance = true;
outputFolder = "generated-code" + File.separator + "java";
modelTemplateFiles.put("model.mustache", ".java");
apiTemplateFiles.put("api.mustache", ".java");
Expand Down Expand Up @@ -235,6 +236,8 @@ public void processOpts() {
importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");
importMapping.put("JsonSubTypes", "com.fasterxml.jackson.annotation.JsonSubTypes");
importMapping.put("JsonTypeInfo", "com.fasterxml.jackson.annotation.JsonTypeInfo");
importMapping.put("JsonValue", "com.fasterxml.jackson.annotation.JsonValue");
importMapping.put("Objects", "java.util.Objects");
importMapping.put("StringUtil", invokerPackage + ".StringUtil");
Expand Down Expand Up @@ -539,6 +542,10 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
if(codegenModel.description != null) {
codegenModel.imports.add("ApiModel");
}
if (codegenModel.discriminator != null) {
codegenModel.imports.add("JsonSubTypes");
codegenModel.imports.add("JsonTypeInfo");
}
if (allDefinitions != null && codegenModel != null && codegenModel.parentSchema != null && codegenModel.hasEnums) {
final Model parentModel = allDefinitions.get(codegenModel.parentSchema);
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{#description}}@ApiModel(description = "{{{description}}}"){{/description}}
{{>generatedAnnotation}}
{{#hasChildren}}{{>typeInfoAnnotation}}{{/hasChildren}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#vars}}{{#isEnum}}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "{{discriminator}}" )
@JsonSubTypes({
{{#children}}@JsonSubTypes.Type(value = {{name}}.class, name = "{{name}}"){{#hasMoreChildren}},
{{/hasMoreChildren}}{{/children}}
})

0 comments on commit ff0cefe

Please sign in to comment.