Skip to content

Commit e867458

Browse files
committed
Fix issue #2449: SubClass annotations are missing from the base class.
Added a children property to CodegenModel and write out the necessary annotations.
1 parent 1ad14b7 commit e867458

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class CodegenModel {
1111
// References to parent and interface CodegenModels. Only set when code generator supports inheritance.
1212
public CodegenModel parentModel;
1313
public List<CodegenModel> interfaceModels;
14+
public List<CodegenModel> children;
1415

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

2829
public Set<String> imports = new TreeSet<String>();
29-
public Boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum;
30+
public Boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, hasChildren, hasMoreChildren;
3031
public ExternalDocs externalDocs;
3132

3233
public Map<String, Object> vendorExtensions;

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
156156
}
157157
}
158158
}
159+
// Let parent know about all its children
160+
for (String name : allModels.keySet()) {
161+
CodegenModel cm = allModels.get(name);
162+
if (cm.parent == null) continue;
163+
CodegenModel parent = allModels.get(cm.parent);
164+
if (parent.children == null) {
165+
parent.children = new ArrayList<CodegenModel>();
166+
} else {
167+
parent.children.get(parent.children.size() - 1).hasMoreChildren = Boolean.TRUE;
168+
}
169+
parent.children.add(cm);
170+
parent.hasChildren = true;
171+
}
159172
}
160173
return objs;
161174
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
4545

4646
public JavaClientCodegen() {
4747
super();
48+
supportsInheritance = true;
4849
outputFolder = "generated-code" + File.separator + "java";
4950
modelTemplateFiles.put("model.mustache", ".java");
5051
apiTemplateFiles.put("api.mustache", ".java");
@@ -235,6 +236,8 @@ public void processOpts() {
235236
importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
236237
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
237238
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");
239+
importMapping.put("JsonSubTypes", "com.fasterxml.jackson.annotation.JsonSubTypes");
240+
importMapping.put("JsonTypeInfo", "com.fasterxml.jackson.annotation.JsonTypeInfo");
238241
importMapping.put("JsonValue", "com.fasterxml.jackson.annotation.JsonValue");
239242
importMapping.put("Objects", "java.util.Objects");
240243
importMapping.put("StringUtil", invokerPackage + ".StringUtil");
@@ -539,6 +542,10 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
539542
if(codegenModel.description != null) {
540543
codegenModel.imports.add("ApiModel");
541544
}
545+
if (codegenModel.discriminator != null) {
546+
codegenModel.imports.add("JsonSubTypes");
547+
codegenModel.imports.add("JsonTypeInfo");
548+
}
542549
if (allDefinitions != null && codegenModel != null && codegenModel.parentSchema != null && codegenModel.hasEnums) {
543550
final Model parentModel = allDefinitions.get(codegenModel.parentSchema);
544551
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);

modules/swagger-codegen/src/main/resources/Java/pojo.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{#description}}@ApiModel(description = "{{{description}}}"){{/description}}
22
{{>generatedAnnotation}}
3+
{{#hasChildren}}{{>typeInfoAnnotation}}{{/hasChildren}}
34
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
45
{{#vars}}{{#isEnum}}
56

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "{{discriminator}}" )
2+
@JsonSubTypes({
3+
{{#children}}@JsonSubTypes.Type(value = {{name}}.class, name = "{{name}}"){{#hasMoreChildren}},
4+
{{/hasMoreChildren}}{{/children}}
5+
})
6+

0 commit comments

Comments
 (0)