fix(接口测试): 修复swagger文件导入query参数默认值导入失败问题

--user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001020352
This commit is contained in:
guoyuqi 2022-11-29 13:57:03 +08:00 committed by fit2-zhao
parent afbe89092d
commit 7d08e84f97
3 changed files with 52 additions and 7 deletions

View File

@ -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<>();

View File

@ -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);
}

View File

@ -328,12 +328,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
Set<String> refSet = new HashSet<>();
Map<String, Schema> 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<KeyValue> 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<String> 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描述文件样例