From 25f02084eca1f0023111c15307ef477bec643e72 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 16 Dec 2024 18:48:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=AF=BC=E5=85=A5swagger=E6=8A=A5=E7=A9=BA=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1048546 --user=陈建星 【接口测试】接口定义-导入swagger 接口文档接口的object下array内容信息没导入 https://www.tapd.cn/55049933/s/1631527 --- .../Swagger3ParserApiDefinition.java | 53 ++++++++----------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/Swagger3ParserApiDefinition.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/Swagger3ParserApiDefinition.java index 969a61b4fa..dc7bef3f2d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/Swagger3ParserApiDefinition.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/Swagger3ParserApiDefinition.java @@ -584,28 +584,40 @@ public class Swagger3ParserApiDefinition extends HttpApiDefinitionImportAbstract private JsonSchemaItem parseSchema(Schema schema, Set refModelSet) { if (schema != null) { + if (StringUtils.equals(schema.getType(), PropertyConstant.NULL)) { + return parseNull(); + } return switch (schema) { case ArraySchema arraySchema -> parseArraySchema(arraySchema, refModelSet); case ObjectSchema objectSchema -> parseObject(objectSchema, refModelSet); case MapSchema mapSchema -> parseMapObject(mapSchema, refModelSet); + case IntegerSchema integerSchema -> parseInteger(integerSchema); + case StringSchema stringSchema -> parseString(stringSchema); + case NumberSchema numberSchema -> parseNumber(numberSchema); + case BooleanSchema booleanSchema -> parseBoolean(booleanSchema); default -> { if (StringUtils.isNotBlank(schema.get$ref())) { yield parseObject(schema, refModelSet); } - yield parseSchemaBySimpleType(schema); + yield parseSchemaByType(schema, refModelSet); } }; } return null; } - private JsonSchemaItem parseSchemaBySimpleType(Schema schema) { + private JsonSchemaItem parseSchemaByType(Schema schema, Set refModelSet) { String type = schema.getType(); + if (type == null) { + return null; + } return switch (type) { case PropertyConstant.STRING -> parseString(schema); case PropertyConstant.INTEGER -> parseInteger(schema); case PropertyConstant.NUMBER -> parseNumber(schema); case PropertyConstant.BOOLEAN -> parseBoolean(schema); + case PropertyConstant.OBJECT -> parseObject(schema, refModelSet); + case PropertyConstant.ARRAY -> parseArraySchema(schema, refModelSet); default -> { JsonSchemaItem jsonSchemaItem = new JsonSchemaItem(); jsonSchemaItem.setId(IDGenerator.nextStr()); @@ -628,16 +640,8 @@ public class Swagger3ParserApiDefinition extends HttpApiDefinitionImportAbstract return jsonSchemaItem; } JsonSchemaItem item = new JsonSchemaItem(); - switch (value) { - case IntegerSchema integerSchema -> item = parseInteger(integerSchema); - case StringSchema stringSchema -> item = parseString(stringSchema); - case NumberSchema numberSchema -> item = parseNumber(numberSchema); - case BooleanSchema booleanSchema -> item = parseBoolean(booleanSchema); - case ArraySchema arraySchema -> item = parseArraySchema(arraySchema, refModelSet); - case ObjectSchema objectSchemaItem -> item = parseObject(objectSchemaItem, refModelSet); - case Schema schema -> item = parseSchema(schema, refModelSet); - default -> { - } + if (value instanceof Schema schema) { + item = parseSchema(schema, refModelSet); } jsonSchemaProperties.put(StringUtils.EMPTY, item); jsonSchemaItem.setProperties(jsonSchemaProperties); @@ -664,7 +668,7 @@ public class Swagger3ParserApiDefinition extends HttpApiDefinitionImportAbstract } if (MapUtils.isNotEmpty(properties)) { properties.forEach((key, value) -> { - JsonSchemaItem item = parseProperty(value, refModelSet); + JsonSchemaItem item = parseSchema(value, refModelSet); jsonSchemaProperties.put(key, item); }); } @@ -672,23 +676,6 @@ public class Swagger3ParserApiDefinition extends HttpApiDefinitionImportAbstract return jsonSchemaItem; } - private JsonSchemaItem parseProperty(Schema value, Set refModelSet) { - if (StringUtils.equals(value.getType(), PropertyConstant.NULL)) { - return parseNull(); - } - - return switch (value) { - case IntegerSchema integerSchema -> parseInteger(integerSchema); - case StringSchema stringSchema -> parseString(stringSchema); - case NumberSchema numberSchema -> parseNumber(numberSchema); - case BooleanSchema booleanSchema -> parseBoolean(booleanSchema); - case ArraySchema arraySchema -> parseArraySchema(arraySchema, refModelSet); - case ObjectSchema objectSchema -> parseObject(objectSchema, refModelSet); - case MapSchema mapSchema -> parseMapObject(mapSchema, refModelSet); - case Schema schema -> parseSchema(schema, refModelSet); - }; - } - private JsonSchemaItem parseString(Schema stringSchema) { JsonSchemaItem jsonSchemaItem = parseSchemaItem(stringSchema); jsonSchemaItem.setType(PropertyConstant.STRING); @@ -750,8 +737,10 @@ public class Swagger3ParserApiDefinition extends HttpApiDefinitionImportAbstract jsonSchemaArray.setMaxItems(arraySchema.getMaxItems()); jsonSchemaArray.setMinItems(arraySchema.getMinItems()); - JsonSchemaItem itemsJsonSchema = parseProperty(arraySchema.getItems(), refModelSet); - jsonSchemaArray.setItems(List.of(itemsJsonSchema)); + JsonSchemaItem itemsJsonSchema = parseSchema(arraySchema.getItems(), refModelSet); + if (itemsJsonSchema != null) { + jsonSchemaArray.setItems(List.of(itemsJsonSchema)); + } return jsonSchemaArray; }