fix(接口测试): 修复JSON Schema Integer 类型处理问题

--bug=1044960 --user=赵勇 [接口测试]github#32536请求体内使用Mock的@integer函数并指定类型为integer,请求参数仍然是字符串类型 https://www.tapd.cn/55049933/s/1564131

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2024-08-16 16:06:09 +08:00 committed by Craftsman
parent e46a2c2ea8
commit cf08833361
1 changed files with 36 additions and 36 deletions

View File

@ -18,8 +18,21 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.regex.Pattern;
public class JSONSchemaBuilder {
private static final Pattern INTEGER_PATTERN = Pattern.compile("^@integer|@natural");
private static final Pattern BOOLEAN_PATTERN = Pattern.compile("^@boolean");
private static final Pattern FLOAT_PATTERN = Pattern.compile("^@float");
private static final Map<String, Function<String, Object>> handlers = new HashMap<>();
static {
handlers.put("@integer", str -> NumberUtils.parseNumber(str, Long.class));
handlers.put("@boolean", Boolean::parseBoolean);
handlers.put("@float", Float::parseFloat);
}
private static void analyzeSchema(String json, JSONObject rootObj, Map<String, String> map) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class);
@ -63,20 +76,18 @@ public class JSONSchemaBuilder {
private static boolean valueOf(String evlValue, String propertyName, JSONObject concept) {
if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) {
case "@integer":
case "@natural":
if (INTEGER_PATTERN.matcher(evlValue).find()) {
// Parse number as Long
concept.put(propertyName, NumberUtils.parseNumber(str, Long.class));
break;
case "@boolean":
} else if (BOOLEAN_PATTERN.matcher(evlValue).find()) {
// Parse boolean
concept.put(propertyName, Boolean.parseBoolean(str));
break;
case "@float":
} else if (FLOAT_PATTERN.matcher(evlValue).find()) {
// Parse float
concept.put(propertyName, Float.parseFloat(str));
break;
default:
} else {
// Default case
concept.put(propertyName, str);
break;
}
return false;
} else {
@ -88,25 +99,14 @@ public class JSONSchemaBuilder {
private static void arrayValueOf(String evlValue, JSONArray array) {
if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) {
case "@integer":
array.put(NumberUtils.parseNumber(str, Long.class));
break;
case "@boolean":
array.put(Boolean.parseBoolean(str));
break;
case "@float":
array.put(Float.parseFloat(str));
break;
default:
array.put(str);
break;
}
Function<String, Object> handler = handlers.getOrDefault(evlValue, s -> s);
array.put(handler.apply(str));
} else {
array.put(ScriptEngineUtils.buildFunctionCallString(evlValue));
}
}
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object, Map<String, String> map) {
if (!object.has(PropertyConstant.TYPE)) {
return;
@ -115,14 +115,14 @@ public class JSONSchemaBuilder {
if (object.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get(PropertyConstant.TYPE).getAsString();
}
if (propertyObjType.equals(PropertyConstant.STRING) || propertyObjType.equals(PropertyConstant.ENUM)) {
if (StringUtils.equals(propertyObjType, PropertyConstant.STRING) || StringUtils.equals(propertyObjType, PropertyConstant.ENUM)) {
concept.put(propertyName, object.has(PropertyConstant.MOCK) ? FormatterUtil.getMockValue(object) : StringUtils.EMPTY);
} else if (propertyObjType.equals(PropertyConstant.INTEGER) || propertyObjType.equals(PropertyConstant.NUMBER)) {
} else if (StringUtils.equals(propertyObjType, PropertyConstant.INTEGER) || StringUtils.equals(propertyObjType, PropertyConstant.NUMBER)) {
try {
concept.put(propertyName, 0);
if (FormatterUtil.isMockValue(object)) {
Number value = FormatterUtil.getElementValue(object).getAsNumber();
if (value.toString().indexOf(".") == -1) {
if (!value.toString().contains(".")) {
concept.put(propertyName, value.longValue());
} else {
concept.put(propertyName, new BigDecimal(object.getAsString()));
@ -135,14 +135,14 @@ public class JSONSchemaBuilder {
processValue(concept, map, propertyName, value);
}
}
} else if (propertyObjType.equals(PropertyConstant.BOOLEAN)) {
} else if (StringUtils.equals(propertyObjType, PropertyConstant.BOOLEAN)) {
// 先设置空值
concept.put(propertyName, false);
try {
if (FormatterUtil.isMockValue(object)) {
String value = FormatterUtil.getMockValue(object);
if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) {
if (value.contains("\"")) {
value = value.replaceAll("\"", StringUtils.EMPTY);
}
if (isBoolean(value)) {
@ -155,9 +155,9 @@ public class JSONSchemaBuilder {
} catch (Exception e) {
concept.put(propertyName, false);
}
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
} else if (StringUtils.equals(propertyObjType, PropertyConstant.ARRAY)) {
analyzeArray(concept, propertyName, object, map);
} else if (propertyObjType.equals(PropertyConstant.OBJECT)) {
} else if (StringUtils.equals(propertyObjType, PropertyConstant.OBJECT)) {
JSONObject obj = new JSONObject();
concept.put(propertyName, obj);
analyzeObject(object, obj, map);
@ -238,7 +238,7 @@ public class JSONSchemaBuilder {
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.NUMBER)) {
JsonElement valueObj = FormatterUtil.getElementValue(itemsObject);
String value = valueObj.getAsString();
if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().indexOf(".") != -1) {
if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().contains(".")) {
array.put(new BigDecimal(value));
} else {
array.put(Integer.valueOf(value));