From 1be59b0f502ddbcb0484492b6c7143323ec55c69 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 15 May 2024 14:16:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20Swagger=20=E5=AF=BC=E5=85=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/parser/api/Swagger3Parser.java | 95 +++++++------------ 1 file changed, 36 insertions(+), 59 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java index 05202f7e29..3227839baa 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java @@ -324,7 +324,7 @@ public class Swagger3Parser extends ApiImportAbstractParser extends ApiImportAbstractParser getModelByRef(String ref) { if (StringUtils.isBlank(ref)) { return null; } @@ -453,10 +453,10 @@ public class Swagger3Parser extends ApiImportAbstractParser schema) { if (schema != null) { String refName = schema.get$ref(); - Schema modelByRef = null; + Schema modelByRef; if (StringUtils.isNotBlank(refName)) { modelByRef = getModelByRef(refName); } else { @@ -470,10 +470,9 @@ public class Swagger3Parser extends ApiImportAbstractParser properties = modelByRef.getProperties(); Map jsonSchemaProperties = new LinkedHashMap<>(); - if (MapUtils.isNotEmpty(properties)) { - properties.forEach((key, value) -> { + if (MapUtils.isNotEmpty(modelByRef.getProperties())) { + modelByRef.getProperties().forEach((key, value) -> { JsonSchemaItem item = parseProperty(value, false); jsonSchemaProperties.put(key, item); }); @@ -498,18 +497,15 @@ public class Swagger3Parser extends ApiImportAbstractParser 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.getItems(), false); + case ObjectSchema objectSchemaItem -> item = parseObject(objectSchemaItem, false); + default -> { + } } jsonSchemaProperties.put(StringUtils.EMPTY, item); jsonSchemaItem.setProperties(jsonSchemaProperties); @@ -522,10 +518,9 @@ public class Swagger3Parser extends ApiImportAbstractParser properties = objectSchema.getProperties(); Map jsonSchemaProperties = new LinkedHashMap<>(); - if (MapUtils.isNotEmpty(properties)) { - properties.forEach((key, value) -> { + if (MapUtils.isNotEmpty(objectSchema.getProperties())) { + objectSchema.getProperties().forEach((key, value) -> { JsonSchemaItem item = parseProperty(value, onlyOnce); jsonSchemaProperties.put(key, item); }); @@ -534,7 +529,7 @@ public class Swagger3Parser extends ApiImportAbstractParser value, boolean onlyOnce) { if (value instanceof IntegerSchema integerSchema) { return parseInteger(integerSchema); } @@ -589,8 +584,8 @@ public class Swagger3Parser extends ApiImportAbstractParser 20) { + private boolean isRef(Schema schema, int level) { + if (level > 20 || schema == null) { return true; } @@ -598,42 +593,24 @@ public class Swagger3Parser extends ApiImportAbstractParser isRef(arraySchema.getItems(), level + 1); + case ObjectSchema objectSchema -> hasRefInObjectSchema(objectSchema, level + 1); + default -> false; + }; } + private boolean hasRefInObjectSchema(ObjectSchema objectSchema, int level) { - Map properties = objectSchema.getProperties(); - if (MapUtils.isNotEmpty(properties)) { - for (Schema value : properties.values()) { - if (value instanceof ArraySchema && isRef(((ArraySchema) value).getItems(), level + 1) && level > 20) { - return true; - } - if (value instanceof ObjectSchema && isRef(value, level + 1) && level > 20) { - return true; - } - if (value instanceof Schema items && isRef(items, level + 1) && level > 20) { + if (level > 20) { + return true; + } + + if (MapUtils.isNotEmpty(objectSchema.getProperties())) { + for (Schema value : objectSchema.getProperties().values()) { + if (isRef(value, level + 1)) { return true; } } @@ -658,7 +635,7 @@ public class Swagger3Parser extends ApiImportAbstractParser extends ApiImportAbstractParser itemsSchema; if (StringUtils.isNotBlank(items.get$ref())) { itemsSchema = getModelByRef(items.get$ref()); } else {