use vendor extension in operation to set the body parameter name (#264)

Use vendor extension in Operation to set the body parameter name
This commit is contained in:
William Cheng 2018-04-29 17:17:01 +08:00 committed by GitHub
parent 80c8b92cb5
commit 861d11d010
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 14 deletions

View File

@ -2226,7 +2226,12 @@ public class DefaultCodegen implements CodegenConfig {
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);
@ -4172,7 +4177,7 @@ public class DefaultCodegen implements CodegenConfig {
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!");
}
@ -4197,9 +4202,14 @@ public class DefaultCodegen implements CodegenConfig {
schema.setName(name);
codegenModel = fromModel(name, schema, schemas);
}
if (codegenModel != null && !codegenModel.emptyVars) {
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = codegenModel.classname;
codegenParameter.paramName = toParamName(codegenModel.classname);
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.baseType = codegenModel.classname;
codegenParameter.dataType = getTypeDeclaration(codegenModel.classname);
codegenParameter.description = codegenModel.description;
@ -4249,8 +4259,13 @@ public class DefaultCodegen implements CodegenConfig {
mostInnerItem = innerCp;
innerCp = innerCp.items;
}
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = mostInnerItem.complexType;
codegenParameter.paramName = toArrayModelParamName(mostInnerItem.complexType);
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toArrayModelParamName(codegenParameter.baseName);
codegenParameter.items = codegenProperty.items;
codegenParameter.dataType = getTypeDeclaration(arraySchema);
codegenParameter.baseType = getSchemaType(arraySchema);
@ -4273,8 +4288,12 @@ public class DefaultCodegen implements CodegenConfig {
// only support 1-dimension map only
imports.add(codegenProperty.baseType);
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = "request_body";
codegenParameter.paramName = toParamName("request_body");
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.items = codegenProperty.items;
codegenParameter.dataType = getTypeDeclaration(inner);
codegenParameter.baseType = getSchemaType(inner);
@ -4286,10 +4305,10 @@ public class DefaultCodegen implements CodegenConfig {
// 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");
} else {
if (StringUtils.isEmpty(bodyParameterName)) {
codegenParameter.baseName = "body"; // default to body
} else {
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.isPrimitiveType = true;
codegenParameter.baseType = codegenProperty.baseType;

View File

@ -145,7 +145,7 @@ public class JavaClientCodegenTest {
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");
@ -153,7 +153,7 @@ public class JavaClientCodegenTest {
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");
@ -165,7 +165,7 @@ public class JavaClientCodegenTest {
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");