fix(接口测试): 修复跟随API数据展示精度缺失问题

--bug=1028295 --user=王孝刚
【接口测试】接口mock-响应为json开启schema-执行mock-返回响应数据数组中number类型转换为字符串类型了
https://www.tapd.cn/55049933/s/1401818
--bug=1028296 --user=王孝刚
【接口测试】mock响应中number类型精度自动去除了-推荐断言数值类型未显示前缀number
https://www.tapd.cn/55049933/s/1401866
This commit is contained in:
wxg0103 2023-08-08 18:59:19 +08:00 committed by fit2-zhao
parent 113d10d23d
commit 345c519f12
2 changed files with 33 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package io.metersphere.api.exec.generator;
import com.google.gson.*;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections4.MapUtils;
@ -13,6 +14,8 @@ import org.springframework.util.NumberUtils;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -220,7 +223,7 @@ public class JSONSchemaBuilder {
});
}
private static void formatItems(JsonObject itemsObject, JSONArray array, Map<String, String> map) {
public static void formatItems(JsonObject itemsObject, JSONArray array, Map<String, String> map) {
String type = StringUtils.EMPTY;
if (itemsObject.has(PropertyConstant.TYPE)) {
type = itemsObject.get(PropertyConstant.TYPE).getAsString();
@ -279,9 +282,12 @@ public class JSONSchemaBuilder {
analyzeSchema(jsonSchema, root, map);
// 格式化返回
if (root.opt(PropertyConstant.MS_OBJECT) != null) {
return root.get(PropertyConstant.MS_OBJECT).toString();
JSONArray jsonArray = (JSONArray) root.get(PropertyConstant.MS_OBJECT);
List<String> list = new LinkedList<>();
JSONSchemaParser.toJsonString(jsonArray, list);
return list.toString();
}
return root.toString();
return JSON.toJSONString(root.toMap());
}
public static String generator(String jsonSchema) {

View File

@ -3,6 +3,7 @@ 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.JSON;
import io.metersphere.commons.utils.JSONUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
@ -13,6 +14,7 @@ import org.json.JSONObject;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -181,13 +183,7 @@ public class JSONSchemaParser {
JsonObject jsonObject = element.getAsJsonObject();
if (object.has(PropertyConstant.ITEMS)) {
if (FormatterUtil.isMockValue(jsonObject)) {
if (jsonObject.has(PropertyConstant.TYPE)
&& jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.INTEGER)
&& FormatterUtil.isNumber(FormatterUtil.getStrValue(jsonObject))) {
array.put(FormatterUtil.getElementValue(jsonObject).getAsInt());
} else {
array.put(FormatterUtil.getStrValue(jsonObject));
}
JSONSchemaBuilder.formatItems(jsonObject, array, processMap);
} else if (jsonObject.has(PropertyConstant.ENUM)) {
array.put(EnumPropertyUtil.analyzeEnumProperty(jsonObject));
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.STRING)) {
@ -240,9 +236,12 @@ public class JSONSchemaParser {
analyzeSchema(jsonSchema, root, processMap);
// 格式化返回
if (root.opt(PropertyConstant.MS_OBJECT) != null) {
json = root.get(PropertyConstant.MS_OBJECT).toString();
JSONArray jsonArray = (JSONArray) root.get(PropertyConstant.MS_OBJECT);
List<String> list = new LinkedList<>();
toJsonString(jsonArray, list);
json = list.toString();
} else {
json = root.toString();
json = JSON.toJSONString(root.toMap());
}
if (MapUtils.isNotEmpty(processMap)) {
for (String str : processMap.keySet()) {
@ -252,6 +251,22 @@ public class JSONSchemaParser {
return json;
}
public static void toJsonString(JSONArray jsonArray, List<String> list) {
jsonArray.forEach(element -> {
if (element instanceof JSONObject o) {
list.add(JSON.toJSONString(o.toMap()));
} else if (element instanceof JSONArray o) {
List<String> aa = new LinkedList<>();
toJsonString(o, aa);
list.add(aa.toString());
} else if (element instanceof BigDecimal) {
list.add(element.toString());
} else {
list.add(element.toString());
}
});
}
public static String schemaToJson(String jsonSchema) {
try {
if (StringUtils.isEmpty(jsonSchema)) {