Merge pull request #2052 from swagger-api/issue-2001

added x-swagger-router-controller, operationId for node.js versions
This commit is contained in:
Tony Tam 2016-02-05 17:24:00 -08:00
commit c80df9ee4d
2 changed files with 34 additions and 15 deletions

View File

@ -8,11 +8,11 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import io.swagger.codegen.*;
import io.swagger.models.Swagger;
import io.swagger.models.Info;
import io.swagger.models.*;
import io.swagger.util.Yaml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
@ -20,9 +20,6 @@ import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(NodeJSServerCodegen.class);
@ -191,6 +188,7 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
for (CodegenOperation operation : operations) {
operation.httpMethod = operation.httpMethod.toLowerCase();
List<CodegenParameter> params = operation.allParams;
if (params != null && params.size() == 0) {
operation.allParams = null;
@ -253,7 +251,6 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
@Override
public void preprocessSwagger(Swagger swagger) {
String host = swagger.getHost();
String port = "8080";
if (host != null) {
@ -273,6 +270,28 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
this.additionalProperties.put("projectName", projectName);
}
}
// need vendor extensions for x-swagger-router-controller
Map<String, Path> paths = swagger.getPaths();
if(paths != null) {
for(String pathname : paths.keySet()) {
Path path = paths.get(pathname);
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
if(operationMap != null) {
for(HttpMethod method : operationMap.keySet()) {
Operation operation = operationMap.get(method);
String tag = "default";
if(operation.getTags() != null && operation.getTags().size() > 0) {
tag = toApiName(operation.getTags().get(0));
}
if(operation.getOperationId() == null) {
operation.setOperationId(getOrGenerateOperationId(operation, pathname, method.toString()));
}
operation.getVendorExtensions().put("x-swagger-router-controller", toApiName(tag));
}
}
}
}
}
@Override

View File

@ -5,15 +5,14 @@
exports.{{nickname}} = function(args, res, next) {
/**
* parameters expected in the args:
{{#allParams}}* {{paramName}} ({{dataType}})
{{/allParams}}**/
var examples = {};
{{#examples}}
examples['{{contentType}}'] = {{{example}}};
{{/examples}}
{{#allParams}}* {{paramName}} ({{dataType}})
{{/allParams}}**/
{{^returnType}}// no response value expected for this operation
{{/returnType}}
{{#returnType}}
var examples = {};
{{#examples}}examples['{{contentType}}'] = {{{example}}};
{{/examples}}
if(Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
@ -24,5 +23,6 @@ var examples = {};
{{/returnType}}
{{^returnType}}res.end();{{/returnType}}
}
{{/operation}}
{{/operations}}