Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@
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;
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);
Expand Down Expand Up @@ -191,6 +188,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
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;
Expand Down Expand Up @@ -253,7 +251,6 @@ private static List<Map<String, Object>> sortOperationsByPath(List<CodegenOperat

@Override
public void preprocessSwagger(Swagger swagger) {

String host = swagger.getHost();
String port = "8080";
if (host != null) {
Expand All @@ -273,6 +270,28 @@ public void preprocessSwagger(Swagger swagger) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -24,5 +23,6 @@ var examples = {};
{{/returnType}}
{{^returnType}}res.end();{{/returnType}}
}

{{/operation}}
{{/operations}}