diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java index 3e3712197c..1e52a257c0 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java @@ -28,6 +28,8 @@ public class JsonSchemaItem { private BigDecimal minimum; private BigDecimal maximum; private String schema; + protected Object defaultValue; + public JsonSchemaItem() { this.mock = new LinkedHashMap<>(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java index b81862c043..c096c2267f 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java @@ -41,6 +41,14 @@ public class KeyValue { this.enable = enable; } + public KeyValue(String name, String value, String description, boolean required, Integer min, Integer max) { + this.name = name; + this.value = value; + this.description = description; + this.required = required; + this.min = min; + this.max = max; + } public KeyValue(String name, String value, String description, String contentType) { this(name, value, description, contentType, true); } 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 49ab47752d..3134c70b10 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 @@ -328,12 +328,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { Set refSet = new HashSet<>(); Map infoMap = new HashMap(); - Schema schema = mediaType.getSchema(); - if (StringUtils.isBlank(schema.get$ref()) && schema.getItems() == null && StringUtils.isNotBlank(schema.getType()) && StringUtils.equals(schema.getType(), "string")) { - ObjectSchema objectSchema = new ObjectSchema(); - objectSchema.setExample(schema.getExample()); - schema = objectSchema; - } + Schema schema = getSchema(mediaType.getSchema()); Object bodyData = null; if (!StringUtils.equals(contentType, org.springframework.http.MediaType.APPLICATION_JSON_VALUE)) { bodyData = parseSchemaToJson(schema, refSet, infoMap); @@ -535,6 +530,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { item.setMinLength(schema.getMinLength()); item.setMaximum(schema.getMaximum()); item.setMinimum(schema.getMinimum()); + item.setDefaultValue(schema.getDefault()); return item; } @@ -572,7 +568,46 @@ public class Swagger3Parser extends SwaggerAbstractParser { private void parseQueryParameters(Parameter parameter, List arguments) { QueryParameter queryParameter = (QueryParameter) parameter; - arguments.add(new KeyValue(queryParameter.getName(), String.valueOf(queryParameter.getExample()), getDefaultStringValue(queryParameter.getDescription()), parameter.getRequired())); + Schema schema = getSchema(parameter.getSchema()); + Set refSet = new HashSet<>(); + JsonSchemaItem jsonSchemaItem = parseSchema(schema, refSet); + arguments.add(new KeyValue(queryParameter.getName(), getDefaultValue(queryParameter, jsonSchemaItem), getDefaultStringValue(queryParameter.getDescription()), parameter.getRequired(), getMin(jsonSchemaItem), getMax(jsonSchemaItem))); + } + + private Schema getSchema(Schema schema) { + if (StringUtils.isBlank(schema.get$ref()) && StringUtils.isNotBlank(schema.getType()) && StringUtils.equals(schema.getType(),"string")) { + ObjectSchema objectSchema = new ObjectSchema(); + objectSchema.setExample(schema.getExample()); + schema = objectSchema; + } + return schema; + } + + private Integer getMax(JsonSchemaItem jsonSchemaItem) { + if (jsonSchemaItem != null && jsonSchemaItem.getMaxLength() != null) { + return jsonSchemaItem.getMaxLength(); + } else { + return null; + } + } + + private Integer getMin(JsonSchemaItem jsonSchemaItem) { + if (jsonSchemaItem != null && jsonSchemaItem.getMinLength() != null) { + return jsonSchemaItem.getMinLength(); + } else { + return null; + } + } + + private String getDefaultValue(QueryParameter queryParameter, JsonSchemaItem jsonSchemaItem) { + if (queryParameter.getExample() != null) { + return String.valueOf(queryParameter.getExample()); + } else { + if (jsonSchemaItem != null && jsonSchemaItem.getDefaultValue()!=null) { + return String.valueOf(jsonSchemaItem.getDefaultValue()); + } + return null; + } } /* 导出的 swagger json描述文件样例