Skip to content

Commit

Permalink
use vendor extension in operation to set the body parameter name (#264)
Browse files Browse the repository at this point in the history
Use vendor extension in Operation to set the body parameter name
  • Loading branch information
wing328 authored Apr 29, 2018
1 parent 80c8b92 commit 861d11d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,12 @@ public CodegenOperation fromOperation(String path,
if (StringUtils.isNotBlank(requestBody.get$ref())) {
requestBody = openAPI.getComponents().getRequestBodies().get(getSimpleRef(requestBody.get$ref()));
}
bodyParam = fromRequestBody(requestBody, schemas, imports);

String bodyParameterName = "";
if (op.vendorExtensions != null && op.vendorExtensions.containsKey("x-codegen-body-parameter-name")) {
bodyParameterName = (String) op.vendorExtensions.get("x-codegen-body-parameter-name");
}
bodyParam = fromRequestBody(requestBody, schemas, imports, bodyParameterName);
bodyParam.description = requestBody.getDescription();
postProcessParameter(bodyParam);

Expand Down Expand Up @@ -4172,7 +4177,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
return codegenParameter;
}

public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> schemas, Set<String> imports) {
public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> schemas, Set<String> imports, String bodyParameterName) {
if (body == null) {
LOGGER.error("body in fromRequestBody cannot be null!");
}
Expand All @@ -4197,9 +4202,14 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
schema.setName(name);
codegenModel = fromModel(name, schema, schemas);
}

if (codegenModel != null && !codegenModel.emptyVars) {
codegenParameter.baseName = codegenModel.classname;
codegenParameter.paramName = toParamName(codegenModel.classname);
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = codegenModel.classname;
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.baseType = codegenModel.classname;
codegenParameter.dataType = getTypeDeclaration(codegenModel.classname);
codegenParameter.description = codegenModel.description;
Expand Down Expand Up @@ -4249,8 +4259,13 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
mostInnerItem = innerCp;
innerCp = innerCp.items;
}
codegenParameter.baseName = mostInnerItem.complexType;
codegenParameter.paramName = toArrayModelParamName(mostInnerItem.complexType);

if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = mostInnerItem.complexType;
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toArrayModelParamName(codegenParameter.baseName);
codegenParameter.items = codegenProperty.items;
codegenParameter.dataType = getTypeDeclaration(arraySchema);
codegenParameter.baseType = getSchemaType(arraySchema);
Expand All @@ -4273,8 +4288,12 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
// only support 1-dimension map only
imports.add(codegenProperty.baseType);

codegenParameter.baseName = "request_body";
codegenParameter.paramName = toParamName("request_body");
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = "request_body";
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.items = codegenProperty.items;
codegenParameter.dataType = getTypeDeclaration(inner);
codegenParameter.baseType = getSchemaType(inner);
Expand All @@ -4286,10 +4305,10 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
// HTTP request body is primitive type (e.g. integer, string, etc)
CodegenProperty codegenProperty = fromProperty("PRIMITIVE_REQUEST_BODY", schema);
if (codegenProperty != null) {
if (schema.getExtensions() != null && schema.getExtensions().containsKey("x-codegen-body-parameter-name")) {
codegenParameter.baseName = (String) schema.getExtensions().get("x-codegen-body-parameter-name");
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = "body"; // default to body
} else {
codegenParameter.baseName = "body"; // default to body
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.isPrimitiveType = true;
codegenParameter.baseType = codegenProperty.baseType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ public void arraysInRequestBody() throws Exception {
RequestBody body1 = new RequestBody();
body1.setDescription("A list of ids");
body1.setContent(new Content().addMediaType("application/json", new MediaType().schema(new ArraySchema().items(new StringSchema()))));
CodegenParameter codegenParameter1 = codegen.fromRequestBody(body1 , new HashMap<String, Schema>(), new HashSet<String>());
CodegenParameter codegenParameter1 = codegen.fromRequestBody(body1 , new HashMap<String, Schema>(), new HashSet<String>(), null);
Assert.assertEquals(codegenParameter1.description, "A list of ids");
Assert.assertEquals(codegenParameter1.dataType, "List<String>");
Assert.assertEquals(codegenParameter1.baseType, "List");

RequestBody body2 = new RequestBody();
body2.setDescription("A list of list of values");
body2.setContent(new Content().addMediaType("application/json", new MediaType().schema(new ArraySchema().items(new ArraySchema().items(new IntegerSchema())))));
CodegenParameter codegenParameter2 = codegen.fromRequestBody(body2 , new HashMap<String, Schema>(), new HashSet<String>());
CodegenParameter codegenParameter2 = codegen.fromRequestBody(body2 , new HashMap<String, Schema>(), new HashSet<String>(), null);
Assert.assertEquals(codegenParameter2.description, "A list of list of values");
Assert.assertEquals(codegenParameter2.dataType, "List<List<Integer>>");
Assert.assertEquals(codegenParameter2.baseType, "List");
Expand All @@ -165,7 +165,7 @@ public void arraysInRequestBody() throws Exception {
point.addProperties("message", new StringSchema());
point.addProperties("x", new IntegerSchema().format(SchemaTypeUtil.INTEGER32_FORMAT));
point.addProperties("y", new IntegerSchema().format(SchemaTypeUtil.INTEGER32_FORMAT));
CodegenParameter codegenParameter3 = codegen.fromRequestBody(body3 , Collections.<String, Schema>singletonMap("Point", point), new HashSet<String>());
CodegenParameter codegenParameter3 = codegen.fromRequestBody(body3 , Collections.<String, Schema>singletonMap("Point", point), new HashSet<String>(), null);
Assert.assertEquals(codegenParameter3.description, "A list of points");
Assert.assertEquals(codegenParameter3.dataType, "List<Point>");
Assert.assertEquals(codegenParameter3.baseType, "List");
Expand Down

0 comments on commit 861d11d

Please sign in to comment.