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