|
15 | 15 | import io.swagger.codegen.v3.SupportingFile;
|
16 | 16 | import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
|
17 | 17 | import io.swagger.codegen.v3.generators.features.OptionalFeatures;
|
| 18 | +import io.swagger.codegen.v3.generators.util.OpenAPIUtil; |
18 | 19 | import io.swagger.codegen.v3.utils.URLPathUtil;
|
19 | 20 | import io.swagger.v3.oas.models.OpenAPI;
|
20 | 21 | import io.swagger.v3.oas.models.Operation;
|
|
27 | 28 | import java.io.IOException;
|
28 | 29 | import java.io.Writer;
|
29 | 30 | import java.net.URL;
|
30 |
| -import java.util.ArrayList; |
31 |
| -import java.util.Arrays; |
32 |
| -import java.util.HashMap; |
33 |
| -import java.util.List; |
34 |
| -import java.util.Map; |
| 31 | +import java.util.*; |
35 | 32 | import java.util.regex.Matcher;
|
36 | 33 | import java.util.stream.Collectors;
|
37 | 34 |
|
@@ -792,6 +789,60 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
|
792 | 789 | }
|
793 | 790 | }
|
794 | 791 |
|
| 792 | + @Override |
| 793 | + public Map<String, Object> postProcessAllModels(Map<String, Object> objs) { |
| 794 | + Map<String, Object> allProcessedModels = super.postProcessAllModels(objs); |
| 795 | + |
| 796 | + List<Object> allModels = new ArrayList<Object>(); |
| 797 | + for (String name: allProcessedModels.keySet()) { |
| 798 | + Map<String, Object> models = (Map<String, Object>)allProcessedModels.get(name); |
| 799 | + try { |
| 800 | + allModels.add(((List<Object>) models.get("models")).get(0)); |
| 801 | + } catch (Exception e){ |
| 802 | + e.printStackTrace(); |
| 803 | + } |
| 804 | + } |
| 805 | + |
| 806 | + additionalProperties.put("parent", modelInheritanceSupport(allModels)); |
| 807 | + |
| 808 | + return allProcessedModels; |
| 809 | + } |
| 810 | + |
| 811 | + protected List<Map<String, Object>> modelInheritanceSupport(List<?> allModels) { |
| 812 | + Map<CodegenModel, List<CodegenModel>> byParent = new LinkedHashMap<>(); |
| 813 | + for (Object model : allModels) { |
| 814 | + Map entry = (Map) model; |
| 815 | + CodegenModel parent = ((CodegenModel)entry.get("model")).parentModel; |
| 816 | + if(null!= parent) { |
| 817 | + byParent.computeIfAbsent(parent, k -> new LinkedList<>()).add((CodegenModel)entry.get("model")); |
| 818 | + } |
| 819 | + } |
| 820 | + |
| 821 | + List<Map<String, Object>> parentsList = new ArrayList<>(); |
| 822 | + for (Map.Entry<CodegenModel, List<CodegenModel>> parentModelEntry : byParent.entrySet()) { |
| 823 | + CodegenModel parentModel = parentModelEntry.getKey(); |
| 824 | + List<Map<String, Object>> childrenList = new ArrayList<>(); |
| 825 | + Map<String, Object> parent = new HashMap<>(); |
| 826 | + parent.put("classname", parentModel.classname); |
| 827 | + List<CodegenModel> childrenModels = byParent.get(parentModel); |
| 828 | + for (CodegenModel model : childrenModels) { |
| 829 | + Map<String, Object> child = new HashMap<>(); |
| 830 | + child.put("name", model.name); |
| 831 | + child.put("classname", model.classname); |
| 832 | + childrenList.add(child); |
| 833 | + } |
| 834 | + parent.put("children", childrenList); |
| 835 | + parent.put("discriminator", parentModel.discriminator); |
| 836 | + if(parentModel.discriminator != null && parentModel.discriminator.getMapping() != null) |
| 837 | + { |
| 838 | + parentModel.discriminator.getMapping().replaceAll((key, value) -> OpenAPIUtil.getSimpleRef(value)); |
| 839 | + } |
| 840 | + parentsList.add(parent); |
| 841 | + } |
| 842 | + |
| 843 | + return parentsList; |
| 844 | + } |
| 845 | + |
795 | 846 | @Override
|
796 | 847 | public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
|
797 | 848 | objs = super.postProcessModelsEnum(objs);
|
|
0 commit comments