From 4e80e2485f8a3264b3580c22b4a4afd16b4f5d6e Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 24 Nov 2021 18:18:34 +0800 Subject: [PATCH] =?UTF-8?q?fix=20(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE=E4=BA=86=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=BB=98=E8=AE=A4=E5=80=BC=E5=90=8E=EF=BC=8Ctest?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E6=97=A0=E6=B3=95=E4=BF=AE=E6=94=B9=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1008159 --user=赵勇 【接口测试】接口定义,设置了参数默认值后,test执行的时候无法修改 https://www.tapd.cn/55049933/s/1074611 --- .../io/metersphere/api/dto/scenario/Body.java | 4 +- .../commons/json/JSONSchemaRunTest.java | 229 ++++++++++++++++++ 2 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java b/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java index 13e67872de..9ba783caa6 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java @@ -3,7 +3,7 @@ package io.metersphere.api.dto.scenario; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import io.metersphere.api.dto.scenario.request.BodyFile; -import io.metersphere.commons.json.JSONSchemaGenerator; +import io.metersphere.commons.json.JSONSchemaRunTest; import io.metersphere.commons.utils.FileUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils; import lombok.Data; @@ -87,7 +87,7 @@ public class Body { if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, "JSON")) { if(StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null && "JSON-SCHEMA".equals(this.format)) { - this.raw = JSONSchemaGenerator.getJson(com.alibaba.fastjson.JSON.toJSONString(this.getJsonSchema())); + this.raw = JSONSchemaRunTest.getJson(com.alibaba.fastjson.JSON.toJSONString(this.getJsonSchema())); } else { // json 文本也支持 mock 参数 try { JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(this.getRaw()); diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java new file mode 100644 index 0000000000..25cfc4e82f --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java @@ -0,0 +1,229 @@ +package io.metersphere.commons.json; + +import com.alibaba.fastjson.JSONObject; +import com.google.gson.*; +import io.metersphere.commons.utils.LogUtil; +import io.metersphere.jmeter.utils.ScriptEngineUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.LinkedList; +import java.util.List; +import java.util.Map.Entry; + +public class JSONSchemaRunTest { + + private static void generator(String json, JSONObject obj) { + analyzeSchema(json, obj); + } + + private static void analyzeSchema(String json, JSONObject rootObj) { + try { + Gson gson = new Gson(); + JsonElement element = gson.fromJson(json, JsonElement.class); + JsonObject rootElement = element.getAsJsonObject(); + analyzeRootSchemaElement(rootElement, rootObj); + } catch (Exception e) { + LogUtil.error(e); + } + + } + + private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) { + if (rootElement.has("type") || rootElement.has("allOf")) { + analyzeObject(rootElement, rootObj); + } + if (rootElement.has("definitions")) { + analyzeDefinitions(rootElement); + } + } + + private static void analyzeObject(JsonObject object, JSONObject rootObj) { + if (object.has("allOf")) { + JsonArray allOfArray = object.get("allOf").getAsJsonArray(); + for (JsonElement allOfElement : allOfArray) { + JsonObject allOfElementObj = allOfElement.getAsJsonObject(); + if (allOfElementObj.has("properties")) { + JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject(); + for (Entry entry : propertiesObj.entrySet()) { + String propertyKey = entry.getKey(); + JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); + analyzeProperty(rootObj, propertyKey, propertyObj); + } + } + } + } else if (object.has("properties")) { + JsonObject propertiesObj = object.get("properties").getAsJsonObject(); + for (Entry entry : propertiesObj.entrySet()) { + String propertyKey = entry.getKey(); + JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); + analyzeProperty(rootObj, propertyKey, propertyObj); + } + } else if (object.has("type") && object.get("type").getAsString().equals("array")) { + analyzeProperty(rootObj, "MS-OBJECT", object); + } else if (object.has("type") && !object.get("type").getAsString().equals("object")) { + analyzeProperty(rootObj, object.getAsString(), object); + } + } + + private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) { + if (object.has("type")) { + String propertyObjType = null; + if (object.get("type") instanceof JsonPrimitive) { + propertyObjType = object.get("type").getAsString(); + } else if (object.get("type") instanceof JsonArray) { + JsonArray typeArray = (JsonArray) object.get("type").getAsJsonArray(); + propertyObjType = typeArray.get(0).getAsString(); + if (typeArray.size() > 1) { + if (typeArray.get(1).getAsString().equals("null")) { + // 暂不处理,后续使用时再加 + } + } + } + if (propertyObjType.equals("string") || propertyObjType.equals("enum")) { + concept.put(propertyName, getValue(object)); + } else if (propertyObjType.equals("integer") || propertyObjType.equals("number")) { + // 先设置空值 + concept.put(propertyName, 0); + try { + if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { + Number value = object.get("mock").getAsJsonObject().get("mock").getAsNumber(); + if (value.toString().indexOf(".") == -1) { + concept.put(propertyName, value.intValue()); + } else { + concept.put(propertyName, value.floatValue()); + } + } + } catch (Exception e) { + String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); + concept.put(propertyName, value); + } + } else if (propertyObjType.equals("boolean")) { + // 先设置空值 + concept.put(propertyName, false); + try { + if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { + String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").toString()); + if (StringUtils.isNotEmpty(value)) { + if (value.indexOf("\"") != -1) { + value = value.replaceAll("\"", ""); + } + concept.put(propertyName, Boolean.valueOf(value)); + } + } + } catch (Exception e) { + concept.put(propertyName, false); + } + } else if (propertyObjType.equals("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("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { + try { + int value = itemsObject.get("mock").getAsJsonObject().get("mock").getAsInt(); + array.add(value); + } catch (Exception e) { + String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); + array.add(value); + } + } else if (itemsObject.has("type") && (itemsObject.has("enum") || itemsObject.get("type").getAsString().equals("string"))) { + array.add(getValue(itemsObject)); + } else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("number")) { + if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { + String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); + array.add(value); + } else { + array.add(0); + } + } else if (itemsObject.has("properties")) { + JSONObject propertyConcept = new JSONObject(true); + JsonObject propertiesObj = itemsObject.get("properties").getAsJsonObject(); + for (Entry entry : propertiesObj.entrySet()) { + String propertyKey = entry.getKey(); + JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); + analyzeProperty(propertyConcept, propertyKey, propertyObj); + } + array.add(propertyConcept); + + } else if (itemsObject.has("type") && itemsObject.get("type") instanceof JsonPrimitive) { + JSONObject newJsonObj = new JSONObject(); + analyzeProperty(newJsonObj, propertyName + "_item", itemsObject); + 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("object")) { + JSONObject obj = new JSONObject(); + concept.put(propertyName, obj); + analyzeObject(object, obj); + } else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) { + concept.put(propertyName, null); + } + } + } + + private static Object getValue(JsonObject object) { + try { + if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString()) && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { + String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); + return value; + } + } catch (Exception e) { + return object.get("mock").getAsJsonObject().get("mock"); + } + return ""; + } + + private static void analyzeDefinitions(JsonObject object) { + JsonObject definitionsObj = object.get("definitions").getAsJsonObject(); + for (Entry entry : definitionsObj.entrySet()) { + String definitionKey = entry.getKey(); + JsonObject definitionObj = definitionsObj.get(definitionKey).getAsJsonObject(); + JSONObject obj = new JSONObject(); + analyzeRootSchemaElement(definitionObj, obj); + } + } + + + private static String formerJson(String jsonSchema) { + try { + JSONObject root = new JSONObject(true); + generator(jsonSchema, root); + // 格式化返回 + Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create(); + if (root.get("MS-OBJECT") != null) { + return gson.toJson(root.get("MS-OBJECT")); + } + return gson.toJson(root); + } catch (Exception e) { + return jsonSchema; + } + } + + public static String getJson(String jsonSchema) { + try { + if (StringUtils.isEmpty(jsonSchema)) { + return null; + } + return formerJson(jsonSchema); + } catch (Exception ex) { + return jsonSchema; + } + } +}