From 91d37ccafe01f53e1b9c62291940492aec0a473c Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 8 Dec 2020 11:28:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/api/parse/MsParser.java | 123 +++++++++++------- .../metersphere/api/parse/Swagger2Parser.java | 8 +- frontend/src/business/components/xpack | 2 +- 3 files changed, 81 insertions(+), 52 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/parse/MsParser.java b/backend/src/main/java/io/metersphere/api/parse/MsParser.java index 676f8b224a..78effbfff9 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -5,12 +5,21 @@ 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.ApiDefinitionResult; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; +import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; +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.commons.constants.MsRequestBodyType; -import org.apache.commons.lang3.StringUtils; +import io.metersphere.api.service.ApiModuleService; +import io.metersphere.base.domain.ApiModule; +import io.metersphere.commons.utils.CommonBeanFactory; +import org.apache.commons.collections.CollectionUtils; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; public class MsParser extends ApiImportAbstractParser { @@ -18,43 +27,72 @@ public class MsParser extends ApiImportAbstractParser { public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) { String testStr = getApiTestStr(source); JSONObject testObject = JSONObject.parseObject(testStr, Feature.OrderedField); - if (testObject.get("projectName") == null) { - testStr = parsePluginFormat(testObject); + apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); + this.projectId = request.getProjectId(); + if (testObject.get("projectName") != null) { + return parseMsFormat(testStr, request); + } else { + return parsePluginFormat(testObject); } - ApiDefinitionImport apiImport = JSON.parseObject(testStr, ApiDefinitionImport.class); + } + + private ApiDefinitionImport parseMsFormat(String testStr, ApiTestImportRequest importRequest) { + + ApiDefinitionImport apiDefinitionImport = JSON.parseObject(testStr, ApiDefinitionImport.class); + List data = apiDefinitionImport.getData(); + data.forEach(apiDefinition -> { + String id = UUID.randomUUID().toString(); + apiDefinition.setModuleId(null); + apiDefinition.setId(id); + apiDefinition.setProjectId(this.projectId); + String request = apiDefinition.getRequest(); + JSONObject requestObj = JSONObject.parseObject(request); + requestObj.put("id", id); + apiDefinition.setRequest(JSONObject.toJSONString(requestObj)); + }); + return apiDefinitionImport; + } + + private ApiDefinitionImport parsePluginFormat(JSONObject testObject) { + List results = new ArrayList<>(); + ApiDefinitionImport apiImport = new ApiDefinitionImport(); + apiImport.setProtocol(RequestType.HTTP); + apiImport.setData(results); + testObject.keySet().forEach(tag -> { + ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1); + createModule(module); + JSONObject requests = testObject.getJSONObject(tag); + requests.keySet().forEach(requestName -> { + + JSONObject requestObject = requests.getJSONObject(requestName); + String path = requestObject.getString("url"); + String method = requestObject.getString("method"); + + MsHTTPSamplerProxy request = buildRequest(requestName, path, method); + ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), requestName, path, method); + apiDefinition.setModuleId(module.getId()); + apiDefinition.setProjectId(this.projectId); + + parseBody(requestObject, request.getBody()); + parseHeader(requestObject, request.getHeaders()); + apiDefinition.setRequest(JSONObject.toJSONString(request)); + results.add(apiDefinition); + }); + }); return apiImport; } - private String parsePluginFormat( JSONObject testObject) { - //插件格式 - JSONArray scenarios = new JSONArray(); - testObject.keySet().forEach(scenarioName -> { - JSONObject scenario = new JSONObject(); - scenario.put("name", scenarioName); - JSONArray requestsObjects = new JSONArray(); - JSONObject requestsObject = testObject.getJSONObject(scenarioName); - requestsObject.keySet().forEach(requestName -> { - JSONObject requestObject = new JSONObject(true); - JSONObject requestTmpObject = requestsObject.getJSONObject(requestName); - //排序,确保type在第一个,否则转换失败 - if (StringUtils.isBlank(requestTmpObject.getString("type"))) { - requestObject.put("type", RequestType.HTTP); - } - - requestTmpObject.keySet().forEach(key -> requestObject.put(key, requestTmpObject.get(key))); - requestObject.put("name", requestName); - parseBody(requestObject); - requestsObjects.add(requestObject); - }); - scenario.put("requests", requestsObjects); - scenarios.add(scenario); - }); - JSONObject result = new JSONObject(); - result.put("scenarios", scenarios); - return result.toJSONString(); + private void parseHeader(JSONObject requestObject, List msHeaders) { + JSONArray headers = requestObject.getJSONArray("headers"); + if (CollectionUtils.isNotEmpty(headers)) { + for (int i = 0; i < headers.size(); i++) { + JSONObject header = headers.getJSONObject(i); + msHeaders.add(new KeyValue(header.getString("name"), header.getString("value"))); + } + } } - private void parseBody(JSONObject requestObject) { + private void parseBody(JSONObject requestObject, Body msBody) { if (requestObject.containsKey("body")) { Object body = requestObject.get("body"); if (body instanceof JSONArray) { @@ -65,25 +103,18 @@ public class MsParser extends ApiImportAbstractParser { String tmp = bodies.getString(i); bodyStr.append(tmp); } - JSONObject bodyObject = new JSONObject(); - bodyObject.put("raw", bodyStr); - bodyObject.put("type", MsRequestBodyType.RAW.value()); - requestObject.put("body", bodyObject); + msBody.setType(Body.RAW); + msBody.setRaw(bodyStr.toString()); } } else if (body instanceof JSONObject) { JSONObject bodyObj = requestObject.getJSONObject("body"); if (bodyObj != null) { - JSONArray kvs = new JSONArray(); + ArrayList kvs = new ArrayList<>(); bodyObj.keySet().forEach(key -> { - JSONObject kv = new JSONObject(); - kv.put("name", key); - kv.put("value", bodyObj.getString(key)); - kvs.add(kv); + kvs.add(new KeyValue(key, bodyObj.getString(key))); }); - JSONObject bodyRes = new JSONObject(); - bodyRes.put("kvs", kvs); - bodyRes.put("type", MsRequestBodyType.KV.value()); - requestObject.put("body", bodyRes); + msBody.setKvs(kvs); + msBody.setType(Body.WWW_FROM); } } } diff --git a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java index 112067cd4a..b5dea282b7 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java @@ -156,9 +156,9 @@ public class Swagger2Parser extends ApiImportAbstractParser { case "application/xml": bodyType = Body.XML; break; -// case "": //todo binary 啥类型 -// bodyType = Body.BINARY; -// break; + case "": + bodyType = Body.BINARY; + break; default: bodyType = Body.RAW; } @@ -235,7 +235,6 @@ public class Swagger2Parser extends ApiImportAbstractParser { refSet.add(simpleRef); if (model != null) { JSONObject bodyParameters = getBodyParameters(model.getProperties(), refSet); - //body.setRaw(bodyParameters.toJSONString()); return bodyParameters.toJSONString(); } } else if (schema instanceof ArrayModel) { @@ -250,7 +249,6 @@ public class Swagger2Parser extends ApiImportAbstractParser { Model model = definitions.get(simpleRef); JSONArray propertyList = new JSONArray(); propertyList.add(getBodyParameters(model.getProperties(), refSet)); - // body.setRaw(propertyList.toString()); return propertyList.toString(); } } diff --git a/frontend/src/business/components/xpack b/frontend/src/business/components/xpack index 8a972a1987..a22a3005d9 160000 --- a/frontend/src/business/components/xpack +++ b/frontend/src/business/components/xpack @@ -1 +1 @@ -Subproject commit 8a972a198775b3783ed6e4cef27197e53d1ebdc8 +Subproject commit a22a3005d9bd254793fcf634d72539cbdf31be3a