From 14d59e0ce221b7f7354bf9ae006a4e5bf9859856 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Fri, 25 Nov 2022 16:44:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dswagger=E6=96=87=E4=BB=B6=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001019960 --- .../io/metersphere/api/parse/api/Swagger2Parser.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java index 7b01462513..c9c1a03392 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java @@ -49,6 +49,10 @@ public class Swagger2Parser extends SwaggerAbstractParser { } else { sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String + //注:有一特殊情况,swagger2.0 文件里如果在response的parameter参数下的properties的参数里存在 required 为string类型, + //swagger2.0不会导入,需替换一下 + sourceStr = replaceStr(sourceStr); + JSONObject jsonObject = JSONUtil.parseObject(sourceStr); 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) { @@ -57,9 +61,9 @@ public class Swagger2Parser extends SwaggerAbstractParser { } 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(); - return swagger3Parser.parse(sourceStr, request); } ApiDefinitionImport definitionImport = new ApiDefinitionImport(); @@ -68,6 +72,10 @@ public class Swagger2Parser extends SwaggerAbstractParser { return definitionImport; } + public static String replaceStr(String sourceStr) { + return sourceStr.replaceAll("\"required\": \".*\"", "\"required\": []"); + } + // 鉴权设置 private List setAuths(ApiTestImportRequest request) { List auths = new ArrayList<>();