Auto generate operationId for python-flask server codegen

This commit is contained in:
xhh
2015-11-25 17:55:04 +08:00
parent d589facb21
commit 6675cbc20e
6 changed files with 85 additions and 69 deletions

View File

@@ -1081,31 +1081,7 @@ public class DefaultCodegen {
Set<String> imports = new HashSet<String>();
op.vendorExtensions = operation.getVendorExtensions();
String operationId = operation.getOperationId();
if (operationId == null) {
String tmpPath = path;
tmpPath = tmpPath.replaceAll("\\{", "");
tmpPath = tmpPath.replaceAll("\\}", "");
String[] parts = (tmpPath + "/" + httpMethod).split("/");
StringBuilder builder = new StringBuilder();
if ("/".equals(tmpPath)) {
// must be root tmpPath
builder.append("root");
}
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
if (part.length() > 0) {
if (builder.toString().length() == 0) {
part = Character.toLowerCase(part.charAt(0)) + part.substring(1);
} else {
part = initialCaps(part);
}
builder.append(part);
}
}
operationId = builder.toString();
LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path);
}
String operationId = getOrGenerateOperationId(operation, path, httpMethod);
operationId = removeNonNameElementToCamelCase(operationId);
op.path = path;
op.operationId = toOperationId(operationId);
@@ -1620,6 +1596,43 @@ public class DefaultCodegen {
return secs;
}
/**
* Get operationId from the operation object, and if it's blank, generate a new one from the given parameters.
*
* @param operation the operation object
* @param path the path of the operation
* @param httpMethod the HTTP method of the operation
* @return the (generated) operationId
*/
protected String getOrGenerateOperationId(Operation operation, String path, String httpMethod) {
String operationId = operation.getOperationId();
if (StringUtils.isBlank(operationId)) {
String tmpPath = path;
tmpPath = tmpPath.replaceAll("\\{", "");
tmpPath = tmpPath.replaceAll("\\}", "");
String[] parts = (tmpPath + "/" + httpMethod).split("/");
StringBuilder builder = new StringBuilder();
if ("/".equals(tmpPath)) {
// must be root tmpPath
builder.append("root");
}
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
if (part.length() > 0) {
if (builder.toString().length() == 0) {
part = Character.toLowerCase(part.charAt(0)) + part.substring(1);
} else {
part = initialCaps(part);
}
builder.append(part);
}
}
operationId = builder.toString();
LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path);
}
return operationId;
}
/**
* Check the type to see if it needs import the library/module/package
*

View File

@@ -5,6 +5,7 @@ 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.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
@@ -205,13 +206,15 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
for(String pathname : swagger.getPaths().keySet()) {
Path path = swagger.getPath(pathname);
if(path.getOperations() != null) {
for(Operation operation : path.getOperations()) {
String operationId = operation.getOperationId();
if(operationId != null && operationId.indexOf(".") == -1) {
operation.setVendorExtension("x-operationId", underscore(sanitizeName(operationId)));
operationId = controllerPackage + "." + defaultController + "." + underscore(sanitizeName(operationId));
operation.setOperationId(operationId);
for(Map.Entry<HttpMethod, Operation> entry : path.getOperationMap().entrySet()) {
String httpMethod = entry.getKey().name().toLowerCase();
Operation operation = entry.getValue();
String operationId = getOrGenerateOperationId(operation, pathname, httpMethod);
String xOperationId = underscore(sanitizeName(operationId));
if(!operationId.contains(".")) {
operation.setOperationId(controllerPackage + "." + defaultController + "." + xOperationId);
}
operation.setVendorExtension("x-operationId", xOperationId);
if(operation.getTags() != null) {
List<Map<String, String>> tags = new ArrayList<Map<String, String>>();
for(String tag : operation.getTags()) {