From 6eb6eafd0add1c069ff56b859b3a6d91e63be9b8 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 14 Mar 2023 20:26:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=E8=A7=A3=E5=86=B3api=5Fmodule=E6=A0=91=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1024415 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001024415 --- .../ApiDefinitionImportUtilService.java | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java index df1d0a67d0..fd8c36105d 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java @@ -212,7 +212,8 @@ public class ApiDefinitionImportUtilService { Map> apiIdCaseMap = optionDataCases.stream().collect(Collectors.groupingBy(ApiTestCase::getApiDefinitionId)); if (MapUtils.isNotEmpty(moduleMap)) { - moduleMap.forEach((k, v) -> apiModuleMapper.insert(v)); + + delModule(apiModuleService, optionData, moduleMap, apiModuleMapper); } int num = 0; if (!CollectionUtils.isEmpty(optionData) && optionData.get(0) != null && optionData.get(0).getProjectId() != null) { @@ -279,6 +280,32 @@ public class ApiDefinitionImportUtilService { return apiImportSendNoticeDTOS; } + private static void delModule(ApiModuleService apiModuleService, List optionData, Map moduleMap, ApiModuleMapper apiModuleMapper) { + Map rootModuleMap = new HashMap<>(); + Map> moduleOptionData = optionData.stream().collect(Collectors.groupingBy(ApiDefinition::getModulePath)); + //过滤空的模块 + moduleMap.forEach((modulePath, v) -> { + if (moduleOptionData.get(modulePath) != null && moduleOptionData.get(modulePath).size() > 0) { + apiModuleMapper.insert(v); + } else { + rootModuleMap.put(modulePath, v); + } + }); + //防止空的模块是其余模块的父亲 + rootModuleMap.forEach((modulePath, v) -> { + for (String path : moduleOptionData.keySet()) { + String[] modulePathTree = apiModuleService.getPathTree(modulePath); + String[] pathTree = apiModuleService.getPathTree(path); + List modulePathList = Arrays.asList(modulePathTree); + List pathTreeList = Arrays.asList(pathTree); + if (new HashSet<>(pathTreeList).containsAll(modulePathList)) { + apiModuleMapper.insert(v); + break; + } + } + }); + } + /** * @param optionDatas 操作过可以导入的文件里的接口 * @param repeatApiMap 文件里与可操作的接口重复的接口 @@ -306,7 +333,7 @@ public class ApiDefinitionImportUtilService { List nameList = new ArrayList<>(); List importCaseList = importCaseMap.get(apiId); if (CollectionUtils.isNotEmpty(importCaseList)) { - nameList = importCaseList.stream().map(ApiTestCaseWithBLOBs::getName).toList(); + nameList = importCaseList.stream().map(ApiTestCaseWithBLOBs::getName).collect(Collectors.toList()); } for (int i = 0; i < apiTestCaseWithBLOBs.size(); i++) { ApiTestCaseWithBLOBs apiTestCaseWithBLOBs1 = apiTestCaseWithBLOBs.get(i); @@ -1063,7 +1090,6 @@ public class ApiDefinitionImportUtilService { List moduleData = moduleOptionData.get(modulePath); if (moduleData != null && moduleData.size() <= 1) { moduleMap.remove(modulePath); - removeModulePath(moduleMap, moduleOptionData, modulePath); moduleData.remove(apiDefinitionWithBLOBs); } //不覆盖同一接口不做更新 注意原接口的update时间不变 @@ -1075,21 +1101,6 @@ public class ApiDefinitionImportUtilService { }); } - private void removeModulePath(Map moduleMap, Map> moduleOptionData, String modulePath) { - if (StringUtils.isBlank(modulePath)) { - return; - } - ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); - String[] pathTree = apiModuleService.getPathTree(modulePath); - String lastPath = pathTree[pathTree.length - 1]; - String substring = modulePath.substring(0, modulePath.indexOf("/" + lastPath)); - if (moduleOptionData.get(substring) == null || moduleOptionData.get(substring).size() == 0) { - moduleMap.remove(substring); - removeModulePath(moduleMap, moduleOptionData, substring); - } - - } - private static void buildCases(List optionDataCases, Map> oldCaseMap, List caseIds) { if (MapUtils.isNotEmpty(oldCaseMap)) { List oldCaseList = new ArrayList<>();