From b4bf0c6e632ca739e2a52b75011f14f75082d8c1 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 17 Jul 2023 17:51:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E5=9C=BA=E6=99=AF=E5=AF=BC=E5=85=A5=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=8E=86=E5=8F=B2=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1027894 --user=王孝刚 【接口测试】场景详情-UI列表导入-切换项目-选择模块-选择所有数据-复制报500 https://www.tapd.cn/55049933/s/1394138 --- .../definition/ApiDefinitionService.java | 41 ++++++++++++------- .../definition/ApiTestCaseService.java | 26 ++++++------ 2 files changed, 40 insertions(+), 27 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 810dae7540..264f1c94dd 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 @@ -180,6 +180,12 @@ public class ApiDefinitionService { public static final String ARGUMENTS = "arguments"; public static final String BODY = "body"; private static final String SCHEDULE = "schedule"; + public static final String TYPE = "type"; + public static final String HTTP = "HTTPSamplerProxy"; + public static final String CLAZZ = "className"; + public static final String FORMAT = "format"; + public static final String RAW = "raw"; + public static final String JSONSCHEMA = "jsonSchema"; public List list(ApiDefinitionRequest request) { @@ -330,19 +336,21 @@ public class ApiDefinitionService { } 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)); - } + JSONObject jsonObject = JSONUtil.parseObject(item.getRequest()); + if (jsonObject != null && jsonObject.has(TYPE) && jsonObject.optString(TYPE).equals(HTTP)) { + jsonObject.put(CLAZZ, MsHTTPSamplerProxy.class.getCanonicalName()); + JSONObject body = jsonObject.optJSONObject(BODY); + if (StringUtils.isNotBlank(body.optString(TYPE)) + && StringUtils.equals(body.optString(TYPE), Body.JSON_STR) + && StringUtils.isNotEmpty(body.optString(FORMAT)) + && body.optJSONObject(JSONSCHEMA) != null + && Body.JSON_SCHEMA.equals(body.optString(FORMAT))) { + body.put(RAW, JSONSchemaParser.preview(body.optString(JSONSCHEMA))); + jsonObject.put(BODY, body); } - ); + item.setRequest(jsonObject.toString()); + } + }); // 排序 FixedOrderComparator fixedOrderComparator = new FixedOrderComparator(request.getIds()); fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE); @@ -1563,9 +1571,12 @@ public class ApiDefinitionService { */ List specifyData = new ArrayList<>(); if (StringUtils.isNotBlank(mockApiResourceId)) { - ApiDefinitionWithBLOBs apiDefinition = apiDefinitionMapper.selectByPrimaryKey(mockApiResourceId); - if (apiDefinition != null) { - specifyData.add(apiDefinition); + ApiDefinitionExample apiDefinitionExample = new ApiDefinitionExample(); + apiDefinitionExample.createCriteria().andIdEqualTo(mockApiResourceId).andStatusNotEqualTo("Trash"); + List apiDefinitions = apiDefinitionMapper.selectByExampleWithBLOBs(apiDefinitionExample); + ApiDefinitionWithBLOBs apiDefinition; + if (CollectionUtils.isNotEmpty(apiDefinitions)) { + specifyData.add(apiDefinitions.get(0)); } else { ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(mockApiResourceId); if (testCase != null) { 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 4b7b3c9eb9..87b4194084 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 @@ -851,19 +851,21 @@ public class ApiTestCaseService { } 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)); - } + JSONObject jsonObject = JSONUtil.parseObject(item.getRequest()); + if (jsonObject != null && jsonObject.has(ApiDefinitionService.TYPE) && jsonObject.optString(ApiDefinitionService.TYPE).equals(ApiDefinitionService.HTTP)) { + jsonObject.put(ApiDefinitionService.CLAZZ, MsHTTPSamplerProxy.class.getCanonicalName()); + JSONObject body = jsonObject.optJSONObject(ApiDefinitionService.BODY); + if (StringUtils.isNotBlank(body.optString(ApiDefinitionService.TYPE)) + && StringUtils.equals(body.optString(ApiDefinitionService.TYPE), Body.JSON_STR) + && StringUtils.isNotEmpty(body.optString(ApiDefinitionService.FORMAT)) + && body.optJSONObject(ApiDefinitionService.JSONSCHEMA) != null + && Body.JSON_SCHEMA.equals(body.optString(ApiDefinitionService.FORMAT))) { + body.put(ApiDefinitionService.RAW, JSONSchemaParser.preview(body.optString(ApiDefinitionService.JSONSCHEMA))); + jsonObject.put(ApiDefinitionService.BODY, body); } - ); + item.setRequest(jsonObject.toString()); + } + }); } // 排序 FixedOrderComparator fixedOrderComparator = new FixedOrderComparator(request.getIds());