From c970a77d05a7f8bc01e14456d15142bfaef470aa Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Thu, 30 Jun 2022 17:18:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E6=8E=A5=E5=8F=A3=E5=AF=BC=E5=85=A5=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user=郭雨琦 --- .../automation/parse/HarScenarioParser.java | 1 + .../dto/automation/parse/MsJmeterParser.java | 1 + .../parse/PostmanScenarioParser.java | 1 + .../parse/JmeterDefinitionParser.java | 1 + .../parse/PostmanDefinitionParser.java | 17 +++++- .../dto/definition/parse/Swagger2Parser.java | 23 ++++++++ .../dto/definition/parse/Swagger3Parser.java | 57 +++++++++++++------ .../api/service/ApiDefinitionService.java | 11 ++-- .../api/service/ApiModuleService.java | 18 +++++- .../api/service/ApiScenarioModuleService.java | 1 + 10 files changed, 104 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/HarScenarioParser.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/HarScenarioParser.java index dcebcebf4c..0697f81aa9 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/HarScenarioParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/HarScenarioParser.java @@ -76,6 +76,7 @@ public class HarScenarioParser extends HarScenarioAbstractParser scenarioWithBLOBs.setModulePath("/" + module.getName()); } }*/ + scenarioWithBLOBs.setModulePath("/" + msScenario.getName()); scenarioWithBLOBsList.add(scenarioWithBLOBs); } diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java index a0a8f6f1e5..41e1ec5f5c 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java @@ -127,6 +127,7 @@ public class MsJmeterParser extends ApiImportAbstractParser { scenarioWithBLOBs.setProjectId(request.getProjectId()); if (msScenario != null && CollectionUtils.isNotEmpty(msScenario.getHashTree())) { scenarioWithBLOBs.setStepTotal(msScenario.getHashTree().size()); + scenarioWithBLOBs.setModulePath("/" + msScenario.getName()); } /*if (module != null) { scenarioWithBLOBs.setApiScenarioModuleId(module.getId()); diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/PostmanScenarioParser.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/PostmanScenarioParser.java index 79f2bc2f6f..d95ed5aa4e 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/PostmanScenarioParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/PostmanScenarioParser.java @@ -58,6 +58,7 @@ public class PostmanScenarioParser extends PostmanAbstractParserParser cases = new ArrayList<>(); Map repeatMap = new HashMap(); ProjectMapper projectMapper = CommonBeanFactory.getBean(ProjectMapper.class); @@ -49,7 +54,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser items, List variables, List results, + protected void parseItem(List items, String modulePath, List variables, List results, List cases, Map repeatMap, Boolean repeatable) { for (PostmanItem item : items) { List childItems = item.getItem(); if (childItems != null) { /*ApiModule module = null; module = ApiDefinitionImportUtil.buildModule(parentModule, item.getName(), this.projectId);*/ - parseItem(childItems, variables, results, cases, repeatMap, repeatable); + if (StringUtils.isNotBlank(modulePath) && StringUtils.isNotBlank(item.getName())) { + modulePath = modulePath + "/" + item.getName(); + } + parseItem(childItems, modulePath, variables, results, cases, repeatMap, repeatable); } else { MsHTTPSamplerProxy msHTTPSamplerProxy = parsePostman(item); HttpResponse response = parsePostmanResponse(item); @@ -82,6 +90,9 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser tags) { + StringBuilder modulePathBuilder = new StringBuilder(); + String modulePath = getModulePath(tags, modulePathBuilder); + apiDefinition.setModulePath(modulePath); + } + + private String getModulePath(List tagTree, StringBuilder modulePath) { + for (String s : tagTree) { + if (s.contains("/")) { + String[] split = s.split("/"); + if (split.length > 0) { + getModulePath(List.of(split), modulePath); + } + } else { + if (StringUtils.isNotBlank(s)) { + modulePath.append("/").append(s); + } + } + } + return modulePath.toString(); + } + private ApiDefinitionWithBLOBs buildApiDefinition(String id, Operation operation, String path, String method, ApiTestImportRequest importRequest) { String name = ""; if (StringUtils.isNotBlank(operation.getSummary())) { diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java index 3bb0280d00..7f7b8cc414 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java @@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.Feature; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiModuleDTO; import io.metersphere.api.dto.definition.SwaggerApiExportResult; import io.metersphere.api.dto.definition.parse.swagger.SwaggerApiInfo; import io.metersphere.api.dto.definition.parse.swagger.SwaggerInfo; @@ -16,10 +15,8 @@ import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.request.RequestType; -import io.metersphere.api.service.ApiModuleService; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.XMLUtils; import io.swagger.parser.OpenAPIParser; @@ -130,15 +127,6 @@ public class Swagger3Parser extends SwaggerAbstractParser { List results = new ArrayList<>(); - /*ApiModule selectModule = null; - String selectModulePath = null; - if (StringUtils.isNotBlank(importRequest.getModuleId())) { - selectModule = ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId()); - if (selectModule != null) { - selectModulePath = ApiDefinitionImportUtil.getSelectModulePath(selectModule.getName(), selectModule.getParentId()); - } - }*/ - for (String pathName : pathNames) { PathItem pathItem = paths.get(pathName); @@ -166,9 +154,8 @@ public class Swagger3Parser extends SwaggerAbstractParser { } // 有数据的话,去掉 Kvs 里初始化的第一个全 null 的数据,否则有空行 apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses()))); -/* - buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath); -*/ + //buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath); + buildModulePath(apiDefinition, operation.getTags()); if (operation.getDeprecated() != null && operation.getDeprecated()) { apiDefinition.setTags("[\"Deleted\"]"); } @@ -180,6 +167,12 @@ public class Swagger3Parser extends SwaggerAbstractParser { return results; } + private void buildModulePath(ApiDefinitionWithBLOBs apiDefinition, List tags) { + StringBuilder modulePathBuilder = new StringBuilder(); + String modulePath = getModulePath(tags, modulePathBuilder); + apiDefinition.setModulePath(modulePath); + } + private ApiDefinitionWithBLOBs buildApiDefinition(String id, Operation operation, String path, String method, ApiTestImportRequest importRequest) { String name; if (StringUtils.isNotBlank(operation.getSummary())) { @@ -589,15 +582,28 @@ public class Swagger3Parser extends SwaggerAbstractParser { SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:} swaggerApiInfo.setSummary(apiDefinition.getName()); // 设置导入后的模块名 (根据 api 的 moduleID 查库获得所属模块,作为导出的模块名) - ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); + /*ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); String moduleName = ""; if (apiDefinition.getModuleId() != null) { // module_id 可能为空 ApiModuleDTO node = apiModuleService.getNode(apiDefinition.getModuleId()); if (node != null) { moduleName = node.getName(); } + }*/ + //直接导出完整路径 + if (apiDefinition.getModulePath() != null) { + String[] split = new String[0]; + String modulePath = apiDefinition.getModulePath(); + String substring = modulePath.substring(0, 1); + if (substring.equals("/")) { + modulePath = modulePath.substring(1); + } + if (modulePath.contains("/")) { + split = modulePath.split("/"); + } + swaggerApiInfo.setTags(Arrays.asList(split)); } - swaggerApiInfo.setTags(Arrays.asList(moduleName)); + // 设置请求体 JSONObject requestObject = JSON.parseObject(apiDefinition.getRequest(), Feature.DisableSpecialKeyDetect); // 将api的request属性转换成JSON对象以便获得参数 JSONObject requestBody = buildRequestBody(requestObject); @@ -991,4 +997,21 @@ public class Swagger3Parser extends SwaggerAbstractParser { } return content; } + + private String getModulePath(List tagTree, StringBuilder modulePath) { + for (String s : tagTree) { + if (s.contains("/")) { + String[] split = s.split("/"); + if (split.length > 0) { + getModulePath(List.of(split), modulePath); + } + } else { + if (StringUtils.isNotBlank(s)) { + modulePath.append("/").append(s); + } + } + } + return modulePath.toString(); + } + } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index a76e380703..cff5e54710 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -1399,6 +1399,12 @@ public class ApiDefinitionService { num = getNextNum(data.get(0).getProjectId()); } + if (moduleList != null) { + for (ApiModule apiModule : moduleList) { + apiModuleMapper.insert(apiModule); + } + } + for (int i = 0; i < data.size(); i++) { ApiDefinitionWithBLOBs item = data.get(i); this.setModule(item); @@ -1420,11 +1426,6 @@ public class ApiDefinitionService { } else { importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), updateList); } - if (moduleList != null) { - for (ApiModule apiModule : moduleList) { - apiModuleMapper.insert(apiModule); - } - } if (i % 300 == 0) { sqlSession.flushStatements(); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index 4b300325f0..fb06723d8b 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -618,6 +618,10 @@ public class ApiModuleService extends NodeTreeService { fullCoverage = false; } + if (fullCoverageApi == null) { + fullCoverageApi = false; + } + //标准版ESB数据导入不区分是否覆盖,默认都为覆盖 if (apiImport.getEsbApiParamsMap() != null) { fullCoverage = true; @@ -810,6 +814,9 @@ public class ApiModuleService extends NodeTreeService { BeanUtils.copyBean(api, apiDefinitionWithBLOBs); api.setId(definitionWithBLOBs.getId()); api.setVersionId(definitionWithBLOBs.getVersionId()); + api.setOrder(definitionWithBLOBs.getOrder()); + api.setRefId(apiDefinitionWithBLOBs.getRefId()); + api.setLatest(apiDefinitionWithBLOBs.getLatest()); coverApiList.add(api); } optionData.remove(apiDefinitionWithBLOBs); @@ -830,6 +837,9 @@ public class ApiModuleService extends NodeTreeService { api.setVersionId(definitionWithBLOBs.getVersionId()); api.setModuleId(definitionWithBLOBs.getModuleId()); api.setModulePath(definitionWithBLOBs.getModulePath()); + api.setOrder(definitionWithBLOBs.getOrder()); + api.setRefId(apiDefinitionWithBLOBs.getRefId()); + api.setLatest(apiDefinitionWithBLOBs.getLatest()); coverApiList.add(api); } optionData.remove(apiDefinitionWithBLOBs); @@ -963,6 +973,7 @@ public class ApiModuleService extends NodeTreeService { parentModule.setProjectId(pidChildrenMap.get("root").get(0).getProjectId()); parentModule.setId("root"); parentModule.setLevel(0); + parentModule.setProtocol(pidChildrenMap.get("root").get(0).getProtocol()); } else { if (!parentModuleList.isEmpty() && parentModule == null) { String parentId = parentModuleList.get(0).getParentId(); @@ -1024,7 +1035,6 @@ public class ApiModuleService extends NodeTreeService { Map> idChildrenMap = new HashMap<>(); int i = 0; Map> idModuleMap = new HashMap<>(); - List moduleList = new ArrayList<>(); for (ApiModuleDTO apiModuleDTO : nodeTreeByProjectId) { if (StringUtils.isBlank(apiModuleDTO.getParentId())) { apiModuleDTO.setParentId("root"); @@ -1050,7 +1060,11 @@ public class ApiModuleService extends NodeTreeService { i = i + 1; List childrenList = idChildrenMap.get(apiModuleDTO.getId()); if (apiModuleDTO.getChildren() != null) { - childrenList.addAll(apiModuleDTO.getChildren()); + if (childrenList != null) { + childrenList.addAll(apiModuleDTO.getChildren()); + } else { + idChildrenMap.put(apiModuleDTO.getId(), apiModuleDTO.getChildren()); + } } else { if (childrenList == null) { pidChildrenMap.put(apiModuleDTO.getId(), new ArrayList<>()); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java index 16f298ec54..a534b0e3eb 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java @@ -491,6 +491,7 @@ public class ApiScenarioModuleService extends NodeTreeService scenarioModules = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId); List nodeTreeByProjectId = this.getNodeTrees(scenarioModules);