fix(接口测试): 修复schema类型数据执行取值类型错误问题

--bug=1011864 --user=赵勇 【接口测试】github#12179,开启json-schema后,POST接口JSON数组里面的字符串类型错误 https://www.tapd.cn/55049933/s/1129586
This commit is contained in:
fit2-zhao 2022-04-02 16:17:27 +08:00 committed by fit2-zhao
parent cab59fbac0
commit 043ca67d86
1 changed files with 110 additions and 98 deletions

View File

@ -33,6 +33,11 @@ public class JSONSchemaRunTest {
} }
} }
private static boolean isMock(JsonObject itemsObject) {
return (itemsObject.has(BasicConstant.MOCK) && itemsObject.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()));
}
private static void analyzeObject(JsonObject object, JSONObject rootObj, Map<String, String> map) { private static void analyzeObject(JsonObject object, JSONObject rootObj, Map<String, String> map) {
if (object.has(BasicConstant.ALL_OF)) { if (object.has(BasicConstant.ALL_OF)) {
JsonArray allOfArray = object.get(BasicConstant.ALL_OF).getAsJsonArray(); JsonArray allOfArray = object.get(BasicConstant.ALL_OF).getAsJsonArray();
@ -67,7 +72,9 @@ public class JSONSchemaRunTest {
private static void analyzeProperty(JSONObject concept, String propertyName, private static void analyzeProperty(JSONObject concept, String propertyName,
JsonObject object, Map<String, String> map) { JsonObject object, Map<String, String> map) {
if (object.has(BasicConstant.TYPE)) { if (!object.has(BasicConstant.TYPE)) {
return;
}
String propertyObjType = null; String propertyObjType = null;
if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) { if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get(BasicConstant.TYPE).getAsString(); propertyObjType = object.get(BasicConstant.TYPE).getAsString();
@ -77,8 +84,7 @@ public class JSONSchemaRunTest {
} else if (propertyObjType.equals(BasicConstant.INTEGER) || propertyObjType.equals(BasicConstant.NUMBER)) { } else if (propertyObjType.equals(BasicConstant.INTEGER) || propertyObjType.equals(BasicConstant.NUMBER)) {
try { try {
concept.put(propertyName, 0); concept.put(propertyName, 0);
if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null if (isMock(object)) {
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
Number value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber(); Number value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
if (value.toString().indexOf(".") == -1) { if (value.toString().indexOf(".") == -1) {
concept.put(propertyName, value.longValue()); concept.put(propertyName, value.longValue());
@ -94,10 +100,8 @@ public class JSONSchemaRunTest {
// 先设置空值 // 先设置空值
concept.put(propertyName, false); concept.put(propertyName, false);
try { try {
if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null if (isMock(object)) {
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).toString()); String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).toString());
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) { if (value.indexOf("\"") != -1) {
value = value.replaceAll("\"", ""); value = value.replaceAll("\"", "");
@ -128,11 +132,22 @@ public class JSONSchemaRunTest {
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
JsonObject itemsObject = jsonArray.get(i).getAsJsonObject(); JsonObject itemsObject = jsonArray.get(i).getAsJsonObject();
if (object.has("items")) { if (object.has("items")) {
if (itemsObject.has(BasicConstant.MOCK) && itemsObject.get(BasicConstant.MOCK).getAsJsonObject() != null if (isMock(itemsObject)) {
&& StringUtils.isNotEmpty(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
String type = "";
if (itemsObject.has(BasicConstant.TYPE)) {
type = itemsObject.get(BasicConstant.TYPE).getAsString();
}
if (StringUtils.equalsIgnoreCase(type, BasicConstant.STRING)) {
String value = itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString();
array.add(value);
} else if (StringUtils.equalsIgnoreCase(type, BasicConstant.INTEGER)) {
int value = itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsInt(); int value = itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsInt();
array.add(value); array.add(value);
} else if (StringUtils.equalsIgnoreCase(type, BasicConstant.NUMBER)) {
Number value = itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
array.add(value);
}
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
@ -140,8 +155,7 @@ public class JSONSchemaRunTest {
} else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) { } else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) {
array.add(getValue(itemsObject)); array.add(getValue(itemsObject));
} else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) { } else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) {
if (itemsObject.has(BasicConstant.MOCK) && itemsObject.get(BasicConstant.MOCK).getAsJsonObject() != null if (isMock(itemsObject)) {
&& StringUtils.isNotEmpty(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
} else { } else {
@ -176,12 +190,10 @@ public class JSONSchemaRunTest {
concept.put(propertyName, null); concept.put(propertyName, null);
} }
} }
}
private static Object getValue(JsonObject object) { private static Object getValue(JsonObject object) {
try { try {
if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null if (isMock(object)) {
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
return value; return value;
} }