From f79886910f8e021ebd1bc9ba5bead5698eab689f Mon Sep 17 00:00:00 2001 From: xhh Date: Mon, 7 Mar 2016 20:28:19 +0800 Subject: [PATCH] Sort API and models by name --- .../io/swagger/codegen/DefaultGenerator.java | 107 +++++++++--------- 1 file changed, 53 insertions(+), 54 deletions(-) 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 c966cf6ba6..6ccf8670a6 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.io.*; 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 class DefaultGenerator extends AbstractGenerator implements Generator { 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 class DefaultGenerator extends AbstractGenerator implements Generator { 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 class DefaultGenerator extends AbstractGenerator implements Generator { } } } + if (System.getProperty("debugOperations") != null) { LOGGER.info("############ Operation info ############"); Json.prettyPrint(allOperations); @@ -513,54 +558,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { } } - private static List 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);