Skip to content
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

#602 Generated code does not implement model inheritance correctly #603

Merged
merged 1 commit into from
Feb 21, 2020
Merged
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 @@ -15,6 +15,7 @@
import io.swagger.codegen.v3.SupportingFile;
import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
import io.swagger.codegen.v3.generators.features.OptionalFeatures;
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
import io.swagger.codegen.v3.utils.URLPathUtil;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand All @@ -27,11 +28,7 @@
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -792,6 +789,60 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}
}

@Override
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
Map<String, Object> allProcessedModels = super.postProcessAllModels(objs);

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", modelInheritanceSupport(allModels));

return allProcessedModels;
}

protected List<Map<String, Object>> modelInheritanceSupport(List<?> allModels) {
Map<CodegenModel, List<CodegenModel>> byParent = new LinkedHashMap<>();
for (Object model : allModels) {
Map entry = (Map) model;
CodegenModel parent = ((CodegenModel)entry.get("model")).parentModel;
if(null!= parent) {
byParent.computeIfAbsent(parent, k -> new LinkedList<>()).add((CodegenModel)entry.get("model"));
}
}

List<Map<String, Object>> parentsList = new ArrayList<>();
for (Map.Entry<CodegenModel, List<CodegenModel>> parentModelEntry : byParent.entrySet()) {
CodegenModel parentModel = parentModelEntry.getKey();
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);
if(parentModel.discriminator != null && parentModel.discriminator.getMapping() != null)
{
parentModel.discriminator.getMapping().replaceAll((key, value) -> OpenAPIUtil.getSimpleRef(value));
}
parentsList.add(parent);
}

return parentsList;
}

@Override
public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
objs = super.postProcessModelsEnum(objs);
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/handlebars/JavaSpring/model.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package {{package}};

{{^x-is-composed-model}}
{{^isComposedModel}}
import java.util.Objects;
{{#imports}}import {{import}};
{{/imports}}
Expand All @@ -21,20 +21,20 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
{{#withXml}}
import javax.xml.bind.annotation.*;
{{/withXml}}
{{/x-is-composed-model}}
{{/isComposedModel}}

{{#models}}
{{#model}}
{{#vendorExtensions.x-is-composed-model}}
{{#isComposedModel}}
{{>interface}}
{{/vendorExtensions.x-is-composed-model}}
{{^vendorExtensions.x-is-composed-model}}
{{/isComposedModel}}
{{^isComposedModel}}
{{#isEnum}}
{{>enumOuterClass}}
{{/isEnum}}
{{^isEnum}}
{{>pojo}}
{{/isEnum}}
{{/vendorExtensions.x-is-composed-model}}
{{/isComposedModel}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{{#jackson}}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "{{discriminator.propertyName}}", visible = true )
@JsonSubTypes({
{{#children}}
@JsonSubTypes.Type(value = {{classname}}.class, name = "{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"),
{{/children}}
}){{/jackson}}
{{#if discriminator.mapping}}
{{#each discriminator.mapping}}
@JsonSubTypes.Type(value = {{this}}.class, name = "{{@key}}"),
{{/each}}
{{else}}
{{#children}}
@JsonSubTypes.Type(value = {{classname}}.class, name = "{{name}}"),
{{/children}}
{{/if}}
})
{{/jackson}}