diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 94f281b3b1..067000154f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -306,7 +306,7 @@ public class ApiAutomationService { .andIdNotEqualTo(request.getId()); List list = apiScenarioMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(list)) { - MSException.throwException("自定义ID "+ request.getCustomNum() +" 已存在!"); + MSException.throwException("自定义ID " + request.getCustomNum() + " 已存在!"); } } @@ -1755,12 +1755,64 @@ public class ApiAutomationService { return apiImport; } + private void setHashTree(JSONArray hashTree) { + // 将引用转成复制 + if (CollectionUtils.isNotEmpty(hashTree)) { + for (int i = 0; i < hashTree.size(); i++) { + JSONObject object = (JSONObject) hashTree.get(i); + String referenced = object.getString("referenced"); + if (StringUtils.isNotBlank(referenced) && StringUtils.equals(referenced, "REF")) { + // 检测引用对象是否存在,若果不存在则改成复制对象 + String refType = object.getString("refType"); + if (StringUtils.isNotEmpty(refType)) { + if (refType.equals("CASE")) { + ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(object.getString("id")); + if (bloBs != null) { + object = JSON.parseObject(bloBs.getRequest()); + object.put("id", bloBs.getId()); + object.put("name", bloBs.getName()); + hashTree.set(i, object); + } + } else { + ApiScenarioWithBLOBs bloBs = this.getDto(object.getString("id")); + if (bloBs != null) { + object = JSON.parseObject(bloBs.getScenarioDefinition()); + hashTree.set(i, object); + } + } + } else if ("scenario".equals(object.getString("type"))) { + ApiScenarioWithBLOBs bloBs = this.getDto(object.getString("id")); + if (bloBs != null) { + object = JSON.parseObject(bloBs.getScenarioDefinition()); + hashTree.set(i, object); + } + } + } + if (CollectionUtils.isNotEmpty(object.getJSONArray("hashTree"))) { + setHashTree(object.getJSONArray("hashTree")); + } + } + } + } + private List getExportResult(ApiScenarioBatchRequest request) { ServiceUtils.getSelectAllIds(request, request.getCondition(), (query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query)); ApiScenarioExample example = new ApiScenarioExample(); example.createCriteria().andIdIn(request.getIds()); List apiScenarioWithBLOBs = apiScenarioMapper.selectByExampleWithBLOBs(example); + // 处理引用数据 + if (CollectionUtils.isNotEmpty(apiScenarioWithBLOBs)) { + apiScenarioWithBLOBs.forEach(item -> { + if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { + JSONObject scenario = JSONObject.parseObject(item.getScenarioDefinition()); + JSONArray hashTree = scenario.getJSONArray("hashTree"); + setHashTree(hashTree); + scenario.put("hashTree", hashTree); + item.setScenarioDefinition(JSON.toJSONString(scenario)); + } + }); + } return apiScenarioWithBLOBs; } diff --git a/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue b/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue index cef2c048ea..06a32f1507 100644 --- a/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue +++ b/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue @@ -18,7 +18,7 @@ {{ $t('api_test.automation.reference_deleted') }} {{ $t('commons.copy') }} {{ $t('api_test.scenario.reference') }} - {{ getProjectName(request.projectId) }} + {{ getProjectName(request.projectId) }}