refactor(接口测试): 优化json-schema定义
This commit is contained in:
parent
117a0c3d33
commit
e4b3bfc5ae
|
@ -11,7 +11,7 @@ import io.metersphere.api.dto.request.ApiEditPosRequest;
|
|||
import io.metersphere.api.dto.request.ApiTransferRequest;
|
||||
import io.metersphere.api.dto.request.ImportRequest;
|
||||
import io.metersphere.api.service.ApiFileResourceService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionImportUtilService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionImportService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionLogService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionNoticeService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionService;
|
||||
|
@ -60,7 +60,7 @@ public class ApiDefinitionController {
|
|||
@Resource
|
||||
private ApiFileResourceService apiFileResourceService;
|
||||
@Resource
|
||||
private ApiDefinitionImportUtilService apiDefinitionImportUtilService;
|
||||
private ApiDefinitionImportService apiDefinitionImportService;
|
||||
|
||||
@PostMapping(value = "/add")
|
||||
@Operation(summary = "接口测试-接口管理-添加接口定义")
|
||||
|
@ -224,7 +224,7 @@ public class ApiDefinitionController {
|
|||
@Operation(summary = "接口测试-接口管理-导入接口定义")
|
||||
public void testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ImportRequest request) {
|
||||
request.setUserId(SessionUtils.getUserId());
|
||||
apiDefinitionImportUtilService.apiTestImport(file, request, SessionUtils.getCurrentProjectId());
|
||||
apiDefinitionImportService.apiTestImport(file, request, SessionUtils.getCurrentProjectId());
|
||||
}
|
||||
|
||||
@PostMapping("/operation-history")
|
||||
|
|
|
@ -21,10 +21,6 @@ import java.util.Map;
|
|||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class JsonSchemaItem {
|
||||
/**
|
||||
* 示例值 类似于 hangman 或者 @string 如果是mock 也是用这个属性
|
||||
*/
|
||||
private Object example;
|
||||
/**
|
||||
* 参数ID
|
||||
*/
|
||||
|
@ -33,6 +29,12 @@ public class JsonSchemaItem {
|
|||
* 参数名称
|
||||
*/
|
||||
private String title;
|
||||
/**
|
||||
* 示例值
|
||||
* 这里需要支持填写mock函数
|
||||
* 类型为 String
|
||||
*/
|
||||
private String example;
|
||||
/**
|
||||
* 参数类型
|
||||
* 取值范围,参考 {@link JsonSchemaItemType}
|
||||
|
@ -57,6 +59,7 @@ public class JsonSchemaItem {
|
|||
private Map<String, JsonSchemaItem> properties;
|
||||
/**
|
||||
* 附加属性
|
||||
* 包含未在属性列表中定义的额外属性的选项
|
||||
* 当 type 为 object 时,使用该值
|
||||
*/
|
||||
private JsonSchemaItem additionalProperties;
|
||||
|
@ -65,7 +68,11 @@ public class JsonSchemaItem {
|
|||
*/
|
||||
private List<String> required;
|
||||
/**
|
||||
* 正则表达式
|
||||
* 默认值
|
||||
*/
|
||||
private Object defaultValue;
|
||||
/**
|
||||
* 参数值需满足的正则表达式
|
||||
*/
|
||||
private String pattern;
|
||||
/**
|
||||
|
@ -84,30 +91,18 @@ public class JsonSchemaItem {
|
|||
* 最大值
|
||||
*/
|
||||
private BigDecimal maximum;
|
||||
/**
|
||||
* schema 一般是解析用的
|
||||
*/
|
||||
private String schema;
|
||||
/**
|
||||
* 一般是选择日期格式
|
||||
*/
|
||||
private String format;
|
||||
/**
|
||||
* 字符串的枚举
|
||||
* 参数值的枚举
|
||||
*/
|
||||
private List<String> enumString;
|
||||
private List<? extends Object> enumValues;
|
||||
/**
|
||||
* 整数的枚举
|
||||
* 是否启用
|
||||
*/
|
||||
private List<Number> enumInteger;
|
||||
/**
|
||||
* 数字的枚举
|
||||
*/
|
||||
private List<BigDecimal> enumNumber;
|
||||
/**
|
||||
* 延伸 一般是用来存放一些自定义的属性
|
||||
*/
|
||||
private Map<String, Object> extensions = null;
|
||||
private Boolean enable = true;
|
||||
|
||||
public JsonSchemaItem(String type) {
|
||||
this.type = type;
|
||||
|
|
|
@ -8,9 +8,9 @@ import org.apache.commons.lang3.StringUtils;
|
|||
public class ImportParserFactory {
|
||||
public static ImportParser<?> getImportParser(String platform) {
|
||||
if (StringUtils.equals(ApiImportPlatform.Swagger3.name(), platform)) {
|
||||
return new Swagger3Parser<>();
|
||||
return new Swagger3Parser();
|
||||
} else if (StringUtils.equals(ApiImportPlatform.Postman.name(), platform)) {
|
||||
return new PostmanParser<>();
|
||||
return new PostmanParser();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ import java.io.InputStream;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PostmanParser<T> extends PostmanAbstractParserParser<ApiDefinitionImport> {
|
||||
public class PostmanParser extends PostmanAbstractParserParser<ApiDefinitionImport> {
|
||||
|
||||
@Override
|
||||
public ApiDefinitionImport parse(InputStream source, ImportRequest request) throws JsonProcessingException {
|
||||
|
|
|
@ -41,7 +41,7 @@ import java.io.InputStream;
|
|||
import java.util.*;
|
||||
|
||||
|
||||
public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImport> {
|
||||
public class Swagger3Parser extends ApiImportAbstractParser<ApiDefinitionImport> {
|
||||
|
||||
protected String projectId;
|
||||
private Components components;
|
||||
|
@ -184,7 +184,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
|||
if (value != null && !StringUtils.equals(PropertyConstant.OBJECT, value.getType())) {
|
||||
FormDataKV formDataKV = new FormDataKV();
|
||||
formDataKV.setKey(key);
|
||||
formDataKV.setValue(String.valueOf(value.getExample()));
|
||||
formDataKV.setValue(value.getExample());
|
||||
formDataKV.setRequired(CollectionUtils.isNotEmpty(required) && required.contains(key));
|
||||
formDataKV.setDescription(value.getDescription());
|
||||
formDataKV.setParamType(value.getType());
|
||||
|
@ -209,7 +209,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
|||
if (value != null && !StringUtils.equals(PropertyConstant.OBJECT, value.getType())) {
|
||||
FormDataKV formDataKV = new FormDataKV();
|
||||
formDataKV.setKey(key);
|
||||
formDataKV.setValue(String.valueOf(value.getExample()));
|
||||
formDataKV.setValue(value.getExample());
|
||||
formDataKV.setRequired(CollectionUtils.isNotEmpty(required) && required.contains(key));
|
||||
formDataKV.setDescription(value.getDescription());
|
||||
formDataKV.setParamType(value.getType());
|
||||
|
@ -670,56 +670,47 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
|||
|
||||
|
||||
private JsonSchemaItem parseString(StringSchema stringSchema) {
|
||||
JsonSchemaItem jsonSchemaString = new JsonSchemaItem();
|
||||
jsonSchemaString.setType(PropertyConstant.STRING);
|
||||
jsonSchemaString.setId(IDGenerator.nextStr());
|
||||
jsonSchemaString.setFormat(StringUtils.isNotBlank(stringSchema.getFormat()) ? stringSchema.getFormat() : StringUtils.EMPTY);
|
||||
jsonSchemaString.setDescription(getDefaultStringValue(stringSchema.getDescription()));
|
||||
jsonSchemaString.setExample(stringSchema.getExample());
|
||||
if (stringSchema.getMaxLength() != null) {
|
||||
jsonSchemaString.setMaxLength(stringSchema.getMaxLength());
|
||||
}
|
||||
if (stringSchema.getMinLength() != null) {
|
||||
jsonSchemaString.setMinLength(stringSchema.getMinLength());
|
||||
}
|
||||
jsonSchemaString.setPattern(stringSchema.getPattern());
|
||||
jsonSchemaString.setEnumString(stringSchema.getEnum());
|
||||
if (stringSchema.getExample() == null && CollectionUtils.isNotEmpty(stringSchema.getEnum())) {
|
||||
jsonSchemaString.setExample(stringSchema.getEnum().getFirst());
|
||||
}
|
||||
return jsonSchemaString;
|
||||
JsonSchemaItem jsonSchemaItem = parseSchemaItem(stringSchema);
|
||||
jsonSchemaItem.setType(PropertyConstant.STRING);
|
||||
jsonSchemaItem.setFormat(getDefaultStringValue(stringSchema.getFormat()));
|
||||
jsonSchemaItem.setDescription(getDefaultStringValue(stringSchema.getDescription()));
|
||||
jsonSchemaItem.setMaxLength(stringSchema.getMaxLength());
|
||||
jsonSchemaItem.setMinLength(stringSchema.getMinLength());
|
||||
jsonSchemaItem.setPattern(stringSchema.getPattern());
|
||||
jsonSchemaItem.setEnumValues(stringSchema.getEnum());
|
||||
return jsonSchemaItem;
|
||||
}
|
||||
|
||||
private JsonSchemaItem parseInteger(IntegerSchema integerSchema) {
|
||||
JsonSchemaItem jsonSchemaInteger = new JsonSchemaItem();
|
||||
jsonSchemaInteger.setType(PropertyConstant.INTEGER);
|
||||
jsonSchemaInteger.setId(IDGenerator.nextStr());
|
||||
jsonSchemaInteger.setFormat(StringUtils.isNotBlank(integerSchema.getFormat()) ? integerSchema.getFormat() : StringUtils.EMPTY);
|
||||
jsonSchemaInteger.setDescription(StringUtils.isNotBlank(integerSchema.getDescription()) ? integerSchema.getDescription() : StringUtils.EMPTY);
|
||||
jsonSchemaInteger.setExample(integerSchema.getExample());
|
||||
jsonSchemaInteger.setMaximum(integerSchema.getMaximum());
|
||||
jsonSchemaInteger.setMinimum(integerSchema.getMinimum());
|
||||
jsonSchemaInteger.setEnumInteger(integerSchema.getEnum());
|
||||
return jsonSchemaInteger;
|
||||
JsonSchemaItem jsonSchemaItem = parseSchemaItem(integerSchema);
|
||||
jsonSchemaItem.setType(PropertyConstant.INTEGER);
|
||||
jsonSchemaItem.setFormat(StringUtils.isNotBlank(integerSchema.getFormat()) ? integerSchema.getFormat() : StringUtils.EMPTY);
|
||||
jsonSchemaItem.setMaximum(integerSchema.getMaximum());
|
||||
jsonSchemaItem.setMinimum(integerSchema.getMinimum());
|
||||
jsonSchemaItem.setEnumValues(integerSchema.getEnum());
|
||||
return jsonSchemaItem;
|
||||
}
|
||||
|
||||
private JsonSchemaItem parseNumber(NumberSchema numberSchema) {
|
||||
JsonSchemaItem jsonSchemaNumber = new JsonSchemaItem();
|
||||
jsonSchemaNumber.setType(PropertyConstant.NUMBER);
|
||||
jsonSchemaNumber.setId(IDGenerator.nextStr());
|
||||
jsonSchemaNumber.setDescription(StringUtils.isNotBlank(numberSchema.getDescription()) ? numberSchema.getDescription() : StringUtils.EMPTY);
|
||||
jsonSchemaNumber.setExample(numberSchema.getExample());
|
||||
jsonSchemaNumber.setEnumNumber(numberSchema.getEnum());
|
||||
return jsonSchemaNumber;
|
||||
JsonSchemaItem jsonSchemaItem = parseSchemaItem(numberSchema);
|
||||
jsonSchemaItem.setType(PropertyConstant.NUMBER);
|
||||
return jsonSchemaItem;
|
||||
}
|
||||
|
||||
private JsonSchemaItem parseBoolean(BooleanSchema booleanSchema) {
|
||||
JsonSchemaItem jsonSchemaBoolean = new JsonSchemaItem();
|
||||
jsonSchemaBoolean.setType(PropertyConstant.BOOLEAN);
|
||||
jsonSchemaBoolean.setId(IDGenerator.nextStr());
|
||||
jsonSchemaBoolean.setDescription(getDefaultStringValue(booleanSchema.getDescription()));
|
||||
jsonSchemaBoolean.setExample(booleanSchema.getExample());
|
||||
return jsonSchemaBoolean;
|
||||
JsonSchemaItem jsonSchemaItem = parseSchemaItem(booleanSchema);
|
||||
jsonSchemaItem.setType(PropertyConstant.NUMBER);
|
||||
return jsonSchemaItem;
|
||||
}
|
||||
|
||||
private JsonSchemaItem parseSchemaItem(Schema schema) {
|
||||
JsonSchemaItem jsonSchemaItem = new JsonSchemaItem();
|
||||
jsonSchemaItem.setId(IDGenerator.nextStr());
|
||||
jsonSchemaItem.setDescription(getDefaultStringValue(schema.getDescription()));
|
||||
Optional.ofNullable(schema.getExample()).ifPresent(example -> jsonSchemaItem.setExample(example.toString()));
|
||||
jsonSchemaItem.setEnumValues(schema.getEnum());
|
||||
jsonSchemaItem.setDefaultValue(schema.getDefault());
|
||||
return jsonSchemaItem;
|
||||
}
|
||||
|
||||
private JsonSchemaItem parseNull() {
|
||||
|
|
|
@ -65,7 +65,7 @@ import java.util.stream.Collectors;
|
|||
import static io.metersphere.project.utils.NodeSortUtils.DEFAULT_NODE_INTERVAL_POS;
|
||||
|
||||
@Service
|
||||
public class ApiDefinitionImportUtilService {
|
||||
public class ApiDefinitionImportService {
|
||||
|
||||
private static final String UNPLANNED_API = "api_unplanned_request";
|
||||
private final ThreadLocal<Long> currentApiOrder = new ThreadLocal<>();
|
||||
|
@ -638,13 +638,13 @@ public class ApiDefinitionImportUtilService {
|
|||
}
|
||||
|
||||
return switch (Body.BodyType.valueOf(dbBody.getBodyType())) {
|
||||
case Body.BodyType.FORM_DATA ->
|
||||
case FORM_DATA ->
|
||||
!formBodyHaDifferent(dbBody.getFormDataBody(), importBody.getFormDataBody());
|
||||
case Body.BodyType.WWW_FORM ->
|
||||
case WWW_FORM ->
|
||||
!wwwFormBodyHasDifferent(dbBody.getWwwFormBody(), importBody.getWwwFormBody());
|
||||
case Body.BodyType.RAW -> rawBodyAreSame(dbBody.getRawBody(), importBody.getRawBody());
|
||||
case Body.BodyType.JSON -> jsonBodyAreSame(dbBody.getJsonBody(), importBody.getJsonBody());
|
||||
case Body.BodyType.XML -> xmlBodyAreSame(dbBody.getXmlBody(), importBody.getXmlBody());
|
||||
case RAW -> rawBodyAreSame(dbBody.getRawBody(), importBody.getRawBody());
|
||||
case JSON -> jsonBodyAreSame(dbBody.getJsonBody(), importBody.getJsonBody());
|
||||
case XML -> xmlBodyAreSame(dbBody.getXmlBody(), importBody.getXmlBody());
|
||||
default -> true;
|
||||
};
|
||||
}
|
|
@ -4,7 +4,7 @@ package io.metersphere.api.service.schedule;
|
|||
import io.metersphere.api.constants.ApiImportPlatform;
|
||||
import io.metersphere.api.dto.definition.ApiScheduleDTO;
|
||||
import io.metersphere.api.dto.request.ImportRequest;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionImportUtilService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionImportService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionScheduleService;
|
||||
import io.metersphere.sdk.util.BeanUtils;
|
||||
import io.metersphere.sdk.util.CommonBeanFactory;
|
||||
|
@ -16,12 +16,12 @@ import org.quartz.JobKey;
|
|||
import org.quartz.TriggerKey;
|
||||
|
||||
public class SwaggerUrlImportJob extends BaseScheduleJob {
|
||||
private ApiDefinitionImportUtilService apiDefinitionImportUtilService;
|
||||
private ApiDefinitionImportService apiDefinitionImportService;
|
||||
private ApiDefinitionScheduleService apiDefinitionScheduleService;
|
||||
private SimpleUserService simpleUserService;
|
||||
|
||||
public SwaggerUrlImportJob() {
|
||||
apiDefinitionImportUtilService = CommonBeanFactory.getBean(ApiDefinitionImportUtilService.class);
|
||||
apiDefinitionImportService = CommonBeanFactory.getBean(ApiDefinitionImportService.class);
|
||||
apiDefinitionScheduleService = CommonBeanFactory.getBean(ApiDefinitionScheduleService.class);
|
||||
simpleUserService = CommonBeanFactory.getBean(SimpleUserService.class);
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ public class SwaggerUrlImportJob extends BaseScheduleJob {
|
|||
request.setUserId(jobDataMap.getString("userId"));
|
||||
request.setType("SCHEDULE");
|
||||
request.setResourceId(resourceId);
|
||||
apiDefinitionImportUtilService.apiTestImport(null, request, request.getProjectId());
|
||||
apiDefinitionImportService.apiTestImport(null, request, request.getProjectId());
|
||||
}
|
||||
|
||||
public static JobKey getJobKey(String resourceId) {
|
||||
|
|
|
@ -126,7 +126,7 @@ public class JsonSchemaBuilder {
|
|||
}
|
||||
|
||||
public static String preview(String jsonSchema) {
|
||||
String jsonString = JsonSchemaBuilder.jsonSchemaToJson(jsonSchema);
|
||||
String jsonString = jsonSchemaToJson(jsonSchema);
|
||||
//需要匹配到mock函数 然后换成mock数据
|
||||
if (StringUtils.isNotBlank(jsonString)) {
|
||||
String pattern = "@[a-zA-Z\\\\(|,'-\\\\d ]*[a-zA-Z)-9),\\\\\"]";
|
||||
|
|
Loading…
Reference in New Issue