Skip to content

Fix issue #2449: SubClass annotations are missing from the base class. #2467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -123,6 +123,19 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
}
}
}
// Let parent know about all its children
for (String name : allModels.keySet()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (CodegenModel cm : allModels.values()) ?

CodegenModel cm = allModels.get(name);
if (cm.parent == null) continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continue is ugly here.
It would be better to put the folllowing code in a if (cm.parent != null) loop.

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

public JavaClientCodegen() {
super();
supportsInheritance = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be done at the moment. See : #1120 (comment)
Does it work if you keep supportsInheritance to false ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I have a PR on the swagger-parser that would make supportsInheritance work : swagger-api/swagger-parser#246

outputFolder = "generated-code" + File.separator + "java";
modelTemplateFiles.put("model.mustache", ".java");
apiTemplateFiles.put("api.mustache", ".java");
Expand Down Expand Up @@ -238,6 +239,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 @@ -542,6 +545,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}}
})