fix(接口测试): 修复swagger文件导入失败问题

--user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001019960
This commit is contained in:
guoyuqi 2022-11-25 16:44:03 +08:00 committed by fit2-zhao
parent 6ea6859227
commit 14d59e0ce2
1 changed files with 10 additions and 2 deletions

View File

@ -49,6 +49,10 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} else { } else {
sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String
//有一特殊情况swagger2.0 文件里如果在response的parameter参数下的properties的参数里存在 required 为string类型
//swagger2.0不会导入需替换一下
sourceStr = replaceStr(sourceStr);
JSONObject jsonObject = JSONUtil.parseObject(sourceStr); JSONObject jsonObject = JSONUtil.parseObject(sourceStr);
if (jsonObject.opt("swagger") == null || jsonObject.opt("swagger") == "null" || jsonObject.opt("swagger") == StringUtils.SPACE) { if (jsonObject.opt("swagger") == null || jsonObject.opt("swagger") == "null" || jsonObject.opt("swagger") == StringUtils.SPACE) {
if (jsonObject.opt("openapi") == null || jsonObject.opt("openapi") == "null" || jsonObject.opt("openapi") == StringUtils.SPACE) { if (jsonObject.opt("openapi") == null || jsonObject.opt("openapi") == "null" || jsonObject.opt("openapi") == StringUtils.SPACE) {
@ -57,9 +61,9 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} }
swagger = new SwaggerParser().readWithInfo(sourceStr, false).getSwagger(); swagger = new SwaggerParser().readWithInfo(sourceStr, false).getSwagger();
} }
if (swagger == null || swagger.getSwagger() == null) { // 不是 2.0 版本则尝试转换 3.0 if (swagger == null || swagger.getSwagger() == null) {
// 不是 2.0 版本则尝试转换 3.0
Swagger3Parser swagger3Parser = new Swagger3Parser(); Swagger3Parser swagger3Parser = new Swagger3Parser();
return swagger3Parser.parse(sourceStr, request); return swagger3Parser.parse(sourceStr, request);
} }
ApiDefinitionImport definitionImport = new ApiDefinitionImport(); ApiDefinitionImport definitionImport = new ApiDefinitionImport();
@ -68,6 +72,10 @@ public class Swagger2Parser extends SwaggerAbstractParser {
return definitionImport; return definitionImport;
} }
public static String replaceStr(String sourceStr) {
return sourceStr.replaceAll("\"required\": \".*\"", "\"required\": []");
}
// 鉴权设置 // 鉴权设置
private List<AuthorizationValue> setAuths(ApiTestImportRequest request) { private List<AuthorizationValue> setAuths(ApiTestImportRequest request) {
List<AuthorizationValue> auths = new ArrayList<>(); List<AuthorizationValue> auths = new ArrayList<>();