From 530c3b498e70fec398df24774f59c98f4fb91feb Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Thu, 13 Jul 2023 13:59:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=84=9A=E6=9C=AC=E4=B8=AD=E4=BD=BF=E7=94=A8?= =?UTF-8?q?API=E5=AF=BC=E5=85=A5=EF=BC=8C=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B8=AD=E6=B2=A1=E6=9C=89json-schema?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1027752 --user=王孝刚 【项目设置】github#25553,API 请求体开启 json-schema 后,在代码片段使用API导入,生成的代码使用的接口参数非最新数据 https://www.tapd.cn/55049933/s/1392604 --- .../definition/ApiDefinitionService.java | 18 ++++++++++++- .../definition/ApiTestCaseService.java | 25 ++++++++++++++++--- .../common/function/ScriptNavMenu.vue | 3 +++ .../metersphere/service/ApiPluginService.java | 2 +- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java index 49ccfad8ef..810dae7540 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java @@ -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 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 fixedOrderComparator = new FixedOrderComparator(request.getIds()); fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE); @@ -1741,7 +1757,7 @@ public class ApiDefinitionService { } public List getApiByIds(List ids) { - if(CollectionUtils.isNotEmpty(ids)) { + if (CollectionUtils.isNotEmpty(ids)) { List list = extApiDefinitionMapper.selectApiByIds(ids); buildCustomField(list); return list; diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java index cb2c062605..4b7b3c9eb9 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java @@ -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); @@ -286,11 +288,12 @@ public class ApiTestCaseService { } public List selectByCaseIds(List ids) { - if(CollectionUtils.isEmpty(ids)){ + if (CollectionUtils.isEmpty(ids)) { return new ArrayList<>(); } return extApiTestCaseMapper.selectByCaseIds(ids); } + public ApiTestCaseInfo getResult(String id) { return extApiTestCaseMapper.selectApiCaseInfoByPrimaryKey(id); } @@ -439,7 +442,7 @@ public class ApiTestCaseService { requestOrg, test.getRequest(), test.getCreateUserId() - ); + ); } // 存储附件关系 extFileAssociationService.saveApi(test.getId(), request.getRequest(), FileAssociationTypeEnums.CASE.name()); @@ -514,7 +517,7 @@ public class ApiTestCaseService { null, test.getRequest(), test.getCreateUserId() - ); + ); // 存储附件关系 extFileAssociationService.saveApi(test.getId(), request.getRequest(), FileAssociationTypeEnums.CASE.name()); return test; @@ -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 fixedOrderComparator = new FixedOrderComparator(request.getIds()); diff --git a/api-test/frontend/src/business/automation/scenario/common/function/ScriptNavMenu.vue b/api-test/frontend/src/business/automation/scenario/common/function/ScriptNavMenu.vue index a4a3d2ddc7..914b76ddc6 100644 --- a/api-test/frontend/src/business/automation/scenario/common/function/ScriptNavMenu.vue +++ b/api-test/frontend/src/business/automation/scenario/common/function/ScriptNavMenu.vue @@ -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, diff --git a/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java b/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java index b2af6bc49d..f07e2a0f1f 100644 --- a/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java +++ b/system-setting/backend/src/main/java/io/metersphere/service/ApiPluginService.java @@ -35,7 +35,6 @@ public class ApiPluginService { @Resource private PluginMapper pluginMapper; - PluginManager pluginManager = new PluginManager(); public List addApiPlugin(MultipartFile file) { String id = UUID.randomUUID().toString(); String path = FileUtils.create(id, file); @@ -83,6 +82,7 @@ public class ApiPluginService { private List getMethod(String path, String fileName, MultipartFile file) { List resources = new LinkedList<>(); validatePluginType(path); + PluginManager pluginManager = new PluginManager(); try { PluginManagerUtil.loadPlugin(path, pluginManager,file); PluginClassLoader classLoader = pluginManager.getClassLoader(path);