From b84c73212e0ad4c4b8e12dd8d6d69501d709deb9 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 5 Jul 2022 14:38:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E6=96=87=E4=BB=B6=E5=AF=BC=E5=85=A5=E5=8A=A0?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=88=A4=E6=96=AD=E4=BB=A5=E5=8F=8Aswagger?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=AF=BC=E5=85=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user=郭雨琦 --bug=1014627 【接口测试】接口定义导入,在ms格式下不选择覆盖模式点导入,然后切换har格式没提示格式错误 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014627 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014636 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014632 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014637 --- .../api/dto/definition/parse/HarParser.java | 2 +- .../definition/parse/MsDefinitionParser.java | 76 ++----------------- .../parse/PostmanDefinitionParser.java | 33 ++++---- .../dto/definition/parse/Swagger2Parser.java | 22 +++--- .../api/service/ApiDefinitionService.java | 26 +++++-- .../api/service/ApiModuleService.java | 15 ++-- .../api/service/ApiScenarioModuleService.java | 4 + 7 files changed, 62 insertions(+), 116 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarParser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarParser.java index 2068adb8c0..4c6c92608b 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarParser.java @@ -46,7 +46,7 @@ public class HarParser extends HarAbstractParser { MSException.throwException(e.getMessage()); LogUtil.error(e.getMessage(), e); } - if (ObjectUtils.isEmpty(har)) { + if (ObjectUtils.isEmpty(har) || har.log == null) { MSException.throwException("解析失败,请确认选择的是 Har 格式!"); } ApiDefinitionImport definitionImport = new ApiDefinitionImport(); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/MsDefinitionParser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/MsDefinitionParser.java index 7f41c514cd..63f333b511 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/MsDefinitionParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/MsDefinitionParser.java @@ -11,6 +11,7 @@ import io.metersphere.api.parse.MsAbstractParser; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.commons.constants.ApiImportPlatform; +import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; @@ -27,23 +28,14 @@ import java.util.stream.Collectors; public class MsDefinitionParser extends MsAbstractParser { - - /* private ApiModule selectModule; - - private String selectModulePath;*/ - @Override public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) { String testStr = getApiTestStr(source); JSONObject testObject = JSONObject.parseObject(testStr, Feature.DisableSpecialKeyDetect); this.projectId = request.getProjectId(); - /*if (StringUtils.isNotBlank(request.getModuleId())) { - this.selectModule = ApiDefinitionImportUtil.getSelectModule(request.getModuleId()); - if (this.selectModule != null) { - this.selectModulePath = ApiDefinitionImportUtil.getSelectModulePath(this.selectModule.getName(), this.selectModule.getParentId()); - } - }*/ - + if (this.projectId == null) { + MSException.throwException("wrong format"); + } if (testObject.get("projectName") != null || testObject.get("projectId") != null) {// metersphere 格式导入 return parseMsFormat(testStr, request); } else { // chrome 插件录制格式导入 @@ -58,16 +50,10 @@ public class MsDefinitionParser extends MsAbstractParser { protected List parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest, Boolean isCreateModule) { List results = new ArrayList<>(); testObject.keySet().forEach(tag -> { - /*String moduleId = null; - if (isCreateModule) { - moduleId = ApiDefinitionImportUtil.buildModule(this.selectModule, tag, this.projectId).getId(); - }*/ + List msHTTPSamplerProxies = parseMsHTTPSamplerProxy(testObject, tag, false); for (MsHTTPSamplerProxy msHTTPSamplerProxy : msHTTPSamplerProxies) { ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(), msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), importRequest); -/* - apiDefinition.setModuleId(moduleId); -*/ apiDefinition.setProjectId(this.projectId); apiDefinition.setRequest(JSONObject.toJSONString(msHTTPSamplerProxy)); apiDefinition.setName(apiDefinition.getPath() + " [" + apiDefinition.getMethod() + "]"); @@ -93,19 +79,7 @@ public class MsDefinitionParser extends MsAbstractParser { caseList.add(item); }); } -/* - Set moduleIdSet = apiDefinitionImport.getData().stream() - .map(ApiDefinitionWithBLOBs::getModuleId).collect(Collectors.toSet());*/ - /* Map nodeMap = null; - List nodeTree = apiDefinitionImport.getNodeTree(); - if (CollectionUtils.isNotEmpty(nodeTree)) { - cutDownTree(nodeTree, moduleIdSet); - ApiDefinitionImportUtil.createNodeTree(nodeTree, projectId, importRequest.getModuleId()); - nodeMap = getNodeMap(nodeTree); - }*/ - - /*Map finalNodeMap = nodeMap;*/ apiDefinitionImport.getData().forEach(apiDefinition -> { parseApiDefinition(apiDefinition, importRequest, caseMap); }); @@ -122,17 +96,6 @@ public class MsDefinitionParser extends MsAbstractParser { private void parseApiDefinition(ApiDefinitionWithBLOBs apiDefinition, ApiTestImportRequest importRequest, Map> caseMap) { String originId = apiDefinition.getId(); - /*if (nodeMap != null && nodeMap.get(apiDefinition.getModuleId()) != null) { - NodeTree nodeTree = nodeMap.get(apiDefinition.getModuleId()); - apiDefinition.setModuleId(nodeTree.getNewId()); - apiDefinition.setModulePath(nodeTree.getPath()); - } else { - if (StringUtils.isBlank(apiDefinition.getModulePath())) { - apiDefinition.setModuleId(null); - } - // 旧版本未导出模块 - parseModule(apiDefinition.getModulePath(), importRequest, apiDefinition); - }*/ apiDefinition.setProjectId(this.projectId); JSONObject requestObj = this.parseObject(apiDefinition.getRequest(), apiDefinition.getProjectId()); @@ -195,33 +158,4 @@ public class MsDefinitionParser extends MsAbstractParser { } } -/* - private void parseModule(String modulePath, ApiTestImportRequest importRequest, ApiDefinitionWithBLOBs apiDefinition) { - if (StringUtils.isEmpty(modulePath)) { - return; - } - if (modulePath.startsWith("/")) { - modulePath = modulePath.substring(1, modulePath.length()); - } - if (modulePath.endsWith("/")) { - modulePath = modulePath.substring(0, modulePath.length() - 1); - } - List modules = Arrays.asList(modulePath.split("/")); - ApiModule parent = this.selectModule; - Iterator iterator = modules.iterator(); - while (iterator.hasNext()) { - String item = iterator.next(); - parent = ApiDefinitionImportUtil.buildModule(parent, item, this.projectId, importRequest.getUserId()); - if (!iterator.hasNext()) { - apiDefinition.setModuleId(parent.getId()); - String path = apiDefinition.getModulePath() == null ? "" : apiDefinition.getModulePath(); - if (StringUtils.isNotBlank(this.selectModulePath)) { - apiDefinition.setModulePath(this.selectModulePath + path); - } else if (StringUtils.isBlank(importRequest.getModuleId())) { - apiDefinition.setModulePath("/未规划接口" + path); - } - } - } - } -*/ } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/PostmanDefinitionParser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/PostmanDefinitionParser.java index edbf78f3be..cb0f25bf25 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/PostmanDefinitionParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/PostmanDefinitionParser.java @@ -1,6 +1,7 @@ package io.metersphere.api.dto.definition.parse; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.Feature; import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; @@ -14,6 +15,7 @@ import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.base.domain.Project; import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.commons.constants.ProjectApplicationType; +import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.dto.ProjectConfig; @@ -25,24 +27,25 @@ import java.util.*; public class PostmanDefinitionParser extends PostmanAbstractParserParser { - /*private ApiModule selectModule; - - private String selectModulePath;*/ - @Override public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) { String testStr = getApiTestStr(source); this.projectId = request.getProjectId(); + JSONObject jsonObject = JSON.parseObject(testStr); + Object info = jsonObject.get("info"); + if (info == null) { + MSException.throwException("wrong format"); + } else { + JSONObject jsonObject1 = JSON.parseObject(info.toString()); + if (jsonObject1.get("_postman_id") == null) { + MSException.throwException("wrong format"); + } + } PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class, Feature.DisableSpecialKeyDetect); List variables = postmanCollection.getVariable(); ApiDefinitionImport apiImport = new ApiDefinitionImport(); List results = new ArrayList<>(); - /*this.selectModule = ApiDefinitionImportUtil.getSelectModule(request.getModuleId()); - if (this.selectModule != null) { - this.selectModulePath = ApiDefinitionImportUtil.getSelectModulePath(this.selectModule.getName(), this.selectModule.getParentId()); - } - ApiModule apiModule = ApiDefinitionImportUtil.buildModule(this.selectModule, postmanCollection.getInfo().getName(), this.projectId);*/ String modulePath = null; if (StringUtils.isNotBlank(postmanCollection.getInfo().getName())) { modulePath = "/" + postmanCollection.getInfo().getName(); @@ -68,8 +71,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser childItems = item.getItem(); if (childItems != null) { - /*ApiModule module = null; - module = ApiDefinitionImportUtil.buildModule(parentModule, item.getName(), this.projectId);*/ + if (StringUtils.isNotBlank(modulePath) && StringUtils.isNotBlank(item.getName())) { modulePath = modulePath + "/" + item.getName(); } @@ -82,14 +84,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser 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()); - } - }*/ - String basePath = swagger.getBasePath(); for (String pathName : pathNames) { Path path = paths.get(pathName); @@ -141,9 +138,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { } apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation, operation.getResponses()))); -/* - buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath); -*/ + buildModulePath(apiDefinition, operation.getTags()); if (operation.isDeprecated() != null && operation.isDeprecated()) { apiDefinition.setTags("[\"Deleted\"]"); @@ -163,6 +158,9 @@ public class Swagger2Parser extends SwaggerAbstractParser { } private String getModulePath(List tagTree, StringBuilder modulePath) { + if (tagTree == null) { + return ""; + } for (String s : tagTree) { if (s.contains("/")) { String[] split = s.split("/"); 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 03bc72c181..2f45405202 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -1328,9 +1328,24 @@ public class ApiDefinitionService { public ApiDefinitionImport apiTestImport(MultipartFile file, ApiTestImportRequest request) { //通过platform,获取对应的导入解析类型。 + String originalFilename = file.getOriginalFilename(); + String suffixName = originalFilename.substring(originalFilename.indexOf(".")); + if (suffixName.equalsIgnoreCase("jmx")) { + if (!request.getPlatform().equalsIgnoreCase("JMeter")) { + MSException.throwException("文件格式不符合要求"); + } + } + if (suffixName.equalsIgnoreCase("har")) { + if (!request.getPlatform().equalsIgnoreCase("Har")) { + MSException.throwException("文件格式不符合要求"); + } + } ApiImportParser runService = ApiDefinitionImportParserFactory.getApiImportParser(request.getPlatform()); ApiDefinitionImport apiImport = null; if (StringUtils.isNotBlank(request.getSwaggerUrl())) { + if (!request.getPlatform().equalsIgnoreCase("Swagger2")) { + MSException.throwException("文件格式不符合要求"); + } if (!UrlTestUtils.testUrlWithTimeOut(request.getSwaggerUrl(), 30000)) { MSException.throwException(Translator.get("connection_timeout")); } @@ -1350,7 +1365,11 @@ public class ApiDefinitionService { if (StringUtils.contains(returnThrowException, "模块树最大深度为")) { MSException.throwException(returnThrowException); } else { - MSException.throwException(Translator.get("parse_data_error")); + if (returnThrowException.equals("wrong format")) { + MSException.throwException("文件格式不符合要求"); + } else { + MSException.throwException(Translator.get("parse_data_error")); + } } // 发送通知 if (StringUtils.equals(request.getType(), "schedule")) { @@ -1708,11 +1727,6 @@ public class ApiDefinitionService { (query) -> extApiDefinitionMapper.selectIds(query)); this.removeToGc(request.getIds()); -// ApiDefinitionExampleWithOperation example = new ApiDefinitionExampleWithOperation(); -// example.createCriteria().andIdIn(request.getIds()); -// example.setOperator(SessionUtils.getUserId()); -// example.setOperationTime(System.currentTimeMillis()); -// extApiDefinitionMapper.removeToGcByExample(example); } public Pager> listRelevance(ApiDefinitionRequest request, int goPage, int pageSize) { 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 b27df48e1f..a433c5f058 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -89,13 +89,6 @@ public class ApiModuleService extends NodeTreeService { Map> filters = new LinkedHashMap<>(); filters.put("status", list); request.setFilters(filters); -// apiModules.forEach(node -> { -// List moduleIds = new ArrayList<>(); -// moduleIds = this.nodeList(apiModules, node.getId(), moduleIds); -// moduleIds.add(node.getId()); -// request.setModuleIds(moduleIds); -// node.setCaseNum(extApiDefinitionMapper.moduleCount(request)); -// }); //优化: 所有统计SQL一次查询出来 List allModuleIdList = new ArrayList<>(); @@ -786,6 +779,8 @@ public class ApiModuleService extends NodeTreeService { apiDefinitionWithBLOBs.setVersionId(v.getVersionId()); apiDefinitionWithBLOBs.setModuleId(v.getModuleId()); apiDefinitionWithBLOBs.setModulePath(v.getModulePath()); + apiDefinitionWithBLOBs.setNum(v.getNum()); + apiDefinitionWithBLOBs.setStatus(v.getStatus()); toUpdateList.add(apiDefinitionWithBLOBs); } }); @@ -801,6 +796,8 @@ public class ApiModuleService extends NodeTreeService { if (apiDefinitionWithBLOBs != null) { apiDefinitionWithBLOBs.setId(v.getId()); apiDefinitionWithBLOBs.setVersionId(v.getVersionId()); + apiDefinitionWithBLOBs.setNum(v.getNum()); + apiDefinitionWithBLOBs.setStatus(v.getStatus()); toUpdateList.add(apiDefinitionWithBLOBs); } }); @@ -964,6 +961,8 @@ public class ApiModuleService extends NodeTreeService { api.setOrder(definitionWithBLOBs.getOrder()); api.setRefId(apiDefinitionWithBLOBs.getRefId()); api.setLatest(apiDefinitionWithBLOBs.getLatest()); + api.setNum(definitionWithBLOBs.getNum()); + api.setStatus(definitionWithBLOBs.getStatus()); coverApiList.add(api); } optionData.remove(apiDefinitionWithBLOBs); @@ -981,6 +980,8 @@ public class ApiModuleService extends NodeTreeService { ApiDefinitionWithBLOBs api = new ApiDefinitionWithBLOBs(); BeanUtils.copyBean(api, apiDefinitionWithBLOBs); api.setId(definitionWithBLOBs.getId()); + api.setNum(definitionWithBLOBs.getNum()); + api.setStatus(definitionWithBLOBs.getStatus()); api.setVersionId(definitionWithBLOBs.getVersionId()); api.setModuleId(definitionWithBLOBs.getModuleId()); api.setModulePath(definitionWithBLOBs.getModulePath()); 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 a505473f68..e264411616 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java @@ -621,6 +621,8 @@ public class ApiScenarioModuleService extends NodeTreeService