From 043ca67d8689e44536854fb4e4076e26f320ca0e Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sat, 2 Apr 2022 16:17:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dschema=E7=B1=BB=E5=9E=8B=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=8F=96=E5=80=BC=E7=B1=BB=E5=9E=8B=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1011864 --user=赵勇 【接口测试】github#12179,开启json-schema后,POST接口JSON数组里面的字符串类型错误 https://www.tapd.cn/55049933/s/1129586 --- .../commons/json/JSONSchemaRunTest.java | 208 +++++++++--------- 1 file changed, 110 insertions(+), 98 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java index fb620dbd32..cdf44c6461 100644 --- a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java +++ b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java @@ -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 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 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 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 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 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 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; }