From 1f9d187f17f433affc54cd87680b9127c5ec62a2 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 26 Oct 2021 11:12:48 +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=E7=94=9F=E6=88=90=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98=20--bug?= =?UTF-8?q?=3D1007472=20--user=3D=E8=B5=B5=E5=8B=87=20=E3=80=90=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=B5=8B=E8=AF=95=E3=80=91=E6=8E=A5=E5=8F=A3=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=EF=BC=8C=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E9=94=99=E8=AF=AF=20https://www.tapd.cn/5504?= =?UTF-8?q?9933/s/1058985?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/json/JSONSchemaGenerator.java | 132 ++++-------------- .../scenario/common/CustomizeReqInfo.vue | 22 ++- 2 files changed, 45 insertions(+), 109 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java index 94e6995ae3..1ce3f8b758 100644 --- a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java +++ b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java @@ -31,34 +31,17 @@ public class JSONSchemaGenerator { if (rootElement.has("type") || rootElement.has("allOf")) { analyzeObject(rootElement, rootObj); } - if (rootElement.has("definitions")) { - // Section 9 in json-validation analyzeDefinitions(rootElement); } } private static void analyzeObject(JsonObject object, JSONObject rootObj) { - // Creating the concept - - if (object.has("title")) { - // 暂不处理,后续使用时再加 - String title = object.get("title").getAsString(); - } - - if (object.has("description")) { - // 暂不处理,后续使用时再加 - String description = object.get("description").getAsString(); - } - if (object.has("allOf")) { JsonArray allOfArray = object.get("allOf").getAsJsonArray(); for (JsonElement allOfElement : allOfArray) { JsonObject allOfElementObj = allOfElement.getAsJsonObject(); - if (allOfElementObj.has("$ref")) { - // 暂不处理,后续使用时再加 - String ref = allOfElementObj.get("$ref").getAsString(); - } else if (allOfElementObj.has("properties")) { + if (allOfElementObj.has("properties")) { // Properties elements will become the attributes/references of the element JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject(); for (Entry entry : propertiesObj.entrySet()) { @@ -68,8 +51,6 @@ public class JSONSchemaGenerator { } } } - } else if (object.has("oneOf")) { - // 暂不处理,后续使用时再加 } else if (object.has("properties")) { JsonObject propertiesObj = object.get("properties").getAsJsonObject(); for (Entry entry : propertiesObj.entrySet()) { @@ -82,10 +63,6 @@ public class JSONSchemaGenerator { } else if (object.has("type") && !object.get("type").getAsString().equals("object")) { analyzeProperty(rootObj, object.getAsString(), object); } - - if (object.has("required")) { - // 必选项暂不处理,后续使用时再加 - } } private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) { @@ -102,11 +79,23 @@ public class JSONSchemaGenerator { } } } - - if(object.has("default")){ + if (object.has("default")) { concept.put(propertyName, object.get("default")); - }else if (object.has("enum")) { - concept.put(propertyName, analyzeEnumProperty(object)); + } else if (object.has("enum")) { + try { + if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { + Object value = object.get("mock").getAsJsonObject().get("mock"); + concept.put(propertyName, value); + } else { + List list = analyzeEnumProperty(object); + if (list.size() > 0) { + int index = (int) (Math.random() * list.size()); + concept.put(propertyName, list.get(index)); + } + } + } catch (Exception e) { + concept.put(propertyName, ""); + } } else if (propertyObjType.equals("string")) { // 先设置空值 concept.put(propertyName, ""); @@ -123,21 +112,6 @@ public class JSONSchemaGenerator { String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); concept.put(propertyName, value); } - if (object.has("maxLength")) { - - } - // Section 6.3.1 in json-schema-validation. Resolved as OCL - - if (object.has("minLength")) { - - } - // Section 6.3.2 in json-schema-validation. Resolved as OCL - - if (object.has("pattern")) { - // Section 6.3.3 in json-schema-validation. Resolved as OCL, possible? - // TODO 6.3.3 in json-schema-validation - } - } else if (propertyObjType.equals("integer")) { // 先设置空值 concept.put(propertyName, 0); @@ -153,30 +127,6 @@ public class JSONSchemaGenerator { concept.put(propertyName, value); } } - if (object.has("multipleOf")) { - - } - // Section 6.2.1 in json-schema-validation. Resolved as OCL - - if (object.has("maximum")) { - - } - // Section 6.2.2 in json-schema-validation. Resolved as OCL - - if (object.has("exclusiveMaximum")) { - - } - // Section 6.2.3 in json-schema-validation. Resolved as OCL - - if (object.has("minimum")) { - - } - // Section 6.2.4 in json-schema-validation. Resolved as OCL - - if (object.has("exclusiveMinimum")) { - - } - // Section 6.2.5 in json-schema-validation. Resolved as OCL } else if (propertyObjType.equals("number")) { // 先设置空值 @@ -215,13 +165,12 @@ public class JSONSchemaGenerator { if (object.has("items") && object.get("items").isJsonArray()) { jsonArray = object.get("items").getAsJsonArray(); } else { - JsonObject itemsObject = itemsObject = object.get("items").getAsJsonObject(); + JsonObject itemsObject = object.get("items").getAsJsonObject(); array.add(itemsObject); } - for(int i = 0; i < jsonArray.size(); i ++){ + 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 { @@ -231,8 +180,8 @@ public class JSONSchemaGenerator { String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); array.add(value); } - }else if (itemsObject.has("enum")) { -// array.add(analyzeEnumProperty(itemsObject)); + } else if (itemsObject.has("enum")) { + array.add(analyzeEnumProperty(itemsObject)); } else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("string")) { if (itemsObject.has("default")) { array.add(itemsObject.get("default")); @@ -251,12 +200,6 @@ public class JSONSchemaGenerator { } else { array.add(0); } - } else if (itemsObject.has("oneOf")) { - - } else if (itemsObject.has("anyOf")) { - - } else if (itemsObject.has("allOf")) { - // TODO } else if (itemsObject.has("properties")) { JSONObject propertyConcept = new JSONObject(); JsonObject propertiesObj = itemsObject.get("properties").getAsJsonObject(); @@ -267,12 +210,10 @@ public class JSONSchemaGenerator { } array.add(propertyConcept); - } else if (itemsObject.has("$ref")) { - analyzeRef(concept, propertyName, itemsObject); - }else if(itemsObject.has("type") && itemsObject.get("type") instanceof JsonPrimitive){ + } 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")); + 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(); @@ -287,45 +228,30 @@ public class JSONSchemaGenerator { concept.put(propertyName, obj); analyzeObject(object, obj); } - } else if (object.has("$ref")) { - analyzeRef(concept, propertyName, object); - } else if (object.has("oneOf")) { - // Section 6.7.3 in json-schema-validation - - } else if (object.has("anyOf")) { - // Section 6.7.2 in json-schema-validation - } } private static List analyzeEnumProperty(JsonObject object) { List list = new LinkedList<>(); String jsonStr = null; - JsonArray enumValues = null; try { - enumValues = object.get("enum").getAsJsonArray(); + JsonArray enumValues = object.get("enum").getAsJsonArray(); for (JsonElement enumValueElem : enumValues) { String enumValue = enumValueElem.getAsString(); list.add(enumValue); } - }catch (Exception e){ + } catch (Exception e) { jsonStr = object.get("enum").getAsString(); } - - if(jsonStr != null && list.isEmpty()){ - String [] arrs = jsonStr.split("\n"); - for (String str: arrs) { + if (jsonStr != null && list.isEmpty()) { + String[] arrays = jsonStr.split("\n"); + for (String str : arrays) { list.add(str); } } - return list; } - private static void analyzeRef(JSONObject concept, String propertyName, JsonObject object) { - String ref = object.get("$ref").getAsString(); - } - private static void analyzeDefinitions(JsonObject object) { JsonObject definitionsObj = object.get("definitions").getAsJsonObject(); for (Entry entry : definitionsObj.entrySet()) { diff --git a/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue b/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue index 5651c588d3..24259f12a2 100644 --- a/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue +++ b/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue @@ -2,24 +2,24 @@
- + - + + style="width: 50%" size="small" @blur="urlChange" :disabled="request.disabled"> {{ $t('api_test.request.refer_to_environment') }}
- + - + + v-model="request.path" size="small" @blur="pathChange" :disabled="request.disabled"/>
@@ -143,6 +143,16 @@ export default { width: 120px; } +.ms-select { + width: 100px; + margin-right: 10px; +} + +.ms-input { + width: 150px; + margin-right: 10px; +} + .is-ref-environment { margin-left: 15px; }