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