From 722b6d123ef178201b3800db8a2fbdafbf47809e Mon Sep 17 00:00:00 2001 From: metersphere-bot <78466014+metersphere-bot@users.noreply.github.com> Date: Thu, 8 Apr 2021 14:44:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=A5=E5=8F=A3=E5=AF=BC=E5=85=A5modu?= =?UTF-8?q?lePath=20=E4=B8=8D=E6=AD=A3=E7=A1=AE=20(#1881)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: chenjianxing Co-authored-by: jianxing <41557596+AgAngle@users.noreply.github.com> --- .../parse/ApiScenarioImportUtil.java | 15 ++++++++++-- .../automation/parse/HarScenarioParser.java | 17 +++++++++----- .../dto/automation/parse/MsJmeterParser.java | 16 +++++++++++-- .../automation/parse/MsScenarioParser.java | 20 +++++++++++++++- .../parse/PostmanScenarioParser.java | 11 +++++++-- .../parse/ApiDefinitionImportUtil.java | 18 +++++++++++++++ .../definition/parse/HarAbstractParser.java | 17 -------------- .../api/dto/definition/parse/HarParser.java | 22 ++++++++++++++++-- .../definition/parse/MsDefinitionParser.java | 21 +++++++++++++++-- .../parse/PostmanDefinitionParser.java | 23 +++++++++++++++---- .../dto/definition/parse/Swagger2Parser.java | 11 +++++++-- .../dto/definition/parse/Swagger3Parser.java | 14 +++++++---- .../parse/SwaggerAbstractParser.java | 9 +++++++- .../api/parse/ApiImportAbstractParser.java | 6 ----- .../api/service/ApiModuleService.java | 3 +++ 15 files changed, 171 insertions(+), 52 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java index 39e4d8fee8..e3c5226f7e 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java @@ -13,10 +13,10 @@ import java.util.List; public class ApiScenarioImportUtil { public static ApiScenarioModule getSelectModule(String moduleId) { - ApiScenarioModuleService apiModuleService = CommonBeanFactory.getBean(ApiScenarioModuleService.class); + ApiScenarioModuleService apiScenarioModuleService = CommonBeanFactory.getBean(ApiScenarioModuleService.class); if (StringUtils.isNotBlank(moduleId) && !StringUtils.equals("root", moduleId)) { ApiScenarioModule module = new ApiScenarioModule(); - ApiScenarioModuleDTO moduleDTO = apiModuleService.getNode(moduleId); + ApiScenarioModuleDTO moduleDTO = apiScenarioModuleService.getNode(moduleId); if (moduleDTO != null) { BeanUtils.copyBean(module, moduleDTO); } @@ -25,6 +25,17 @@ public class ApiScenarioImportUtil { return null; } + public static String getSelectModulePath(String path, String pid) { + ApiScenarioModuleService apiScenarioModuleService = CommonBeanFactory.getBean(ApiScenarioModuleService.class); + if (StringUtils.isNotBlank(pid)) { + ApiScenarioModuleDTO moduleDTO = apiScenarioModuleService.getNode(pid); + if (moduleDTO != null) { + return getSelectModulePath(moduleDTO.getName() + "/" + path, moduleDTO.getParentId()); + } + } + return "/" + path; + } + public static ApiScenarioModule buildModule(ApiScenarioModule parentModule, String name, String projectId) { ApiScenarioModuleService apiModuleService = CommonBeanFactory.getBean(ApiScenarioModuleService.class); ApiScenarioModule module; 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 d0ed200928..f65dbce19c 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 @@ -18,6 +18,7 @@ import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import java.io.InputStream; import java.util.ArrayList; @@ -62,16 +63,20 @@ public class HarScenarioParser extends HarScenarioAbstractParser } private void parseScenarioWithBLOBs(List scenarioWithBLOBsList, MsScenario msScenario, ApiTestImportRequest request) { - ApiScenarioModule module = ApiScenarioImportUtil.getSelectModule(request.getModuleId()); - if (module == null) { - ApiScenarioModuleService apiModuleService = CommonBeanFactory.getBean(ApiScenarioModuleService.class); - module = apiModuleService.getNewModule(msScenario.getName(), projectId, 1); + ApiScenarioModule selectModule = null; + if (StringUtils.isNotBlank(request.getModuleId())) { + selectModule = ApiScenarioImportUtil.getSelectModule(request.getModuleId()); } - + ApiScenarioModule module = ApiScenarioImportUtil.buildModule(selectModule, msScenario.getName(), this.projectId); ApiScenarioWithBLOBs scenarioWithBLOBs = parseScenario(msScenario); if (module != null) { scenarioWithBLOBs.setApiScenarioModuleId(module.getId()); - scenarioWithBLOBs.setModulePath("/" + module.getName()); + if (selectModule != null) { + String selectModulePath = ApiScenarioImportUtil.getSelectModulePath(selectModule.getName(), selectModule.getParentId()); + scenarioWithBLOBs.setModulePath(selectModulePath + "/" + module.getName()); + } else { + scenarioWithBLOBs.setModulePath("/" + module.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 0320e452b0..e891088121 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 @@ -112,7 +112,15 @@ public class MsJmeterParser extends ApiImportAbstractParser { private List paseObj(MsScenario msScenario, ApiTestImportRequest request) { List scenarioWithBLOBsList = new ArrayList<>(); ApiScenarioWithBLOBs scenarioWithBLOBs = new ApiScenarioWithBLOBs(); - ApiScenarioModule module = ApiScenarioImportUtil.buildModule(ApiScenarioImportUtil.getSelectModule(request.getModuleId()), msScenario.getName(), this.projectId); + ApiScenarioModule selectModule = null; + String selectModulePath = null; + if (StringUtils.isNotBlank(request.getModuleId())) { + selectModule = ApiScenarioImportUtil.getSelectModule(request.getModuleId()); + if (selectModule != null) { + selectModulePath = ApiScenarioImportUtil.getSelectModulePath(selectModule.getName(), selectModule.getParentId()); + } + } + ApiScenarioModule module = ApiScenarioImportUtil.buildModule(selectModule, msScenario.getName(), this.projectId); scenarioWithBLOBs.setName(msScenario.getName()); scenarioWithBLOBs.setProjectId(request.getProjectId()); if (msScenario != null && CollectionUtils.isNotEmpty(msScenario.getHashTree())) { @@ -120,7 +128,11 @@ public class MsJmeterParser extends ApiImportAbstractParser { } if (module != null) { scenarioWithBLOBs.setApiScenarioModuleId(module.getId()); - scenarioWithBLOBs.setModulePath("/" + module.getName()); + if (StringUtils.isNotBlank(selectModulePath)) { + scenarioWithBLOBs.setModulePath(selectModulePath + "/" + module.getName()); + } else { + scenarioWithBLOBs.setModulePath("/" + module.getName()); + } } scenarioWithBLOBs.setId(UUID.randomUUID().toString()); scenarioWithBLOBs.setScenarioDefinition(JSON.toJSONString(msScenario)); diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsScenarioParser.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsScenarioParser.java index 2bfbc8e7e4..28b95d8e9b 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsScenarioParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsScenarioParser.java @@ -17,11 +17,23 @@ import java.util.*; public class MsScenarioParser extends MsAbstractParser { + private ApiScenarioModule selectModule; + + private String selectModulePath; + @Override public ScenarioImport parse(InputStream source, ApiTestImportRequest request) { String testStr = getApiTestStr(source); this.projectId = request.getProjectId(); JSONObject testObject = JSONObject.parseObject(testStr, Feature.OrderedField); + + if (StringUtils.isNotBlank(request.getModuleId())) { + this.selectModule = ApiScenarioImportUtil.getSelectModule(request.getModuleId()); + if (this.selectModule != null) { + this.selectModulePath = ApiScenarioImportUtil.getSelectModulePath(this.selectModule.getName(), this.selectModule.getParentId()); + } + } + if (testObject.get("projectName") != null || testObject.get("projectId") != null ) { return parseMsFormat(testStr, request); } else { @@ -73,13 +85,19 @@ public class MsScenarioParser extends MsAbstractParser { modulePath = modulePath.substring(0, modulePath.length() - 1); } List modules = Arrays.asList(modulePath.split("/")); - ApiScenarioModule parent = ApiScenarioImportUtil.getSelectModule(importRequest.getModuleId()); + ApiScenarioModule parent = this.selectModule; Iterator iterator = modules.iterator(); while (iterator.hasNext()) { String item = iterator.next(); parent = ApiScenarioImportUtil.buildModule(parent, item, this.projectId); if (!iterator.hasNext()) { apiScenarioWithBLOBs.setApiScenarioModuleId(parent.getId()); + String path = apiScenarioWithBLOBs.getModulePath() == null ? "" : apiScenarioWithBLOBs.getModulePath(); + if (StringUtils.isNotBlank(this.selectModulePath)) { + apiScenarioWithBLOBs.setModulePath(this.selectModulePath + path); + } else if (StringUtils.isBlank(importRequest.getModuleId())) { + apiScenarioWithBLOBs.setModulePath("/默认模块" + path); + } } } } 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 201feeed86..da669fc41b 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 @@ -46,11 +46,18 @@ public class PostmanScenarioParser extends PostmanAbstractParserParser scenarioWithBLOBsList, MsScenario msScenario, ApiTestImportRequest request) { - ApiScenarioModule module = ApiScenarioImportUtil.buildModule(ApiScenarioImportUtil.getSelectModule(request.getModuleId()), msScenario.getName(), this.projectId); + ApiScenarioModule selectModule = ApiScenarioImportUtil.getSelectModule(request.getModuleId()); + + ApiScenarioModule module = ApiScenarioImportUtil.buildModule(selectModule, msScenario.getName(), this.projectId); ApiScenarioWithBLOBs scenarioWithBLOBs = parseScenario(msScenario); if (module != null) { scenarioWithBLOBs.setApiScenarioModuleId(module.getId()); - scenarioWithBLOBs.setModulePath("/" + module.getName()); + if (selectModule != null) { + String selectModulePath = ApiScenarioImportUtil.getSelectModulePath(selectModule.getName(), selectModule.getParentId()); + scenarioWithBLOBs.setModulePath(selectModulePath + "/" + module.getName()); + } else { + scenarioWithBLOBs.setModulePath("/" + module.getName()); + } } scenarioWithBLOBsList.add(scenarioWithBLOBs); } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImportUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImportUtil.java index 78df96ac1e..d78585271d 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImportUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImportUtil.java @@ -26,6 +26,24 @@ public class ApiDefinitionImportUtil { return null; } + public static String getSelectModulePath(String path, String pid) { + ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); + if (StringUtils.isNotBlank(pid)) { + ApiModuleDTO moduleDTO = apiModuleService.getNode(pid); + if (moduleDTO != null) { + return getSelectModulePath(moduleDTO.getName() + "/" + path, moduleDTO.getParentId()); + } + } + return "/" + path; + } + + public static ApiModule getNodeTree(String projectId) { + ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); + List nodeTrees = apiModuleService.getNodeTreeByProjectId(projectId, RequestType.HTTP); + + return null; + } + public static ApiModule buildModule(ApiModule parentModule, String name, String projectId) { ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); ApiModule module; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarAbstractParser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarAbstractParser.java index b437567bb0..62c4af2eb9 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/HarAbstractParser.java @@ -1,27 +1,10 @@ package io.metersphere.api.dto.definition.parse; - import io.metersphere.api.parse.ApiImportAbstractParser; -import io.metersphere.base.domain.ApiDefinitionWithBLOBs; -import io.metersphere.base.domain.ApiModule; - -import java.util.List; - /** * @author song.tianyang * @Date 2021/3/10 11:15 上午 * @Description */ public abstract class HarAbstractParser extends ApiImportAbstractParser { - - protected void buildModule(ApiModule parentModule, ApiDefinitionWithBLOBs apiDefinition, List tags) { - if (tags != null) { - tags.forEach(tag -> { - ApiModule module = ApiDefinitionImportUtil.buildModule(parentModule, tag, this.projectId); - apiDefinition.setModuleId(module.getId()); - }); - }else { - apiDefinition.setModuleId(parentModule.getId()); - } - } } 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 2d017c14bf..073277be33 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 @@ -61,7 +61,15 @@ public class HarParser extends HarAbstractParser { private List parseRequests(Har har, ApiTestImportRequest importRequest) { List results = new ArrayList<>(); - ApiModule parentNode = ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId()); + 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()); + } + } + List harEntryList = new ArrayList<>(); if (har.log != null && har.log.entries != null) { @@ -103,7 +111,17 @@ public class HarParser extends HarAbstractParser { addBodyHeader(request); apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setResponse(JSON.toJSONString(parseResponse(entry.response))); - buildModule(parentNode, apiDefinition, null); + if (selectModule == null) { + apiDefinition.setModuleId("default-module"); + + } else { + apiDefinition.setModuleId(selectModule.getId()); + } + if (StringUtils.isNotBlank(selectModulePath)) { + apiDefinition.setModulePath(selectModulePath); + } else { + apiDefinition.setModulePath("/默认模块"); + } results.add(apiDefinition); } } 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 ce76eddb03..43299a6220 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 @@ -19,11 +19,22 @@ import java.util.*; 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.OrderedField); 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 (testObject.get("projectName") != null || testObject.get("projectId") != null ) {// metersphere 格式导入 return parseMsFormat(testStr, request); } else { // chrome 插件录制格式导入 @@ -40,7 +51,7 @@ public class MsDefinitionParser extends MsAbstractParser { testObject.keySet().forEach(tag -> { String moduleId = null; if (isCreateModule) { - moduleId = ApiDefinitionImportUtil.buildModule(ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId()), tag, this.projectId).getId(); + moduleId = ApiDefinitionImportUtil.buildModule(this.selectModule, tag, this.projectId).getId(); } List msHTTPSamplerProxies = parseMsHTTPSamplerProxy(testObject, tag); for (MsHTTPSamplerProxy msHTTPSamplerProxy : msHTTPSamplerProxies) { @@ -113,13 +124,19 @@ public class MsDefinitionParser extends MsAbstractParser { modulePath = modulePath.substring(0, modulePath.length() - 1); } List modules = Arrays.asList(modulePath.split("/")); - ApiModule parent = ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId()); + ApiModule parent = this.selectModule; Iterator iterator = modules.iterator(); while (iterator.hasNext()) { String item = iterator.next(); parent = ApiDefinitionImportUtil.buildModule(parent, item, this.projectId); 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 208f7a345e..3df1c9b894 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 @@ -9,6 +9,7 @@ import io.metersphere.api.dto.parse.postman.PostmanKeyValue; import io.metersphere.api.parse.PostmanAbstractParserParser; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; +import org.apache.commons.lang3.StringUtils; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.base.domain.Project; import io.metersphere.base.mapper.ProjectMapper; @@ -20,6 +21,10 @@ 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); @@ -28,26 +33,31 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser 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); List cases = new ArrayList<>(); Map repeatMap = new HashMap(); ProjectMapper projectMapper = CommonBeanFactory.getBean(ProjectMapper.class); Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); parseItem(postmanCollection.getItem(), variables, results, - ApiDefinitionImportUtil.buildModule(ApiDefinitionImportUtil.getSelectModule(request.getModuleId()), - postmanCollection.getInfo().getName(), this.projectId), cases, repeatMap, project.getRepeatable()); + apiModule, apiModule.getName(), cases, repeatMap, project.getRepeatable()); apiImport.setData(results); apiImport.setCases(cases); return apiImport; } protected void parseItem(List items, List variables, List results, - ApiModule parentModule, List cases, Map repeatMap, Boolean repeatable) { + ApiModule parentModule, String path, 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, module, cases, repeatMap, repeatable); + parseItem(childItems, variables, results, module, path + "/" + module.getName(), cases, repeatMap, repeatable); } else { MsHTTPSamplerProxy msHTTPSamplerProxy = parsePostman(item); ApiDefinitionWithBLOBs request = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(), @@ -56,6 +66,11 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser results = new ArrayList<>(); - ApiModule parentNode = ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId()); + 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) { @@ -76,7 +83,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { } apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation, operation.getResponses()))); - buildModule(parentNode, apiDefinition, operation.getTags()); + buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath); results.add(apiDefinition); } } 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 920a8f09b4..ee4027e939 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 @@ -26,15 +26,12 @@ import io.swagger.v3.oas.models.parameters.*; import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; import io.swagger.v3.parser.core.models.SwaggerParseResult; -import net.sf.saxon.ma.json.XMLToJsonFn; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; -import org.fife.ui.rsyntaxtextarea.parser.XmlParser; import org.springframework.http.HttpMethod; import java.io.InputStream; -import java.math.BigDecimal; import java.util.*; @@ -84,7 +81,14 @@ public class Swagger3Parser extends SwaggerAbstractParser { List results = new ArrayList<>(); - ApiModule parentNode = ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId()); + 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); @@ -109,7 +113,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { addBodyHeader(request); apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses()))); - buildModule(parentNode, apiDefinition, operation.getTags()); + buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath); results.add(apiDefinition); } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/SwaggerAbstractParser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/SwaggerAbstractParser.java index 86e37543a8..c0a4380896 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/SwaggerAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/SwaggerAbstractParser.java @@ -3,16 +3,23 @@ package io.metersphere.api.dto.definition.parse; import io.metersphere.api.parse.ApiImportAbstractParser; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; +import org.apache.commons.lang3.StringUtils; import java.util.List; public abstract class SwaggerAbstractParser extends ApiImportAbstractParser { - protected void buildModule(ApiModule parentModule, ApiDefinitionWithBLOBs apiDefinition, List tags) { + protected void buildModule(ApiModule parentModule, ApiDefinitionWithBLOBs apiDefinition, + List tags, String selectModulePath) { if (tags != null) { tags.forEach(tag -> { ApiModule module = ApiDefinitionImportUtil.buildModule(parentModule, tag, this.projectId); apiDefinition.setModuleId(module.getId()); + if (StringUtils.isNotBlank(selectModulePath)) { + apiDefinition.setModulePath(selectModulePath + "/" + tag); + } else { + apiDefinition.setModulePath("/" + tag); + } }); } } diff --git a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java index 34cbaaa326..9f1ee10b21 100644 --- a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java @@ -198,20 +198,14 @@ public abstract class ApiImportAbstractParser implements ApiImportParser { } protected ApiScenarioWithBLOBs parseScenario(MsScenario msScenario) { -// ApiScenarioModule module = ApiScenarioImportUtil.buildModule(ApiScenarioImportUtil.getSelectModule(request.getModuleId()), msScenario.getName(), this.projectId); ApiScenarioWithBLOBs scenarioWithBLOBs = new ApiScenarioWithBLOBs(); scenarioWithBLOBs.setName(msScenario.getName()); scenarioWithBLOBs.setProjectId(this.projectId); if (msScenario != null && CollectionUtils.isNotEmpty(msScenario.getHashTree())) { scenarioWithBLOBs.setStepTotal(msScenario.getHashTree().size()); } -// if (module != null) { -// scenarioWithBLOBs.setApiScenarioModuleId(module.getId()); -// scenarioWithBLOBs.setModulePath("/" + module.getName()); -// } scenarioWithBLOBs.setId(UUID.randomUUID().toString()); scenarioWithBLOBs.setScenarioDefinition(JSON.toJSONString(msScenario)); return scenarioWithBLOBs; -// scenarioWithBLOBsList.add(scenarioWithBLOBs); } } 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 b620710743..3c33f855f2 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -275,6 +275,9 @@ public class ApiModuleService extends NodeTreeService { @Override public ApiModuleDTO getNode(String id) { ApiModule module = apiModuleMapper.selectByPrimaryKey(id); + if (module == null) { + return null; + } ApiModuleDTO dto = JSON.parseObject(JSON.toJSONString(module), ApiModuleDTO.class); return dto; }