fix(接口测试): 请求内容是json数据时进行格式化处理

--bug=1019717 --user=赵勇 【接口测试】github#19744,请求内容 post data json-schema 没有格式化显示 https://www.tapd.cn/55049933/s/1304199
This commit is contained in:
fit2-zhao 2022-11-24 12:42:22 +08:00 committed by fit2-zhao
parent 0da58df7cf
commit 302d138dd3
3 changed files with 17 additions and 3 deletions

View File

@ -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);

View File

@ -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<String, String> 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;
}

View File

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