Merge pull request #1135 from restlet/fix/nodejs_server_swagger_invalid_json

[Node.js] fix invalid JSON being generated in swagger.json
This commit is contained in:
wing328 2015-08-28 18:31:39 +08:00
commit 28579cee03
2 changed files with 9 additions and 10 deletions

View File

@ -207,11 +207,14 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> getOperations(Map<String, Object> objs) { private List<Map<String, Object>> getOperations(Map<String, Object> objs) {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
Map<String, Object> apiInfo = (Map<String, Object>) objs.get("apiInfo"); Map<String, Object> apiInfo = (Map<String, Object>) objs.get("apiInfo");
List<Map<String, Object>> apis = (List<Map<String, Object>>) apiInfo.get("apis"); List<Map<String, Object>> apis = (List<Map<String, Object>>) apiInfo.get("apis");
Map<String, Object> api = apis.get(0); for (Map<String, Object> api : apis) {
return (Map<String, Object>) api.get("operations"); result.add((Map<String, Object>) api.get("operations"));
}
return result;
} }
private List<Map<String, Object>> sortOperationsByPath(List<CodegenOperation> ops) { private List<Map<String, Object>> sortOperationsByPath(List<CodegenOperation> ops) {
@ -221,7 +224,7 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
opsByPath.put(op.path, op); opsByPath.put(op.path, op);
} }
List<Map<String, Object>> opsByPathList = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> opsByPathList = new ArrayList<Map<String, Object>>();
for (Entry<String, Collection<CodegenOperation>> entry : opsByPath.asMap().entrySet()) { for (Entry<String, Collection<CodegenOperation>> entry : opsByPath.asMap().entrySet()) {
Map<String, Object> opsByPathEntry = new HashMap<String, Object>(); Map<String, Object> opsByPathEntry = new HashMap<String, Object>();
opsByPathList.add(opsByPathEntry); opsByPathList.add(opsByPathEntry);
@ -239,16 +242,13 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
@Override @Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) { public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Map<String, Object> operations = getOperations(objs); for (Map<String, Object> operations : getOperations(objs)) {
if (operations != null) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
List<Map<String, Object>> opsByPathList = sortOperationsByPath(ops); List<Map<String, Object>> opsByPathList = sortOperationsByPath(ops);
operations.put("operationsByPath", opsByPathList); operations.put("operationsByPath", opsByPathList);
} }
return super.postProcessSupportingFileData(objs); return super.postProcessSupportingFileData(objs);
} }
} }

View File

@ -14,7 +14,7 @@
{{#operations}} {{#operations}}
{{#operationsByPath}} {{#operationsByPath}}
"{{{path}}}": { "{{{path}}}": {
{{#operation}} {{#operation}}
"{{httpMethod}}": { "{{httpMethod}}": {
"summary": "{{summary}}", "summary": "{{summary}}",
"description":"{{notes}}", "description":"{{notes}}",
@ -34,7 +34,6 @@
{{/operation}} {{/operation}}
} {{#hasMore}},{{/hasMore}} } {{#hasMore}},{{/hasMore}}
{{/operationsByPath}} {{/operationsByPath}}
{{#hasMore}},{{/hasMore}}
{{/operations}} {{/operations}}
{{/apis}} {{/apis}}
{{/apiInfo}} {{/apiInfo}}