fix(接口定义): 修复接口定义导出swagger错误 (#1697)

* feat(测试跟踪): 测试用例下载模版增加标签列

* fix(接口定义): 扩大请求头键长度

* fix: schedule表对旧数据name字段兼容的补充

* fix(接口定义): 修复接口定义导出swagger错误
This commit is contained in:
Coooder-X 2021-03-24 17:50:35 +08:00 committed by GitHub
parent d1c1957a10
commit 5818505b9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 8 deletions

View File

@ -14,9 +14,9 @@ import java.util.List;
public class SwaggerApiExportResult extends ApiExportResult{
private String openapi;
private SwaggerInfo info;
private String externalDocs;
private JSONObject externalDocs;
private List<String> servers;
private List<SwaggerTag> tags;
private JSONObject paths; // Map<String, Object>, Object 里放 Operation 对象
private List<String> components;
private JSONObject components;
}

View File

@ -231,6 +231,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
MediaType mediaType = content.get(contentType);
if (mediaType == null) {
Set<String> contentTypes = content.keySet();
if(contentTypes.size() == 0) { // 防止空指针
return;
}
contentType = contentTypes.iterator().next();
if (StringUtils.isBlank(contentType)) {
return;
@ -410,26 +413,34 @@ public class Swagger3Parser extends SwaggerAbstractParser {
result.setInfo(new SwaggerInfo());
result.setServers(new ArrayList<>());
result.setTags(new ArrayList<>());
result.setComponents(new ArrayList<>());
result.setComponents(new JSONObject());
result.setExternalDocs(new JSONObject());
JSONObject paths = new JSONObject();
JSONObject swaggerPath = new JSONObject();
for(ApiDefinitionWithBLOBs apiDefinition : apiDefinitionList) {
SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:}
swaggerApiInfo.setSummary(apiDefinition.getName());
// 设置导入后的模块名 根据 api moduleID 查库获得所属模块作为导出的模块名
ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
String moduleName = apiModuleService.getNode(apiDefinition.getModuleId()).getName();
String moduleName = "";
if(apiDefinition.getModuleId() != null) { // module_id 可能为空
moduleName = apiModuleService.getNode(apiDefinition.getModuleId()).getName();
}
swaggerApiInfo.setTags(Arrays.asList(moduleName));
// 设置请求体
JSONObject requestObject = JSON.parseObject(apiDefinition.getRequest()); // 将api的request属性转换成JSON对象以便获得参数
JSONObject requestBody = buildRequestBody(requestObject);
swaggerApiInfo.setRequestBody(requestBody);
// 设置响应体
swaggerApiInfo.setResponses(new JSONObject());
// 设置请求参数列表
List<JSONObject> paramsList = buildParameters(requestObject);
swaggerApiInfo.setParameters(paramsList);
swaggerPath.put(apiDefinition.getMethod().toLowerCase(), JSON.parseObject(JSON.toJSONString(swaggerApiInfo))); // 设置api的请求类型和api定义参数
paths.put(apiDefinition.getPath(), swaggerPath);
JSONObject methodDetail = JSON.parseObject(JSON.toJSONString(swaggerApiInfo));
if(paths.getJSONObject(apiDefinition.getPath()) == null) {
paths.put(apiDefinition.getPath(), new JSONObject());
} // 一个路径下有多个发方法如postget因此是一个 JSONObject 类型
paths.getJSONObject(apiDefinition.getPath()).put(apiDefinition.getMethod().toLowerCase(), methodDetail);
}
result.setPaths(paths);
return result;
@ -477,7 +488,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
schema.put("format", null);
typeName.put("schema", schema);
JSONObject content = new JSONObject();
content.put(typeMap.get(type), typeName);
if (type != null && StringUtils.isNotBlank(type)) {
content.put(typeMap.get(type), typeName);
}
requestBody.put("content", content);
return requestBody;
}

View File

@ -14,4 +14,5 @@ public class SwaggerApiInfo {
private String summary; // 对应 API 的名字
private List<JSONObject> parameters; // 对应 API 的请求参数
private JSONObject requestBody;
private JSONObject responses;
}