From 302d138dd3d6da44b6094f7f515c7511ef728012 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 24 Nov 2022 12:42:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=86=85=E5=AE=B9=E6=98=AFjson=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=97=B6=E8=BF=9B=E8=A1=8C=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1019717 --user=赵勇 【接口测试】github#19744,请求内容 post data json-schema 没有格式化显示 https://www.tapd.cn/55049933/s/1304199 --- .../java/io/metersphere/api/dto/scenario/Body.java | 2 +- .../api/exec/generator/JSONSchemaRunTest.java | 6 ++++-- .../java/io/metersphere/commons/utils/JSONUtil.java | 12 ++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java index bc4af4c40b..8b1c96413d 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java @@ -104,7 +104,7 @@ public class Body { if (!this.getRaw().contains("$ref")) { jsonMockParse(jsonObject); } - this.raw = jsonObject.toString(); + this.raw = JSONUtil.parser(jsonObject.toString()); } } catch (Exception e) { LoggerUtil.error("json mock value is abnormal", e); diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java index ddaf7fd738..f69bd76b0e 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java @@ -4,8 +4,10 @@ package io.metersphere.api.exec.generator; import com.google.gson.*; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.utils.EnumPropertyUtil; +import io.metersphere.commons.utils.JSONUtil; import io.metersphere.jmeter.utils.ScriptEngineUtils; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONObject; @@ -293,12 +295,12 @@ public class JSONSchemaRunTest { } Map map = new HashMap<>(); String json = formerJson(jsonSchema, map); - if (!map.isEmpty()) { + if (MapUtils.isNotEmpty(map)) { for (String str : map.keySet()) { json = json.replace(str, map.get(str)); } } - return json; + return JSONUtil.parser(json); } catch (Exception ex) { return jsonSchema; } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java index 7246e1a101..5265a8146c 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java @@ -7,6 +7,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParser; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.exception.MSException; import org.apache.commons.lang3.ObjectUtils; @@ -224,6 +227,15 @@ public class JSONUtil { return objectMapper.createObjectNode(); } + public static String parser(String content) { + try { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + return gson.toJson(JsonParser.parseString(content).getAsJsonObject()); + } catch (Exception e) { + return content; + } + } + public static ObjectNode createObj() { return objectMapper.createObjectNode(); }