From 59ea61761a06a0e8b07f13ebc93d72573772dae8 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Fri, 14 Jul 2023 11:16:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8Dswagger=E5=AF=BC=E5=85=A5=E5=BF=85?= =?UTF-8?q?=E9=80=89=E6=9C=AA=E9=80=89=E4=B8=8A=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1027488 --user=王孝刚 【接口测试】github#25290,【接口测试】通过导入swagger json 格式文件,导入MeterSphere之后,没有勾选必填框 https://www.tapd.cn/55049933/s/1393416 --- .../metersphere/api/parse/api/Swagger2Parser.java | 7 +++++++ .../metersphere/api/parse/api/Swagger3Parser.java | 14 ++++++++------ 2 files changed, 15 insertions(+), 6 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 d4f578e795..d397fe48ee 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 @@ -567,6 +567,13 @@ public class Swagger2Parser extends SwaggerAbstractParser { } else if (value instanceof ObjectProperty) { subItem.setType(PropertyConstant.OBJECT); subItem.setProperties(parseSchemaProperties(((ObjectProperty) value).getProperties(), refSet)); + List required = new ArrayList<>(); + ((ObjectProperty) value).getProperties().forEach((key, property) -> { + if (property.getRequired()) { + required.add(key); + } + }); + subItem.setRequired(required); } else { handleBaseProperties(subItem, value); } diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java index e3457f9d29..62d1a83706 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java @@ -875,6 +875,9 @@ public class Swagger3Parser extends SwaggerAbstractParser { } } else if (StringUtils.equals(type, PropertyConstant.OBJECT)) { parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT); + if (requestBody.optJSONArray(PropertyConstant.REQUIRED) != null) { + parsedParam.put(PropertyConstant.REQUIRED, requestBody.optJSONArray(PropertyConstant.REQUIRED)); + } JSONObject properties = requestBody.optJSONObject(PropertyConstant.PROPERTIES); JSONObject jsonObject = buildFormDataSchema(properties); if (StringUtils.isNotBlank(requestBody.optString("description"))) { @@ -1062,11 +1065,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { property.put("example", value); } property.put("description", obj.optString("description")); - property.put(PropertyConstant.REQUIRED, obj.optString(PropertyConstant.REQUIRED)); - if (obj.optJSONObject(PropertyConstant.REQUIRED) != null) { - JSONObject childProperties = buildFormDataSchema(obj.optJSONObject(PropertyConstant.REQUIRED)); - property.put(PropertyConstant.REQUIRED, childProperties.optJSONObject(PropertyConstant.REQUIRED)); - } + property.put(PropertyConstant.REQUIRED, obj.optJSONArray(PropertyConstant.REQUIRED)); if (obj.optJSONObject(PropertyConstant.PROPERTIES) != null) { JSONObject childProperties = buildFormDataSchema(obj.optJSONObject(PropertyConstant.PROPERTIES)); property.put(PropertyConstant.PROPERTIES, childProperties.optJSONObject(PropertyConstant.PROPERTIES)); @@ -1227,7 +1226,10 @@ public class Swagger3Parser extends SwaggerAbstractParser { items.forEach(item -> { if (item instanceof JSONObject) { JSONObject itemRequired = ((JSONObject) item).optJSONObject(PropertyConstant.REQUIRED); - finalRequired.put(itemRequired); + if (itemRequired != null) { + finalRequired.put(itemRequired); + } + finalRequired.putAll(((JSONObject) item).optJSONArray(PropertyConstant.REQUIRED)); } }); required = finalRequired;