fix(接口测试): 解决接口swagger文件导入解析错误问题以及非HTTP协议覆盖模块导入报错问题

--user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001017248
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001017260
This commit is contained in:
guoyuqi 2022-09-26 11:11:29 +08:00 committed by xiaomeinvG
parent 6ca7a6ef42
commit 5db5da7229
2 changed files with 20 additions and 4 deletions

View File

@ -3,6 +3,10 @@ package io.metersphere.api.dto.definition.parse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
@ -47,12 +51,24 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} else {
sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String
JSONObject jsonObject = JSONObject.parseObject(sourceStr);
if (jsonObject.get("swagger") == null || jsonObject.get("swagger") == "null" || jsonObject.get("swagger") == " ") {
if (jsonObject.get("openapi") == null || jsonObject.get("openapi") == "null" || jsonObject.get("openapi") == " ") {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
JsonNode jsonNode = objectMapper.readTree(sourceStr);
if (!jsonNode.has("swagger") && !jsonNode.has("openapi")) {
MSException.throwException("wrong format");
}
String keyName = objectMapper.writeValueAsString(jsonNode.get("swagger"));
if (StringUtils.isBlank(keyName) || StringUtils.equals(keyName, "\"\"")) {
String openapi = objectMapper.writeValueAsString(jsonNode.get("openapi"));
if (StringUtils.isBlank(openapi) || StringUtils.equals(openapi, "\"\"")) {
MSException.throwException("wrong format");
}
}
} catch (JsonProcessingException e) {
LoggerUtil.error("解析swagger出错", e);
}
swagger = new SwaggerParser().readWithInfo(sourceStr, false).getSwagger();
}
if (swagger == null || swagger.getSwagger() == null) { // 不是 2.0 版本则尝试转换 3.0

View File

@ -713,7 +713,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
List<ApiDefinitionWithBLOBs> singleOptionDatas = new ArrayList<>();
removeOtherChooseModuleRepeat(optionDatas, singleOptionDatas, chooseModulePath);
optionDatas = singleOptionDatas;
optionMap = optionDatas.stream().collect(Collectors.toMap(t -> t.getName().concat(t.getMethod()).concat(t.getPath()).concat(chooseModulePath), api -> api));
optionMap = optionDatas.stream().collect(Collectors.toMap(t -> t.getName().concat(chooseModulePath), api -> api));
} else {
getNoHChooseModuleUrlRepeatOptionMap(optionDatas, optionMap, chooseModulePath);
}