Fix swagger.json supporting file for the node.js server (fixes #652)

This commit is contained in:
Hugo Wood 2015-08-11 14:37:00 +02:00
parent be63776162
commit d93b404b23
2 changed files with 59 additions and 3 deletions

View File

@ -9,11 +9,19 @@ import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile; import io.swagger.codegen.SupportingFile;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig { public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig {
protected String apiVersion = "1.0.0"; protected String apiVersion = "1.0.0";
@ -197,4 +205,50 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
} }
return objs; return objs;
} }
@SuppressWarnings("unchecked")
private Map<String, Object> getOperations(Map<String, Object> objs) {
Map<String, Object> apiInfo = (Map<String, Object>) objs.get("apiInfo");
List<Map<String, Object>> apis = (List<Map<String, Object>>) apiInfo.get("apis");
Map<String, Object> api = apis.get(0);
return (Map<String, Object>) api.get("operations");
}
private List<Map<String, Object>> sortOperationsByPath(List<CodegenOperation> ops) {
Multimap<String, CodegenOperation> opsByPath = ArrayListMultimap.create();
for (CodegenOperation op : ops) {
opsByPath.put(op.path, op);
}
List<Map<String, Object>> opsByPathList = new ArrayList<Map<String, Object>>();
for (Entry<String, Collection<CodegenOperation>> entry : opsByPath.asMap().entrySet()) {
Map<String, Object> opsByPathEntry = new HashMap<String, Object>();
opsByPathList.add(opsByPathEntry);
opsByPathEntry.put("path", entry.getKey());
opsByPathEntry.put("operation", entry.getValue());
List<CodegenOperation> operationsForThisPath = Lists.newArrayList(entry.getValue());
operationsForThisPath.get(operationsForThisPath.size() - 1).hasMore = null;
if (opsByPathList.size() < opsByPath.asMap().size()) {
opsByPathEntry.put("hasMore", "true");
}
}
return opsByPathList;
}
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Map<String, Object> operations = getOperations(objs);
if (operations != null) {
@SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
List<Map<String, Object>> opsByPathList = sortOperationsByPath(ops);
operations.put("operationsByPath", opsByPathList);
}
return super.postProcessSupportingFileData(objs);
}
} }

View File

@ -12,8 +12,9 @@
"paths": { "paths": {
{{#apis}} {{#apis}}
{{#operations}} {{#operations}}
{{#operation}} {{#operationsByPath}}
"{{{path}}}": { "{{{path}}}": {
{{#operation}}
"{{httpMethod}}": { "{{httpMethod}}": {
"summary": "{{summary}}", "summary": "{{summary}}",
"description":"{{notes}}", "description":"{{notes}}",
@ -29,9 +30,10 @@
{{#hasMore}},{{/hasMore}} {{#hasMore}},{{/hasMore}}
{{/responses}} {{/responses}}
} }
} } {{#hasMore}},{{/hasMore}}
{{/operation}}
} {{#hasMore}},{{/hasMore}} } {{#hasMore}},{{/hasMore}}
{{/operation}} {{/operationsByPath}}
{{#hasMore}},{{/hasMore}} {{#hasMore}},{{/hasMore}}
{{/operations}} {{/operations}}
{{/apis}} {{/apis}}