fix(接口测试): 解决接口swagger文件导入解析错误问题

--user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001017260
This commit is contained in:
guoyuqi 2022-09-26 11:14:48 +08:00 committed by xiaomeinvG
parent ed2f3f3715
commit 3b8d299dd1
1 changed files with 18 additions and 3 deletions

View File

@ -3,6 +3,10 @@ package io.metersphere.api.dto.definition.parse;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; 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.ApiTestImportRequest;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager; import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
@ -47,11 +51,22 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} else { } else {
sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String
JSONObject jsonObject = JSONObject.parseObject(sourceStr); ObjectMapper objectMapper = new ObjectMapper();
if (jsonObject.get("swagger") == null || jsonObject.get("swagger") == "null" || jsonObject.get("swagger") == " ") { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (jsonObject.get("openapi") == null || jsonObject.get("openapi") == "null" || jsonObject.get("openapi") == " ") { try {
JsonNode jsonNode = objectMapper.readTree(sourceStr);
if (!jsonNode.has("swagger") && !jsonNode.has("openapi")) {
MSException.throwException("wrong format"); 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(); swagger = new SwaggerParser().readWithInfo(sourceStr, false).getSwagger();
} }