From 875ef58d96b9270186359f074dc5def913750c57 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Mon, 17 Oct 2022 12:24:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AF=BC=E5=85=A5postman=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8Cx-www-form-urlencoded=E6=A0=BC=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=BD=93=E6=B2=A1=E6=98=BE=E7=A4=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001018159 --user=郭雨琦 --- .../api/parse/HarScenarioAbstractParser.java | 18 ++++++++------- .../parse/PostmanAbstractParserParser.java | 23 +++++++++++-------- .../api/parse/postman/PostmanRequest.java | 5 ++-- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java index 7b1e2df957..6dc42f0b38 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java @@ -1,6 +1,8 @@ package io.metersphere.api.parse; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.scenario.Body; @@ -189,18 +191,18 @@ public abstract class HarScenarioAbstractParser extends ApiImportAbstractPars } private void parseBody(Body body, PostmanRequest requestDesc) { - JSONObject postmanBody = requestDesc.getBody(); + ObjectNode postmanBody = requestDesc.getBody(); if (postmanBody == null) { return; } - String bodyMode = postmanBody.optString("mode"); + String bodyMode = postmanBody.get("mode").textValue(); if (StringUtils.isBlank(bodyMode)) { return; } if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) { parseRawBody(body, postmanBody, bodyMode); } else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) { - List postmanKeyValues = JSON.parseArray(postmanBody.optString(bodyMode), PostmanKeyValue.class); + List postmanKeyValues = JSON.parseArray(postmanBody.get(bodyMode).textValue(), PostmanKeyValue.class); body.setKvs(parseKeyValue(postmanKeyValues)); if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) { body.setType(Body.FORM_DATA); @@ -218,15 +220,15 @@ public abstract class HarScenarioAbstractParser extends ApiImportAbstractPars return XMLUtil.jsonToXmlStr(object); } - private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) { - body.setRaw(postmanBody.optString(bodyMode)); + private void parseRawBody(Body body, ObjectNode postmanBody, String bodyMode) { + body.setRaw(postmanBody.get(bodyMode).textValue()); body.setType(MsRequestBodyType.RAW.value()); - JSONObject options = postmanBody.optJSONObject("options"); + JsonNode options = postmanBody.get("options"); if (options != null) { - JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value()); + JsonNode raw = options.get(PostmanRequestBodyMode.RAW.value()); if (raw != null) { String bodyType = ""; - switch (raw.optString("language")) { + switch (raw.get("language").textValue()) { case "json": bodyType = Body.JSON_STR; break; diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java index ff6f837086..a8f08df623 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java @@ -1,6 +1,8 @@ package io.metersphere.api.parse; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.scenario.Body; @@ -30,7 +32,8 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa } requestDesc.getAuth(); // todo 认证方式等待优化 PostmanUrl url = requestDesc.getUrl(); - MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? "" : url.getRaw(), requestDesc.getMethod(), Optional.ofNullable(requestDesc.getBody()).orElse(new JSONObject()).optString("jsonSchema")); + MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? "" : url.getRaw(), requestDesc.getMethod(), + requestDesc.getBody().get("jsonSchema") == null ? "" : requestDesc.getBody().get("jsonSchema").textValue()); request.setRest(parseKeyValue(requestDesc.getUrl().getVariable())); if (StringUtils.isNotBlank(request.getPath())) { String path = request.getPath().split("\\?")[0]; @@ -132,18 +135,20 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa } private void parseBody(Body body, PostmanRequest requestDesc) { - JSONObject postmanBody = requestDesc.getBody(); + ObjectNode postmanBody = requestDesc.getBody(); if (postmanBody == null) { return; } - String bodyMode = postmanBody.optString("mode"); + String bodyMode = postmanBody.get("mode").textValue(); if (StringUtils.isBlank(bodyMode)) { return; } if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) { parseRawBody(body, postmanBody, bodyMode); } else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) { - List postmanKeyValues = JSON.parseArray(parseVariable(postmanBody.optString(bodyMode)), PostmanKeyValue.class); + String s1 = postmanBody.get(bodyMode).toString(); + String s = parseVariable(s1); + List postmanKeyValues = JSON.parseArray(s, PostmanKeyValue.class); body.setKvs(parseKeyValue(postmanKeyValues)); if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) { body.setType(Body.FORM_DATA); @@ -156,15 +161,15 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa } } - private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) { - body.setRaw(parseVariable(postmanBody.optString(bodyMode))); + private void parseRawBody(Body body, ObjectNode postmanBody, String bodyMode) { + body.setRaw(parseVariable(postmanBody.get(bodyMode).textValue())); body.setType(MsRequestBodyType.RAW.value()); - JSONObject options = postmanBody.optJSONObject("options"); + JsonNode options = postmanBody.get("options"); if (options != null) { - JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value()); + JsonNode raw = options.get(PostmanRequestBodyMode.RAW.value()); if (raw != null) { String bodyType = ""; - switch (raw.optString("language")) { + switch (raw.get("language").textValue()) { case "json": bodyType = Body.JSON_STR; break; diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/postman/PostmanRequest.java b/api-test/backend/src/main/java/io/metersphere/api/parse/postman/PostmanRequest.java index 9a096c4f60..2f695f52c1 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/postman/PostmanRequest.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/postman/PostmanRequest.java @@ -1,6 +1,7 @@ package io.metersphere.api.parse.postman; +import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.Data; import org.json.JSONObject; @@ -12,8 +13,8 @@ public class PostmanRequest { private String method; private String schema; private List header; - private JSONObject body; - private JSONObject auth; + private ObjectNode body; + private ObjectNode auth; private PostmanUrl url; private String description; }