From 2d70165fcd1e03475dbe19eeeb6f181725f881c8 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Sat, 2 Apr 2022 18:36:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AF=BC=E5=85=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user=郭雨琦 --bug=1011903 接口导入数据调整,接口自动化导入数据调整 https://www.tapd.cn/55049933/bugtrace/bugs/view?bug_id=1155049933001011903 --- .../parse/ApiScenarioImportUtil.java | 25 +++++++++++-------- .../definition/parse/MsDefinitionParser.java | 10 ++++++++ .../api/parse/ApiImportAbstractParser.java | 2 +- 3 files changed, 25 insertions(+), 12 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 5169b0c324..11af6ae202 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 @@ -1,10 +1,13 @@ package io.metersphere.api.dto.automation.parse; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.automation.ApiScenarioModuleDTO; import io.metersphere.api.dto.definition.ApiDefinitionResult; import io.metersphere.api.dto.definition.parse.ms.NodeTree; +import io.metersphere.api.dto.definition.response.HttpResponse; +import io.metersphere.api.parse.ApiImportAbstractParser; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiScenarioModuleService; import io.metersphere.api.service.ApiTestCaseService; @@ -180,7 +183,15 @@ public class ApiScenarioImportUtil { test.setId(id); test.setNum(apiDefinitionService.getNextNum(projectId)+i); test.setName(object.getString("name")); - test.setPath(object.getString("path")); + if(StringUtils.isBlank(object.getString("path"))){ + if(StringUtils.isNotBlank(object.getString("url"))){ + ApiImportAbstractParser apiImportAbstractParser = CommonBeanFactory.getBean(ApiImportAbstractParser.class); + String path = apiImportAbstractParser.formatPath(object.getString("url")); + test.setPath(path); + } + }else{ + test.setPath(object.getString("path")); + } test.setCreateUser(SessionUtils.getUserId()); test.setProjectId(projectId); test.setCreateTime(System.currentTimeMillis()); @@ -193,20 +204,12 @@ public class ApiScenarioImportUtil { object.put("resourceId", test.getId()); object.put("projectId", projectId); object.put("useEnvironment",""); + object.put("url",""); JSONObject objectNew = JSONObject.parseObject(object.toJSONString()); objectNew.remove("refType"); objectNew.remove("referenced"); test.setRequest(objectNew.toJSONString()); - JSONObject obj = new JSONObject(); - obj.put("type", object.get("protocol")); - obj.put("body", object.get("body")); - obj.put("headers", object.get("headers")); - Mapmap = new HashMap<>(); - map.put("enable", true); - List> list = new ArrayList<>(); - list.add(map); - obj.put("statusCode", list); - test.setResponse(obj.toJSONString()); + test.setResponse(JSON.toJSONString(new HttpResponse())); test.setUserId(SessionUtils.getUserId()); test.setLatest(true); test.setOrder(apiDefinitionService.getImportNextOrder(projectId)); 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 5297b9e124..4abafe5a7d 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 @@ -7,11 +7,13 @@ import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.definition.parse.ms.NodeTree; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.scenario.request.RequestType; +import io.metersphere.api.parse.ApiImportAbstractParser; import io.metersphere.api.parse.MsAbstractParser; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.commons.constants.ApiImportPlatform; +import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.SessionUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -123,6 +125,14 @@ public class MsDefinitionParser extends MsAbstractParser { if(requestObj.get("projectId")!=null){ requestObj.put("projectId", apiDefinition.getProjectId()); } + if(StringUtils.isBlank(requestObj.getString("path"))){ + if(StringUtils.isNotBlank(requestObj.getString("url"))){ + ApiImportAbstractParser apiImportAbstractParser = CommonBeanFactory.getBean(ApiImportAbstractParser.class); + String path = apiImportAbstractParser.formatPath(requestObj.getString("url")); + requestObj.put("path",path); + } + } + requestObj.put("url",""); apiDefinition.setRequest(JSONObject.toJSONString(requestObj)); apiDefinition.setCreateUser(SessionUtils.getUserId()); apiDefinition.setUserId(SessionUtils.getUserId()); 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 015e647764..72a67d5ffd 100644 --- a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java @@ -135,7 +135,7 @@ public abstract class ApiImportAbstractParser implements ApiImportParser { return apiDefinition; } - private String formatPath(String url) { + public String formatPath(String url) { try { URL urlObject = new URL(url); String path = StringUtils.isBlank(urlObject.getPath()) ? "/" : urlObject.getPath();