forked from loafle/openapi-generator-original
Make parameters unique in generated code
through a config option "ensureUniqueParams" which is false by default Closes #1422
This commit is contained in:
@@ -40,6 +40,9 @@ public class CodegenConstants {
|
||||
public static final String SORT_PARAMS_BY_REQUIRED_FLAG = "sortParamsByRequiredFlag";
|
||||
public static final String SORT_PARAMS_BY_REQUIRED_FLAG_DESC = "Sort method arguments to place required parameters before optional parameters. Default: true";
|
||||
|
||||
public static final String ENSURE_UNIQUE_PARAMS = "ensureUniqueParams";
|
||||
public static final String ENSURE_UNIQUE_PARAMS_DESC = "Whether to ensure parameter names are unique in an operation (rename parameters that are not). Default: false";
|
||||
|
||||
public static final String PACKAGE_NAME = "packageName";
|
||||
public static final String PACKAGE_VERSION = "packageVersion";
|
||||
public static final String POD_VERSION = "podVersion";
|
||||
|
||||
@@ -87,6 +87,7 @@ public class DefaultCodegen {
|
||||
protected Map<String, String> supportedLibraries = new LinkedHashMap<String, String>();
|
||||
protected String library = null;
|
||||
protected Boolean sortParamsByRequiredFlag = true;
|
||||
protected Boolean ensureUniqueParams = false;
|
||||
|
||||
public List<CliOption> cliOptions() {
|
||||
return cliOptions;
|
||||
@@ -109,6 +110,11 @@ public class DefaultCodegen {
|
||||
this.setSortParamsByRequiredFlag(Boolean.valueOf(additionalProperties
|
||||
.get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.ENSURE_UNIQUE_PARAMS)) {
|
||||
this.setEnsureUniqueParams(Boolean.valueOf(additionalProperties
|
||||
.get(CodegenConstants.ENSURE_UNIQUE_PARAMS).toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// override with any special post-processing
|
||||
@@ -245,6 +251,10 @@ public class DefaultCodegen {
|
||||
this.sortParamsByRequiredFlag = sortParamsByRequiredFlag;
|
||||
}
|
||||
|
||||
public void setEnsureUniqueParams(Boolean ensureUniqueParams) {
|
||||
this.ensureUniqueParams = ensureUniqueParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file name of the Api
|
||||
*
|
||||
@@ -436,6 +446,7 @@ public class DefaultCodegen {
|
||||
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.ENSURE_UNIQUE_PARAMS, CodegenConstants.ENSURE_UNIQUE_PARAMS_DESC));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1245,6 +1256,23 @@ public class DefaultCodegen {
|
||||
if (parameters != null) {
|
||||
for (Parameter param : parameters) {
|
||||
CodegenParameter p = fromParameter(param, imports);
|
||||
// rename parameters to make sure all of them have unique names
|
||||
if (ensureUniqueParams) {
|
||||
while (true) {
|
||||
boolean exists = false;
|
||||
for (CodegenParameter cp : allParams) {
|
||||
if (p.paramName.equals(cp.paramName)) {
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (exists) {
|
||||
p.paramName = generateNextName(p.paramName);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
allParams.add(p);
|
||||
if (param instanceof QueryParameter) {
|
||||
p.isQueryParam = new Boolean(true);
|
||||
@@ -1283,7 +1311,7 @@ public class DefaultCodegen {
|
||||
op.httpMethod = httpMethod.toUpperCase();
|
||||
|
||||
// move "required" parameters in front of "optional" parameters
|
||||
if(sortParamsByRequiredFlag) {
|
||||
if (sortParamsByRequiredFlag) {
|
||||
Collections.sort(allParams, new Comparator<CodegenParameter>() {
|
||||
@Override
|
||||
public int compare(CodegenParameter one, CodegenParameter another) {
|
||||
@@ -1710,6 +1738,22 @@ public class DefaultCodegen {
|
||||
return word;
|
||||
}
|
||||
|
||||
// Generate the next name for the given name, e.g.
|
||||
// status => status2
|
||||
// status2 => status3
|
||||
// myName100 => myName101
|
||||
private String generateNextName(String name) {
|
||||
Pattern pattern = Pattern.compile("\\d+\\z");
|
||||
Matcher matcher = pattern.matcher(name);
|
||||
if (matcher.find()) {
|
||||
String numStr = matcher.group();
|
||||
int num = Integer.parseInt(numStr) + 1;
|
||||
return name.substring(0, name.length() - numStr.length()) + num;
|
||||
} else {
|
||||
return name + "2";
|
||||
}
|
||||
}
|
||||
|
||||
private void addImport(CodegenModel m, String type) {
|
||||
if (type != null && needToImport(type)) {
|
||||
m.imports.add(type);
|
||||
|
||||
@@ -56,6 +56,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
embeddedTemplateDir = templateDir = "Java";
|
||||
apiPackage = "io.swagger.client.api";
|
||||
modelPackage = "io.swagger.client.model";
|
||||
ensureUniqueParams = true;
|
||||
|
||||
reservedWords = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
@@ -12,6 +13,7 @@ import io.swagger.models.properties.Property;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
@@ -32,6 +34,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
modelTemplateFiles.put("model.mustache", ".rb");
|
||||
apiTemplateFiles.put("api.mustache", ".rb");
|
||||
embeddedTemplateDir = templateDir = "ruby";
|
||||
ensureUniqueParams = true;
|
||||
|
||||
typeMapping.clear();
|
||||
languageSpecificPrimitives.clear();
|
||||
@@ -70,7 +73,14 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("file", "File");
|
||||
|
||||
// remove modelPackage and apiPackage added by default
|
||||
cliOptions.clear();
|
||||
Iterator<CliOption> itr = cliOptions.iterator();
|
||||
while (itr.hasNext()) {
|
||||
CliOption opt = itr.next();
|
||||
if (CodegenConstants.MODEL_PACKAGE.equals(opt.getOpt()) ||
|
||||
CodegenConstants.API_PACKAGE.equals(opt.getOpt())) {
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
cliOptions.add(new CliOption(GEM_NAME, "gem name (convention: underscore_case), default: swagger_client"));
|
||||
cliOptions.add(new CliOption(MODULE_NAME, "top module name (convention: CamelCase, usually corresponding to gem name), default: SwaggerClient"));
|
||||
cliOptions.add(new CliOption(GEM_VERSION, "gem version, default: 1.0.0"));
|
||||
|
||||
Reference in New Issue
Block a user