[Erlang] erlang client: properly handle optional parameters (#7042)

* erlang client: properly handle optional parameters

* [Erlang Client] handle multiple path params in the same path
This commit is contained in:
Tristan Sloughter
2017-11-27 00:11:14 -08:00
committed by William Cheng
parent 7a15121f94
commit 3d3a30a533
12 changed files with 214 additions and 104 deletions

View File

@@ -163,10 +163,12 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
public String qsEncode(Object o) {
String r = new String();
CodegenParameter q = (CodegenParameter) o;
if (q.isListContainer) {
r += "[{<<\"" + q.baseName + "\">>, X} || X <- " + q.paramName + "]";
} else {
r += "{<<\"" + q.baseName + "\">>, " + q.paramName + "}";
if (q.required) {
if (q.isListContainer) {
r += "[{<<\"" + q.baseName + "\">>, X} || X <- " + q.paramName + "]";
} else {
r += "{<<\"" + q.baseName + "\">>, " + q.paramName + "}";
}
}
return r;
}
@@ -257,7 +259,7 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> os = (List<CodegenOperation>) operations.get("operation");
List<ExtendedCodegenOperation> newOs = new ArrayList<ExtendedCodegenOperation>();
Pattern pattern = Pattern.compile("(.*)\\{([^\\}]+)\\}(.*)");
Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}");
for (CodegenOperation o : os) {
// force http method to lower case
o.httpMethod = o.httpMethod.toLowerCase();
@@ -270,10 +272,12 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
Matcher matcher = pattern.matcher(o.path);
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
String pathTemplateName = matcher.group(2);
matcher.appendReplacement(buffer, "$1" + "\", " + camelize(pathTemplateName) + ", \"" + "$3");
String pathTemplateName = matcher.group(1);
matcher.appendReplacement(buffer, "\", " + camelize(pathTemplateName) + ", \"");
pathTemplateNames.add(pathTemplateName);
}
matcher.appendTail(buffer);
ExtendedCodegenOperation eco = new ExtendedCodegenOperation(o);
if (buffer.toString().isEmpty()) {
eco.setReplacedPathName(o.path);
@@ -295,8 +299,26 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
this.packageVersion = packageVersion;
}
String length(Object o) {
return Integer.toString((((ExtendedCodegenOperation) o).allParams).size());
String length(Object os) {
int l = 1;
for (CodegenParameter o : ((ExtendedCodegenOperation) os).allParams) {
CodegenParameter q = (CodegenParameter) o;
if (q.required)
l++;
}
return Integer.toString(l);
}
int lengthRequired(List<CodegenParameter> allParams) {
int l = 0;
for (CodegenParameter o : allParams) {
CodegenParameter q = (CodegenParameter) o;
if (q.required || q.isBodyParam)
l++;
}
return l;
}
@Override
@@ -313,6 +335,8 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
class ExtendedCodegenOperation extends CodegenOperation {
private List<String> pathTemplateNames = new ArrayList<String>();
private String replacedPathName;
String arityRequired;
String arityOptional;
public ExtendedCodegenOperation(CodegenOperation o) {
super();
@@ -355,6 +379,8 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
this.produces = o.produces;
this.bodyParam = o.bodyParam;
this.allParams = o.allParams;
this.arityRequired = Integer.toString(lengthRequired(o.allParams));
this.arityOptional = Integer.toString(lengthRequired(o.allParams)+1);
this.bodyParams = o.bodyParams;
this.pathParams = o.pathParams;
this.queryParams = o.queryParams;