fix(接口测试): 修复schema类型数据执行取值类型错误问题
--bug=1011864 --user=赵勇 【接口测试】github#12179,开启json-schema后,POST接口JSON数组里面的字符串类型错误 https://www.tapd.cn/55049933/s/1129586
This commit is contained in:
parent
7cf15aa507
commit
2db9b09845
|
@ -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) {
|
||||
if (object.has(BasicConstant.ALL_OF)) {
|
||||
JsonArray allOfArray = object.get(BasicConstant.ALL_OF).getAsJsonArray();
|
||||
|
@ -67,121 +72,128 @@ public class JSONSchemaRunTest {
|
|||
|
||||
private static void analyzeProperty(JSONObject concept, String propertyName,
|
||||
JsonObject object, Map<String, String> map) {
|
||||
if (object.has(BasicConstant.TYPE)) {
|
||||
String propertyObjType = null;
|
||||
if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
|
||||
propertyObjType = object.get(BasicConstant.TYPE).getAsString();
|
||||
}
|
||||
if (propertyObjType.equals(BasicConstant.STRING) || propertyObjType.equals(BasicConstant.ENUM)) {
|
||||
concept.put(propertyName, getValue(object));
|
||||
} else if (propertyObjType.equals(BasicConstant.INTEGER) || propertyObjType.equals(BasicConstant.NUMBER)) {
|
||||
try {
|
||||
concept.put(propertyName, 0);
|
||||
if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
|
||||
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
|
||||
Number value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
|
||||
if (value.toString().indexOf(".") == -1) {
|
||||
concept.put(propertyName, value.longValue());
|
||||
} else {
|
||||
concept.put(propertyName, value.floatValue());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
|
||||
concept.put(propertyName, value);
|
||||
}
|
||||
} else if (propertyObjType.equals(BasicConstant.BOOLEAN)) {
|
||||
// 先设置空值
|
||||
concept.put(propertyName, false);
|
||||
try {
|
||||
if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
|
||||
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).toString());
|
||||
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
if (value.indexOf("\"") != -1) {
|
||||
value = value.replaceAll("\"", "");
|
||||
}
|
||||
if (isBoolean(value)) {
|
||||
concept.put(propertyName, Boolean.valueOf(value));
|
||||
} else {
|
||||
concept.put(propertyName, value);
|
||||
map.put("\"" + propertyName + "\"" + ": \"" + value + "\"", "\"" + propertyName + "\"" + ":" + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
concept.put(propertyName, false);
|
||||
}
|
||||
} else if (propertyObjType.equals(BasicConstant.ARRAY)) {
|
||||
List<Object> array = new LinkedList<>();
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
if (object.has("items")) {
|
||||
if (object.get("items").isJsonArray()) {
|
||||
jsonArray = object.get("items").getAsJsonArray();
|
||||
if (!object.has(BasicConstant.TYPE)) {
|
||||
return;
|
||||
}
|
||||
String propertyObjType = null;
|
||||
if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
|
||||
propertyObjType = object.get(BasicConstant.TYPE).getAsString();
|
||||
}
|
||||
if (propertyObjType.equals(BasicConstant.STRING) || propertyObjType.equals(BasicConstant.ENUM)) {
|
||||
concept.put(propertyName, getValue(object));
|
||||
} else if (propertyObjType.equals(BasicConstant.INTEGER) || propertyObjType.equals(BasicConstant.NUMBER)) {
|
||||
try {
|
||||
concept.put(propertyName, 0);
|
||||
if (isMock(object)) {
|
||||
Number value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
|
||||
if (value.toString().indexOf(".") == -1) {
|
||||
concept.put(propertyName, value.longValue());
|
||||
} else {
|
||||
JsonObject itemsObject = object.get("items").getAsJsonObject();
|
||||
array.add(itemsObject);
|
||||
concept.put(propertyName, value.floatValue());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
|
||||
concept.put(propertyName, value);
|
||||
}
|
||||
} else if (propertyObjType.equals(BasicConstant.BOOLEAN)) {
|
||||
// 先设置空值
|
||||
concept.put(propertyName, false);
|
||||
try {
|
||||
if (isMock(object)) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).toString());
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
if (value.indexOf("\"") != -1) {
|
||||
value = value.replaceAll("\"", "");
|
||||
}
|
||||
if (isBoolean(value)) {
|
||||
concept.put(propertyName, Boolean.valueOf(value));
|
||||
} else {
|
||||
concept.put(propertyName, value);
|
||||
map.put("\"" + propertyName + "\"" + ": \"" + value + "\"", "\"" + propertyName + "\"" + ":" + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
concept.put(propertyName, false);
|
||||
}
|
||||
} else if (propertyObjType.equals(BasicConstant.ARRAY)) {
|
||||
List<Object> array = new LinkedList<>();
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
if (object.has("items")) {
|
||||
if (object.get("items").isJsonArray()) {
|
||||
jsonArray = object.get("items").getAsJsonArray();
|
||||
} else {
|
||||
JsonObject itemsObject = object.get("items").getAsJsonObject();
|
||||
array.add(itemsObject);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
JsonObject itemsObject = jsonArray.get(i).getAsJsonObject();
|
||||
if (object.has("items")) {
|
||||
if (itemsObject.has(BasicConstant.MOCK) && itemsObject.get(BasicConstant.MOCK).getAsJsonObject() != null
|
||||
&& StringUtils.isNotEmpty(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
|
||||
try {
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
JsonObject itemsObject = jsonArray.get(i).getAsJsonObject();
|
||||
if (object.has("items")) {
|
||||
if (isMock(itemsObject)) {
|
||||
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();
|
||||
array.add(value);
|
||||
} catch (Exception e) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
|
||||
} else if (StringUtils.equalsIgnoreCase(type, BasicConstant.NUMBER)) {
|
||||
Number value = itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
|
||||
array.add(value);
|
||||
}
|
||||
} else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) {
|
||||
array.add(getValue(itemsObject));
|
||||
} 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
|
||||
&& StringUtils.isNotEmpty(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);
|
||||
} else {
|
||||
array.add(0);
|
||||
}
|
||||
} else if (itemsObject.has(BasicConstant.PROPERTIES)) {
|
||||
JSONObject propertyConcept = new JSONObject(true);
|
||||
JsonObject propertiesObj = itemsObject.get(BasicConstant.PROPERTIES).getAsJsonObject();
|
||||
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
|
||||
String propertyKey = entry.getKey();
|
||||
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
|
||||
analyzeProperty(propertyConcept, propertyKey, propertyObj, map);
|
||||
}
|
||||
array.add(propertyConcept);
|
||||
|
||||
} else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
|
||||
JSONObject newJsonObj = new JSONObject();
|
||||
analyzeProperty(newJsonObj, propertyName + "_item", itemsObject, map);
|
||||
array.add(newJsonObj.get(propertyName + "_item"));
|
||||
} catch (Exception e) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
|
||||
array.add(value);
|
||||
}
|
||||
} else if (object.has("items") && object.get("items").isJsonArray()) {
|
||||
JsonArray itemsObjectArray = object.get("items").getAsJsonArray();
|
||||
array.add(itemsObjectArray);
|
||||
} else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) {
|
||||
array.add(getValue(itemsObject));
|
||||
} else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) {
|
||||
if (isMock(itemsObject)) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
|
||||
array.add(value);
|
||||
} else {
|
||||
array.add(0);
|
||||
}
|
||||
} else if (itemsObject.has(BasicConstant.PROPERTIES)) {
|
||||
JSONObject propertyConcept = new JSONObject(true);
|
||||
JsonObject propertiesObj = itemsObject.get(BasicConstant.PROPERTIES).getAsJsonObject();
|
||||
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
|
||||
String propertyKey = entry.getKey();
|
||||
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
|
||||
analyzeProperty(propertyConcept, propertyKey, propertyObj, map);
|
||||
}
|
||||
array.add(propertyConcept);
|
||||
|
||||
} else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
|
||||
JSONObject newJsonObj = new JSONObject();
|
||||
analyzeProperty(newJsonObj, propertyName + "_item", itemsObject, map);
|
||||
array.add(newJsonObj.get(propertyName + "_item"));
|
||||
}
|
||||
} else if (object.has("items") && object.get("items").isJsonArray()) {
|
||||
JsonArray itemsObjectArray = object.get("items").getAsJsonArray();
|
||||
array.add(itemsObjectArray);
|
||||
}
|
||||
concept.put(propertyName, array);
|
||||
} else if (propertyObjType.equals(BasicConstant.OBJECT)) {
|
||||
JSONObject obj = new JSONObject();
|
||||
concept.put(propertyName, obj);
|
||||
analyzeObject(object, obj, map);
|
||||
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
|
||||
concept.put(propertyName, null);
|
||||
}
|
||||
concept.put(propertyName, array);
|
||||
} else if (propertyObjType.equals(BasicConstant.OBJECT)) {
|
||||
JSONObject obj = new JSONObject();
|
||||
concept.put(propertyName, obj);
|
||||
analyzeObject(object, obj, map);
|
||||
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
|
||||
concept.put(propertyName, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static Object getValue(JsonObject object) {
|
||||
try {
|
||||
if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
|
||||
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
|
||||
if (isMock(object)) {
|
||||
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
|
||||
return value;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue