fix(接口测试): 修复脚本中使用API导入,生成的代码中没有json-schema参数的缺陷

--bug=1027752 --user=王孝刚 【项目设置】github#25553,API 请求体开启 json-schema
后,在代码片段使用API导入,生成的代码使用的接口参数非最新数据 https://www.tapd.cn/55049933/s/1392604
This commit is contained in:
wxg0103 2023-07-13 13:59:15 +08:00 committed by fit2-zhao
parent 6b82558cff
commit 530c3b498e
4 changed files with 42 additions and 6 deletions

View File

@ -14,9 +14,11 @@ import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult;
import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest;
import io.metersphere.api.exec.api.ApiExecuteService;
import io.metersphere.api.exec.generator.JSONSchemaParser;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.parse.ApiImportParser;
import io.metersphere.api.parse.api.ApiDefinitionImport;
@ -327,6 +329,20 @@ public class ApiDefinitionService {
return new ArrayList<>();
}
List<ApiDefinitionResult> resList = extApiDefinitionMapper.listByIds(request.getIds());
resList.forEach(item -> {
MsTestElement msTestElement = JSONUtil.parseObject(item.getRequest(), MsTestElement.class);
if (msTestElement instanceof MsHTTPSamplerProxy) {
MsHTTPSamplerProxy requestBody = (MsHTTPSamplerProxy) msTestElement;
Body body = requestBody.getBody();
if (StringUtils.isNotBlank(body.getType()) && StringUtils.equals(body.getType(), Body.JSON_STR)) {
if (StringUtils.isNotEmpty(body.getFormat()) && body.getJsonSchema() != null && Body.JSON_SCHEMA.equals(body.getFormat())) {
body.setRaw(JSONSchemaParser.preview(JSONUtil.toJSONString(body.getJsonSchema())));
}
}
item.setRequest(JSONUtil.toJSONString(requestBody));
}
}
);
// 排序
FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds());
fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE);

View File

@ -11,6 +11,8 @@ import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.MsTestPlan;
import io.metersphere.api.dto.definition.request.MsThreadGroup;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.exec.generator.JSONSchemaParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.*;
@ -115,10 +117,10 @@ public class ApiTestCaseService {
private BaseProjectService baseProjectService;
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;
private static final String DEFAULT_TIME_DATE = "-3D";
//查询测试用例详情
public ApiTestCaseWithBLOBs getInfoJenkins(String id) {
ApiTestCaseWithBLOBs apiTest = apiTestCaseMapper.selectByPrimaryKey(id);
@ -291,6 +293,7 @@ public class ApiTestCaseService {
}
return extApiTestCaseMapper.selectByCaseIds(ids);
}
public ApiTestCaseInfo getResult(String id) {
return extApiTestCaseMapper.selectApiCaseInfoByPrimaryKey(id);
}
@ -847,6 +850,20 @@ public class ApiTestCaseService {
list = new ArrayList<>();
} else {
list = extApiTestCaseMapper.getCaseInfo(request);
list.forEach(item -> {
MsTestElement msTestElement = JSONUtil.parseObject(item.getRequest(), MsTestElement.class);
if (msTestElement instanceof MsHTTPSamplerProxy) {
MsHTTPSamplerProxy requestBody = (MsHTTPSamplerProxy) JSONUtil.parseObject(item.getRequest(), MsTestElement.class);
Body body = requestBody.getBody();
if (StringUtils.isNotBlank(body.getType()) && StringUtils.equals(body.getType(), Body.JSON_STR)) {
if (StringUtils.isNotEmpty(body.getFormat()) && body.getJsonSchema() != null && Body.JSON_SCHEMA.equals(body.getFormat())) {
body.setRaw(JSONSchemaParser.preview(JSONUtil.toJSONString(body.getJsonSchema())));
}
}
item.setRequest(JSONUtil.toJSONString(requestBody));
}
}
);
}
// 排序
FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds());

View File

@ -177,6 +177,9 @@ export default {
requestBodyKvs.set(arg.name, arg.value);
}
});
} else if (body.type === 'JSON' && body.format === 'JSON-SCHEMA') {
requestBody = body.raw;
bodyType = 'json';
}
return {
requestPath,

View File

@ -35,7 +35,6 @@ public class ApiPluginService {
@Resource
private PluginMapper pluginMapper;
PluginManager pluginManager = new PluginManager();
public List<PluginWithBLOBs> addApiPlugin(MultipartFile file) {
String id = UUID.randomUUID().toString();
String path = FileUtils.create(id, file);
@ -83,6 +82,7 @@ public class ApiPluginService {
private List<PluginResourceDTO> getMethod(String path, String fileName, MultipartFile file) {
List<PluginResourceDTO> resources = new LinkedList<>();
validatePluginType(path);
PluginManager pluginManager = new PluginManager();
try {
PluginManagerUtil.loadPlugin(path, pluginManager,file);
PluginClassLoader classLoader = pluginManager.getClassLoader(path);