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(); }