diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index 7c9e945c2c..dbda7d0a1a 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -175,7 +175,7 @@ public class ApiAutomationController { @GetMapping("/getApiScenario/{id}") public ApiScenarioDTO getScenarioDefinition(@PathVariable String id) { - return apiAutomationService.getApiScenario(id); + return apiAutomationService.getNewApiScenario(id); } @PostMapping("/getApiScenarioEnv") 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 970073dd6f..1de0328ba0 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -665,6 +665,58 @@ public class ApiAutomationService { return extApiScenarioMapper.selectById(id); } + public ApiScenarioDTO getNewApiScenario(String id) { + ApiScenarioDTO scenarioWithBLOBs = extApiScenarioMapper.selectById(id); + if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { + JSONObject element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + this.dataFormatting(element); + scenarioWithBLOBs.setScenarioDefinition(JSON.toJSONString(element)); + } + return scenarioWithBLOBs; + } + + public void dataFormatting(JSONArray hashTree) { + for (int i = 0; i < hashTree.size(); i++) { + JSONObject element = hashTree.getJSONObject(i); + if (element != null && StringUtils.equalsIgnoreCase(element.getString("type"), "scenario")) { + ApiScenarioDTO scenarioWithBLOBs = extApiScenarioMapper.selectById(element.getString("id")); + if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { + if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + element.put("referenced", "REF"); + } + element.put("num", scenarioWithBLOBs.getNum()); + element.put("versionName", scenarioWithBLOBs.getVersionName()); + element.put("versionEnable", scenarioWithBLOBs.getVersionEnable()); + hashTree.set(i, element); + } + } + if (element.containsKey("hashTree")) { + JSONArray elementJSONArray = element.getJSONArray("hashTree"); + dataFormatting(elementJSONArray); + } + } + } + + public void dataFormatting(JSONObject element) { + if (element != null && StringUtils.equalsIgnoreCase(element.getString("type"), "scenario")) { + ApiScenarioDTO scenarioWithBLOBs = extApiScenarioMapper.selectById(element.getString("id")); + if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { + if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + element.put("referenced", "REF"); + } + element.put("num", scenarioWithBLOBs.getNum()); + element.put("versionName", scenarioWithBLOBs.getVersionName()); + element.put("versionEnable", scenarioWithBLOBs.getVersionEnable()); + } + } + if (element != null && element.containsKey("hashTree")) { + JSONArray elementJSONArray = element.getJSONArray("hashTree"); + dataFormatting(elementJSONArray); + } + } + public String setDomain(ApiScenarioEnvRequest request) { Boolean enable = request.getEnvironmentEnable(); String scenarioDefinition = request.getDefinition(); diff --git a/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue b/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue index fb34c84411..983abd6ee2 100644 --- a/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue +++ b/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue @@ -119,60 +119,13 @@ export default { created() { if (this.scenario.num) { this.isShowNum = true; + this.getWorkspaceId(this.scenario.projectId); + } else { + this.isSameSpace = false; } if (!this.scenario.projectId) { this.scenario.projectId = getCurrentProjectID(); } - if (this.scenario.id && this.scenario.referenced === 'REF' && !this.scenario.loaded) { - let scenarios = JSON.parse(JSON.stringify(this.scenario.hashTree)); - let map = new Map(); - this.formatResult(map, scenarios); - this.result = this.$get("/api/automation/getApiScenario/" + this.scenario.id, response => { - if (response.data) { - this.scenario.loaded = true; - let obj = {}; - if (response.data.scenarioDefinition) { - obj = JSON.parse(response.data.scenarioDefinition); - this.scenario.hashTree = obj.hashTree; - } - this.scenario.projectId = response.data.projectId; - const pro = this.projectList.find(p => p.id === response.data.projectId); - if (!pro) { - this.scenario.projectId = getCurrentProjectID(); - } - if (this.scenario.hashTree) { - this.setDisabled(this.scenario.hashTree, this.scenario.projectId); - } - if (response.data.num) { - this.scenario.num = response.data.num; - this.getWorkspaceId(response.data.projectId); - } - this.scenario.versionName = response.data.versionName; - this.scenario.versionEnable = response.data.versionEnable; - this.scenario.name = response.data.name; - this.scenario.headers = obj.headers; - this.scenario.variables = obj.variables; - this.scenario.environmentMap = obj.environmentMap; - this.setResult(this.scenario.hashTree, map); - this.$emit('refReload'); - } - }) - } else if (this.scenario.id && (this.scenario.referenced === 'Copy' || this.scenario.referenced === 'Created') && !this.scenario.loaded) { - this.result = this.$get("/api/automation/getApiScenario/" + this.scenario.id, response => { - if (response.data) { - if (response.data.num) { - this.scenario.num = response.data.num; - this.getWorkspaceId(response.data.projectId); - } else { - this.isSameSpace = false - } - this.scenario.versionName = response.data.versionName; - this.scenario.versionEnable = response.data.versionEnable; - } else { - this.isSameSpace = false - } - }) - } }, components: {ApiBaseComponent, MsSqlBasisParameters, MsTcpBasisParameters, MsDubboBasisParameters, MsApiRequestForm}, data() { @@ -196,34 +149,6 @@ export default { }, }, methods: { - formatResult(map, scenarios) { - scenarios.forEach(item => { - if (this.stepFilter.get("AllSamplerProxy").indexOf(item.type) !== -1 && item.requestResult) { - let key = (item.id ? item.id : item.resourceId) + "_" + item.index; - if (map.has(key)) { - map.get(key).push(...item.requestResult); - } else { - map.set(key, item.requestResult); - } - } - if (item.hashTree && item.hashTree.length > 0) { - this.formatResult(map, item.hashTree); - } - }) - }, - setResult(array, scenarios) { - if (array && scenarios) { - array.forEach(item => { - let key = (item.id ? item.id : item.resourceId) + "_" + item.index; - if (scenarios.has(key)) { - item.requestResult = scenarios.get(key); - } - if (item.hashTree && item.hashTree.length > 0) { - this.setResult(item.hashTree, scenarios); - } - }) - } - }, run() { this.scenario.run = true; let runScenario = JSON.parse(JSON.stringify(this.scenario));