forked from loafle/openapi-generator-original
Merge pull request #1614 from xhh/auto-generate-operation-id
Auto generate operationId for python-flask server codegen
This commit is contained in:
@@ -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);
|
||||
@@ -1635,6 +1611,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
|
||||
*
|
||||
|
||||
@@ -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,17 @@ 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()) {
|
||||
// Normalize `operationId` and add package/class path in front, e.g.
|
||||
// controllers.default_controller.add_pet
|
||||
String httpMethod = entry.getKey().name().toLowerCase();
|
||||
Operation operation = entry.getValue();
|
||||
String operationId = getOrGenerateOperationId(operation, pathname, httpMethod);
|
||||
if(!operationId.contains(".")) {
|
||||
operationId = underscore(sanitizeName(operationId));
|
||||
operationId = controllerPackage + "." + defaultController + "." + operationId;
|
||||
}
|
||||
operation.setOperationId(operationId);
|
||||
if(operation.getTags() != null) {
|
||||
List<Map<String, String>> tags = new ArrayList<Map<String, String>>();
|
||||
for(String tag : operation.getTags()) {
|
||||
@@ -292,4 +297,15 @@ public class FlaskConnexionCodegen extends DefaultCodegen implements CodegenConf
|
||||
}
|
||||
return super.postProcessSupportingFileData(objs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toOperationId(String operationId) {
|
||||
operationId = super.toOperationId(operationId);
|
||||
// Use the part after the last dot, e.g.
|
||||
// controllers.defaultController.addPet => addPet
|
||||
operationId = operationId.replaceAll(".*\\.", "");
|
||||
// Need to underscore it since it has been processed via removeNonNameElementToCamelCase, e.g.
|
||||
// addPet => add_pet
|
||||
return underscore(operationId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user