mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-13 05:00:50 +00:00
add option to reorder form/body parameter
This commit is contained in:
parent
6dc5f7f37b
commit
5326152ccd
@ -102,6 +102,9 @@ public class CodegenConstants {
|
|||||||
public static final String SORT_PARAMS_BY_REQUIRED_FLAG = "sortParamsByRequiredFlag";
|
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.";
|
public static final String SORT_PARAMS_BY_REQUIRED_FLAG_DESC = "Sort method arguments to place required parameters before optional parameters.";
|
||||||
|
|
||||||
|
public static final String PREPEND_FORM_OR_BODY_PARAMETERS = "prependFormOrBodyParameters";
|
||||||
|
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_DESC = "Add form or body parameters to the beginning of the parameter list.";
|
||||||
|
|
||||||
public static final String USE_DATETIME_OFFSET = "useDateTimeOffset";
|
public static final String USE_DATETIME_OFFSET = "useDateTimeOffset";
|
||||||
public static final String USE_DATETIME_OFFSET_DESC = "Use DateTimeOffset to model date-time properties";
|
public static final String USE_DATETIME_OFFSET_DESC = "Use DateTimeOffset to model date-time properties";
|
||||||
|
|
||||||
|
@ -100,6 +100,7 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
protected Map<String, String> specialCharReplacements = new HashMap<String, String>();
|
protected Map<String, String> specialCharReplacements = new HashMap<String, String>();
|
||||||
// When a model is an alias for a simple type
|
// When a model is an alias for a simple type
|
||||||
protected Map<String, String> typeAliases = null;
|
protected Map<String, String> typeAliases = null;
|
||||||
|
protected Boolean prependFormOrBodyParameters = false;
|
||||||
|
|
||||||
protected String ignoreFilePathOverride;
|
protected String ignoreFilePathOverride;
|
||||||
|
|
||||||
@ -125,6 +126,11 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
.get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()));
|
.get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (additionalProperties.containsKey(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS)) {
|
||||||
|
this.setPrependFormOrBodyParameters(Boolean.valueOf(additionalProperties
|
||||||
|
.get(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS).toString()));
|
||||||
|
}
|
||||||
|
|
||||||
if (additionalProperties.containsKey(CodegenConstants.ENSURE_UNIQUE_PARAMS)) {
|
if (additionalProperties.containsKey(CodegenConstants.ENSURE_UNIQUE_PARAMS)) {
|
||||||
this.setEnsureUniqueParams(Boolean.valueOf(additionalProperties
|
this.setEnsureUniqueParams(Boolean.valueOf(additionalProperties
|
||||||
.get(CodegenConstants.ENSURE_UNIQUE_PARAMS).toString()));
|
.get(CodegenConstants.ENSURE_UNIQUE_PARAMS).toString()));
|
||||||
@ -583,6 +589,10 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
this.sortParamsByRequiredFlag = sortParamsByRequiredFlag;
|
this.sortParamsByRequiredFlag = sortParamsByRequiredFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPrependFormOrBodyParameters(Boolean prependFormOrBodyParameters) {
|
||||||
|
this.prependFormOrBodyParameters = prependFormOrBodyParameters;
|
||||||
|
}
|
||||||
|
|
||||||
public void setEnsureUniqueParams(Boolean ensureUniqueParams) {
|
public void setEnsureUniqueParams(Boolean ensureUniqueParams) {
|
||||||
this.ensureUniqueParams = ensureUniqueParams;
|
this.ensureUniqueParams = ensureUniqueParams;
|
||||||
}
|
}
|
||||||
@ -846,10 +856,12 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC).defaultValue(Boolean.TRUE.toString()));
|
CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC).defaultValue(Boolean.TRUE.toString()));
|
||||||
cliOptions.add(CliOption.newBoolean(CodegenConstants.ENSURE_UNIQUE_PARAMS, CodegenConstants
|
cliOptions.add(CliOption.newBoolean(CodegenConstants.ENSURE_UNIQUE_PARAMS, CodegenConstants
|
||||||
.ENSURE_UNIQUE_PARAMS_DESC).defaultValue(Boolean.TRUE.toString()));
|
.ENSURE_UNIQUE_PARAMS_DESC).defaultValue(Boolean.TRUE.toString()));
|
||||||
|
|
||||||
// name formatting options
|
// name formatting options
|
||||||
cliOptions.add(CliOption.newBoolean(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, CodegenConstants
|
cliOptions.add(CliOption.newBoolean(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, CodegenConstants
|
||||||
.ALLOW_UNICODE_IDENTIFIERS_DESC).defaultValue(Boolean.FALSE.toString()));
|
.ALLOW_UNICODE_IDENTIFIERS_DESC).defaultValue(Boolean.FALSE.toString()));
|
||||||
|
// option to change the order of form/body parameter
|
||||||
|
cliOptions.add(CliOption.newBoolean(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS,
|
||||||
|
CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS_DESC).defaultValue(Boolean.FALSE.toString()));
|
||||||
|
|
||||||
// initialize special character mapping
|
// initialize special character mapping
|
||||||
initalizeSpecialCharacterMapping();
|
initalizeSpecialCharacterMapping();
|
||||||
@ -2092,10 +2104,13 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
"multipart/form-data".equalsIgnoreCase(getContentType(requestBody))) {
|
"multipart/form-data".equalsIgnoreCase(getContentType(requestBody))) {
|
||||||
// process form parameters
|
// process form parameters
|
||||||
formParams = fromRequestBodyToFormParameters(requestBody, schemas, imports);
|
formParams = fromRequestBodyToFormParameters(requestBody, schemas, imports);
|
||||||
|
// add form parameters to the beginning of all parameter list
|
||||||
|
if (prependFormOrBodyParameters) {
|
||||||
for (CodegenParameter cp : formParams) {
|
for (CodegenParameter cp : formParams) {
|
||||||
LOGGER.info("Adding " + cp.baseName + " to allParams list");
|
LOGGER.info("Adding " + cp.baseName + " to allParams list");
|
||||||
allParams.add(cp.copy());
|
allParams.add(cp.copy());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// process body parameter
|
// process body parameter
|
||||||
if (StringUtils.isNotBlank(requestBody.get$ref())) {
|
if (StringUtils.isNotBlank(requestBody.get$ref())) {
|
||||||
@ -2105,11 +2120,14 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
bodyParam = fromRequestBody(requestBody, schemas, imports);
|
bodyParam = fromRequestBody(requestBody, schemas, imports);
|
||||||
bodyParam.description = requestBody.getDescription();
|
bodyParam.description = requestBody.getDescription();
|
||||||
bodyParams.add(bodyParam);
|
bodyParams.add(bodyParam);
|
||||||
|
|
||||||
|
if (prependFormOrBodyParameters) {
|
||||||
|
allParams.add(bodyParam);
|
||||||
|
}
|
||||||
if (schemas != null) {
|
if (schemas != null) {
|
||||||
// TODO fix NPE
|
// TODO fix NPE
|
||||||
//op.requestBodyExamples = new ExampleGenerator(schemas).generate(null, new ArrayList<String>(getConsumesInfo(operation)), bodyParam.dataType);
|
//op.requestBodyExamples = new ExampleGenerator(schemas).generate(null, new ArrayList<String>(getConsumesInfo(operation)), bodyParam.dataType);
|
||||||
}
|
}
|
||||||
allParams.add(bodyParam);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2123,31 +2141,11 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
|
|
||||||
CodegenParameter p = fromParameter(param, imports);
|
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);
|
allParams.add(p);
|
||||||
// Issue #2561 (neilotoole) : Moved setting of is<Type>Param flags
|
|
||||||
// from here to fromParameter().
|
|
||||||
if (param instanceof QueryParameter || "query".equalsIgnoreCase(param.getIn())) {
|
if (param instanceof QueryParameter || "query".equalsIgnoreCase(param.getIn())) {
|
||||||
queryParams.add(p.copy());
|
queryParams.add(p.copy());
|
||||||
} else if (param instanceof PathParameter || "path".equalsIgnoreCase(param.getIn())) {
|
} else if (param instanceof PathParameter || "path".equalsIgnoreCase(param.getIn())) {
|
||||||
LOGGER.info("Adding path parameter: "+ p.paramName);
|
|
||||||
pathParams.add(p.copy());
|
pathParams.add(p.copy());
|
||||||
} else if (param instanceof HeaderParameter || "header".equalsIgnoreCase(param.getIn())) {
|
} else if (param instanceof HeaderParameter || "header".equalsIgnoreCase(param.getIn())) {
|
||||||
headerParams.add(p.copy());
|
headerParams.add(p.copy());
|
||||||
@ -2157,14 +2155,37 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
LOGGER.warn("Unknown parameter type " + p.baseType + " for " + p.baseName);
|
LOGGER.warn("Unknown parameter type " + p.baseType + " for " + p.baseName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.required) { //required parameters
|
}
|
||||||
requiredParams.add(p.copy());
|
}
|
||||||
|
|
||||||
|
// add form/body parameter (if any) to the end of all parameter list
|
||||||
|
if (!prependFormOrBodyParameters) {
|
||||||
|
for (CodegenParameter cp : formParams) {
|
||||||
|
allParams.add(cp.copy());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (CodegenParameter cp : bodyParams) {
|
||||||
|
allParams.add(cp.copy());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for (CodegenParameter cp : allParams) {
|
||||||
|
if (ensureUniqueParams) {
|
||||||
|
if (isParameterNameUnique(cp, allParams)) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
cp.paramName = generateNextName(cp.paramName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cp.required) { //required parameters
|
||||||
|
requiredParams.add(cp.copy());
|
||||||
} else { // optional parameters
|
} else { // optional parameters
|
||||||
op.hasOptionalParams = true;
|
op.hasOptionalParams = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// add imports to operation import tag
|
||||||
for (String i : imports) {
|
for (String i : imports) {
|
||||||
if (needToImport(i)) {
|
if (needToImport(i)) {
|
||||||
op.imports.add(i);
|
op.imports.add(i);
|
||||||
@ -2214,6 +2235,20 @@ public class DefaultCodegen implements CodegenConfig {
|
|||||||
return op;
|
return op;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isParameterNameUnique(CodegenParameter p, List<CodegenParameter> parameters) {
|
||||||
|
for (CodegenParameter parameter: parameters) {
|
||||||
|
if (System.identityHashCode(p) == System.identityHashCode(parameter)) {
|
||||||
|
continue; // skip itself
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p.paramName.equals(parameter.paramName)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert OAS Response object to Codegen Response object
|
* Convert OAS Response object to Codegen Response object
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user