From 82cc66dbcd279a9ad79e1365b1715808bbd0cf32 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 16 Jul 2020 13:10:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=AF=BC=E5=85=A5=E6=8F=92=E4=BB=B6=E5=BD=95?= =?UTF-8?q?=E5=88=B6json=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/api/parse/MsParser.java | 44 ++++++++++++++++++- .../api/test/components/ApiRequestForm.vue | 4 +- 2 files changed, 45 insertions(+), 3 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 cf2e006a28..e83176b1c9 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -1,7 +1,12 @@ package io.metersphere.api.parse; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.parse.ApiImport; +import io.metersphere.commons.constants.MsRequestBodyType; +import org.apache.commons.lang3.StringUtils; +import org.eclipse.jetty.http.HttpMethod; import java.io.InputStream; @@ -10,7 +15,44 @@ public class MsParser extends ApiImportAbstractParser { @Override public ApiImport parse(InputStream source) { String testStr = getApiTestStr(source); - return JSON.parseObject(testStr, ApiImport.class); + return JSON.parseObject(parsePluginFormat(testStr), ApiImport.class); } + private String parsePluginFormat(String testStr) { + JSONObject testObject = JSONObject.parseObject(testStr); + if (testObject.get("scenarios") != null) { + return testStr; + } else { + //插件格式 + 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 = requestsObject.getJSONObject(requestName); + requestObject.put("name", requestName); + JSONArray bodies = requestObject.getJSONArray("body"); + if (StringUtils.equalsIgnoreCase(requestObject.getString("method"), HttpMethod.POST.name()) && bodies != null) { + StringBuilder bodyStr = new StringBuilder(); + for (int i = 0; i < bodies.size(); i++) { + String body = bodies.getString(i); + bodyStr.append(body); + } + JSONObject bodyObject = new JSONObject(); + bodyObject.put("raw", bodyStr); + bodyObject.put("type", MsRequestBodyType.RAW.value()); + requestObject.put("body", bodyObject); + } + requestsObjects.add(requestObject); + }); + scenario.put("requests", requestsObjects); + scenarios.add(scenario); + }); + JSONObject result = new JSONObject(); + result.put("scenarios", scenarios); + return result.toJSONString(); + } + } } diff --git a/frontend/src/business/components/api/test/components/ApiRequestForm.vue b/frontend/src/business/components/api/test/components/ApiRequestForm.vue index c8b268cf2f..56a6c60292 100644 --- a/frontend/src/business/components/api/test/components/ApiRequestForm.vue +++ b/frontend/src/business/components/api/test/components/ApiRequestForm.vue @@ -2,7 +2,7 @@ - + @@ -91,7 +91,7 @@ activeName: "parameters", rules: { name: [ - {max: 100, message: this.$t('commons.input_limit', [1, 100]), trigger: 'blur'} + {max: 300, message: this.$t('commons.input_limit', [1, 300]), trigger: 'blur'} ], url: [ {max: 500, required: true, message: this.$t('commons.input_limit', [1, 500]), trigger: 'blur'},