From 8cacdad6c0f380e37eae8f87224373d2bee1436f Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Thu, 5 May 2022 16:58:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=A7=A3=E5=86=B3swagger=E5=AF=BC=E5=85=A5=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user=郭雨琦 --bug=1012975 [接口测试]github#13228swagger2.0导入接口的请求参数键值均为string类型的map字典,导入MS后在请求参数中缺少这个map https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001012975 --- .../api/dto/definition/parse/Swagger2Parser.java | 6 ++++++ .../dto/definition/request/variable/JsonSchemaItem.java | 1 + .../java/io/metersphere/commons/json/BasicConstant.java | 1 + .../io/metersphere/commons/json/JSONSchemaGenerator.java | 8 ++++++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java index b39b21f9f8..9f908b9ddb 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java @@ -303,6 +303,9 @@ public class Swagger2Parser extends SwaggerAbstractParser { item.setType("object"); ModelImpl model = (ModelImpl) schema; item.setProperties(parseNewSchemaProperties(model, refSet)); + if(model.getAdditionalProperties()!=null){ + item.setAdditionalProperties(parseProperty(model.getAdditionalProperties(), refSet)); + } } else if (schema instanceof AbstractModel) { AbstractModel abstractModel = (AbstractModel) schema; item.setType("object"); @@ -404,6 +407,9 @@ public class Swagger2Parser extends SwaggerAbstractParser { if(model.getRequired()!=null){ item.setRequired(model.getRequired()); } + if(model.getAdditionalProperties()!=null){ + item.setAdditionalProperties(parseProperty(model.getAdditionalProperties(), new HashSet<>())); + } } }else if(schema instanceof AbstractModel){ AbstractModel abstractModel = (AbstractModel) schema; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java index d31dbc1930..103c5772fe 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java @@ -21,6 +21,7 @@ public class JsonSchemaItem { private List items; private Map mock; private Map properties; + private JsonSchemaItem additionalProperties; private List required ; @JSONField(name = "$schema") diff --git a/backend/src/main/java/io/metersphere/commons/json/BasicConstant.java b/backend/src/main/java/io/metersphere/commons/json/BasicConstant.java index a76bf2f732..2e36db7e8a 100644 --- a/backend/src/main/java/io/metersphere/commons/json/BasicConstant.java +++ b/backend/src/main/java/io/metersphere/commons/json/BasicConstant.java @@ -4,6 +4,7 @@ public class BasicConstant { public final static String REQUIRED = "required"; public final static String ALL_OF = "allOf"; public final static String PROPERTIES = "properties"; + public final static String ADDITIONAL_PROPERTIES = "additionalProperties"; public final static String TYPE = "type"; public final static String MS_OBJECT = "MS-OBJECT"; public final static String ARRAY = "array"; diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java index f17cf7f917..5519f73962 100644 --- a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java +++ b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java @@ -54,7 +54,11 @@ public class JSONSchemaGenerator { JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); analyzeProperty(rootObj, propertyKey, propertyObj); } - } else if (object.has(BasicConstant.TYPE) && object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.ARRAY)) { + }else if (object.has(BasicConstant.ADDITIONAL_PROPERTIES)) { + JsonObject propertiesObj = object.get(BasicConstant.ADDITIONAL_PROPERTIES).getAsJsonObject(); + analyzeProperty(rootObj, BasicConstant.ADDITIONAL_PROPERTIES, propertiesObj); + } + else if (object.has(BasicConstant.TYPE) && object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.ARRAY)) { analyzeProperty(rootObj, BasicConstant.MS_OBJECT, object); } else if (object.has(BasicConstant.TYPE) && !object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.OBJECT)) { analyzeProperty(rootObj, object.getAsString(), object); @@ -94,7 +98,7 @@ public class JSONSchemaGenerator { } } else if (propertyObjType.equals(BasicConstant.STRING)) { // 先设置空值 - concept.put(propertyName, ""); + concept.put(propertyName,""); if (object.has("format")) { String propertyFormat = object.get("format").getAsString(); if (propertyFormat.equals("date-time")) {