add a CLI option to sort method argument

This commit is contained in:
wing328 2015-09-22 15:57:22 +08:00
parent cbc2fb237d
commit 4e4e9e7893
4 changed files with 21 additions and 2 deletions

View File

@ -37,4 +37,7 @@ public class CodegenConstants {
public static final String LIBRARY = "library"; public static final String LIBRARY = "library";
public static final String LIBRARY_DESC = "library template (sub-template)"; public static final String LIBRARY_DESC = "library template (sub-template)";
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";
} }

View File

@ -103,6 +103,10 @@ public class DefaultCodegen {
if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) { if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
this.setApiPackage((String) additionalProperties.get(CodegenConstants.API_PACKAGE)); this.setApiPackage((String) additionalProperties.get(CodegenConstants.API_PACKAGE));
} }
if (additionalProperties.containsKey(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG)) {
this.setSortParamsByRequiredFlag(Boolean.valueOf((String)additionalProperties.get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()));
}
} }
// override with any special post-processing // override with any special post-processing
@ -227,6 +231,10 @@ public class DefaultCodegen {
this.apiPackage = apiPackage; this.apiPackage = apiPackage;
} }
public void setSortParamsByRequiredFlag(Boolean sortParamsByRequiredFlag) {
this.sortParamsByRequiredFlag = sortParamsByRequiredFlag;
}
public String toApiFilename(String name) { public String toApiFilename(String name) {
return toApiName(name); return toApiName(name);
} }
@ -344,6 +352,7 @@ public class DefaultCodegen {
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC)); 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.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
cliOptions.add(new CliOption(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC));
} }
@ -1033,8 +1042,8 @@ public class DefaultCodegen {
} }
op.bodyParam = bodyParam; op.bodyParam = bodyParam;
op.httpMethod = httpMethod.toUpperCase(); op.httpMethod = httpMethod.toUpperCase();
// move "required" parameters in front of "optional" parameters
// move "required" parameters in front of "optional" parameters
if(sortParamsByRequiredFlag) { if(sortParamsByRequiredFlag) {
Collections.sort(allParams, new Comparator<CodegenParameter>() { Collections.sort(allParams, new Comparator<CodegenParameter>() {
@Override @Override

View File

@ -409,7 +409,7 @@ class UserApi
* *
* Get user by user name * Get user by user name
* *
* @param string $username The name that needs to be fetched. Use user1 for testing. (required) * @param string $username The name that needs to be fetched. Use user1 for testing. (required)
* @return \Swagger\Client\Model\User * @return \Swagger\Client\Model\User
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
*/ */

View File

@ -48,6 +48,8 @@ class ApiClient
public static $PATCH = "PATCH"; public static $PATCH = "PATCH";
public static $POST = "POST"; public static $POST = "POST";
public static $GET = "GET"; public static $GET = "GET";
public static $HEAD = "HEAD";
public static $OPTIONS = "OPTIONS";
public static $PUT = "PUT"; public static $PUT = "PUT";
public static $DELETE = "DELETE"; public static $DELETE = "DELETE";
@ -170,6 +172,11 @@ class ApiClient
if ($method == self::$POST) { if ($method == self::$POST) {
curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} else if ($method == self::$HEAD) {
curl_setopt($curl, CURLOPT_NOBODY, true);
} else if ($method == self::$OPTIONS) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} else if ($method == self::$PATCH) { } else if ($method == self::$PATCH) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);