Skip to content

Commit 2cdc454

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 c159239 commit 2cdc454

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
@@ -123,6 +123,19 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
123123
}
124124
}
125125
}
126+
// Let parent know about all its children
127+
for (String name : allModels.keySet()) {
128+
CodegenModel cm = allModels.get(name);
129+
if (cm.parent == null) continue;
130+
CodegenModel parent = allModels.get(cm.parent);
131+
if (parent.children == null) {
132+
parent.children = new ArrayList<CodegenModel>();
133+
} else {
134+
parent.children.get(parent.children.size() - 1).hasMoreChildren = Boolean.TRUE;
135+
}
136+
parent.children.add(cm);
137+
parent.hasChildren = true;
138+
}
126139
}
127140
return objs;
128141
}

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
@@ -47,6 +47,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
4747

4848
public JavaClientCodegen() {
4949
super();
50+
supportsInheritance = true;
5051
outputFolder = "generated-code" + File.separator + "java";
5152
modelTemplateFiles.put("model.mustache", ".java");
5253
apiTemplateFiles.put("api.mustache", ".java");
@@ -238,6 +239,8 @@ public void processOpts() {
238239
importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
239240
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
240241
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");
242+
importMapping.put("JsonSubTypes", "com.fasterxml.jackson.annotation.JsonSubTypes");
243+
importMapping.put("JsonTypeInfo", "com.fasterxml.jackson.annotation.JsonTypeInfo");
241244
importMapping.put("JsonValue", "com.fasterxml.jackson.annotation.JsonValue");
242245
importMapping.put("Objects", "java.util.Objects");
243246
importMapping.put("StringUtil", invokerPackage + ".StringUtil");
@@ -542,6 +545,10 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
542545
if(codegenModel.description != null) {
543546
codegenModel.imports.add("ApiModel");
544547
}
548+
if (codegenModel.discriminator != null) {
549+
codegenModel.imports.add("JsonSubTypes");
550+
codegenModel.imports.add("JsonTypeInfo");
551+
}
545552
if (allDefinitions != null && codegenModel != null && codegenModel.parentSchema != null && codegenModel.hasEnums) {
546553
final Model parentModel = allDefinitions.get(codegenModel.parentSchema);
547554
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)