修改openapi生成schema 数据类型为数组时数据错误问题

This commit is contained in:
Forget 2021-03-11 10:32:16 +08:00
parent 5abdd84773
commit 37be755c5b
2 changed files with 28 additions and 7 deletions

View File

@ -270,7 +270,7 @@ public class OpenApiBuilder {
} else if (!isRep && Objects.nonNull(apiMethodDoc.getRequestSchema())) {
content.put("schema", apiMethodDoc.getRequestSchema());
} else {
content.put("schema", buildBodySchema(apiMethodDoc.getPath(), isRep));
content.put("schema", buildBodySchema(apiMethodDoc, isRep));
}
}
content.put("examples", buildBodyExample(apiMethodDoc, isRep));
@ -281,18 +281,37 @@ public class OpenApiBuilder {
/**
* content body 的schema 信息
*
* @param url 请求的url 去除server
* @param apiMethodDoc 请求方法参数 去除server
* @param isRep 是否是返回数据
* @return
*/
private static Map<String, Object> buildBodySchema(String url, boolean isRep) {
private static Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean isRep) {
Map<String, Object> schema = new HashMap<>(10);
//当类型为数组时使用
Map<String, Object> innerScheme = new HashMap<>(10);
//去除url中的特殊字符
if (isRep) {
schema.put("$ref", "#/components/schemas/" + url.replaceAll(PATH_REGEX, "_") + "response");
} else {
schema.put("$ref", "#/components/schemas/" + url.replaceAll(PATH_REGEX, "_") + "request");
String responseRef = "#/components/schemas/" + apiMethodDoc.getPath().replaceAll(PATH_REGEX, "_") + "response";
String requestRef = "#/components/schemas/" + apiMethodDoc.getPath().replaceAll(PATH_REGEX, "_") + "request";
//如果是数组类型
if(DocGlobalConstants.ARRAY.equals(apiMethodDoc.getType())){
schema.put("type",DocGlobalConstants.ARRAY);
if (isRep) {
innerScheme.put("$ref", responseRef);
} else {
innerScheme.put("$ref", requestRef);
}
schema.put("items",innerScheme);
}
else {
if (isRep) {
schema.put("$ref", responseRef);
} else {
schema.put("$ref", requestRef);
}
}
return schema;
}

View File

@ -200,4 +200,6 @@ public interface DocGlobalConstants {
String YAPI_JSON = "/yapi.json";
String DUBBO_SWAGGER = "org.apache.dubbo.rpc.protocol.rest.integration.swagger.DubboSwaggerApiListingResource";
String ARRAY = "array";
}