diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index c966cf6ba66..6ccf8670a68 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -16,6 +16,7 @@ import java.util.*; import static org.apache.commons.lang3.StringUtils.isNotEmpty; +import org.apache.commons.lang3.ObjectUtils; public class DefaultGenerator extends AbstractGenerator implements Generator { protected Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class); @@ -167,26 +168,63 @@ public List generate() { List allModels = new ArrayList(); // models - Map definitions = swagger.getDefinitions(); + final Map definitions = swagger.getDefinitions(); if (definitions != null) { - List sortedModelKeys = sortModelsByInheritance(definitions); + Set modelKeys = definitions.keySet(); if(generateModels) { if(modelsToGenerate != null && modelsToGenerate.size() > 0) { - List updatedKeys = new ArrayList(); - for(String m : sortedModelKeys) { + Set updatedKeys = new HashSet(); + for(String m : modelKeys) { if(modelsToGenerate.contains(m)) { updatedKeys.add(m); } } - sortedModelKeys = updatedKeys; + modelKeys = updatedKeys; } // store all processed models - Map allProcessedModels = new HashMap(); + Map allProcessedModels = new TreeMap(new Comparator() { + @Override + public int compare(String o1, String o2) { + Model model1 = definitions.get(o1); + Model model2 = definitions.get(o2); + + int model1InheritanceDepth = getInheritanceDepth(model1); + int model2InheritanceDepth = getInheritanceDepth(model2); + + if (model1InheritanceDepth == model2InheritanceDepth) { + return ObjectUtils.compare(config.toModelName(o1), config.toModelName(o2)); + } else if (model1InheritanceDepth > model2InheritanceDepth) { + return 1; + } else { + return -1; + } + } + + private int getInheritanceDepth(Model model) { + int inheritanceDepth = 0; + Model parent = getParent(model); + + while (parent != null) { + inheritanceDepth++; + parent = getParent(parent); + } + + return inheritanceDepth; + } + + private Model getParent(Model model) { + if (model instanceof ComposedModel) { + return definitions.get(((ComposedModel) model).getParent().getReference()); + } + + return null; + } + }); // process models only - for (String name : sortedModelKeys) { + for (String name : modelKeys) { try { //don't generate models that have an import mapping if(config.importMapping().containsKey(name)) { @@ -289,6 +327,12 @@ public Reader getTemplate(String name) { for (String tag : paths.keySet()) { try { List ops = paths.get(tag); + Collections.sort(ops, new Comparator() { + @Override + public int compare(CodegenOperation one, CodegenOperation another) { + return ObjectUtils.compare(one.operationId, another.operationId); + } + }); Map operation = processOperations(config, tag, ops); operation.put("basePath", basePath); @@ -373,6 +417,7 @@ public Reader getTemplate(String name) { } } } + if (System.getProperty("debugOperations") != null) { LOGGER.info("############ Operation info ############"); Json.prettyPrint(allOperations); @@ -513,54 +558,8 @@ private static void processMimeTypes(List mimeTypeList, Map sortModelsByInheritance(final Map definitions) { - List sortedModelKeys = new ArrayList(definitions.keySet()); - Comparator cmp = new Comparator() { - @Override - public int compare(String o1, String o2) { - Model model1 = definitions.get(o1); - Model model2 = definitions.get(o2); - - int model1InheritanceDepth = getInheritanceDepth(model1); - int model2InheritanceDepth = getInheritanceDepth(model2); - - if (model1InheritanceDepth == model2InheritanceDepth) { - return 0; - } else if (model1InheritanceDepth > model2InheritanceDepth) { - return 1; - } else { - return -1; - } - } - - private int getInheritanceDepth(Model model) { - int inheritanceDepth = 0; - Model parent = getParent(model); - - while (parent != null) { - inheritanceDepth++; - parent = getParent(parent); - } - - return inheritanceDepth; - } - - private Model getParent(Model model) { - if (model instanceof ComposedModel) { - return definitions.get(((ComposedModel) model).getParent().getReference()); - } - - return null; - } - }; - - Collections.sort(sortedModelKeys, cmp); - - return sortedModelKeys; - } - public Map> processPaths(Map paths) { - Map> ops = new HashMap>(); + Map> ops = new TreeMap>(); for (String resourcePath : paths.keySet()) { Path path = paths.get(resourcePath);