From b4801f4ba062502e332da17cde2f086d85491791 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 24 Nov 2021 11:15:25 +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=8Djson=E6=96=AD=E8=A8=80=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1008219 --user=赵勇 【接口测试】文档结构断言,json结构,数组值没校验 https://www.tapd.cn/55049933/s/1073920 --- .../request/assertions/document/Document.java | 22 +++++++++++++++++-- .../assertions/document/DocumentElement.java | 21 +++++++++++++++++- .../json/JSONSchemaToDocumentUtils.java | 6 ++--- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/Document.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/Document.java index 0f6bef997f..c8b02b3e69 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/Document.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/Document.java @@ -81,7 +81,16 @@ public class Document { if (StringUtils.isEmpty(item.getGroupId())) { if (!item.getId().equals("root")) { if (parentNode != null) { - item.setJsonPath(parentNode.getJsonPath() + "." + item.getName()); + if (parentNode.getType().equals("array") && item.getType().equals("string")) { + try { + int index = StringUtils.isNotEmpty(item.getName()) ? Integer.parseInt(item.getName()) : 0; + item.setJsonPath(parentNode.getJsonPath() + "[" + index + "]"); + } catch (Exception e) { + item.setJsonPath(parentNode.getJsonPath() + "." + item.getName()); + } + } else { + item.setJsonPath(parentNode.getJsonPath() + "." + item.getName()); + } } else { item.setJsonPath("$." + item.getName()); } @@ -107,7 +116,16 @@ public class Document { for (DocumentElement item : dataList) { if (StringUtils.isEmpty(item.getGroupId())) { if (parentNode != null) { - item.setJsonPath(parentNode.getJsonPath() + "." + item.getName()); + if (StringUtils.equals(parentNode.getType(), "array") && item.getType().equals("string")) { + try { + int index = StringUtils.isNotEmpty(item.getName()) ? Integer.parseInt(item.getName()) : 0; + item.setJsonPath(parentNode.getJsonPath() + "[" + index + "]"); + } catch (Exception e) { + item.setJsonPath(parentNode.getJsonPath() + "." + item.getName()); + } + } else { + item.setJsonPath(parentNode.getJsonPath() + "." + item.getName()); + } } else { item.setJsonPath("$." + item.getName()); } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentElement.java index 0f6a4ec0ba..92e2dc85bd 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentElement.java @@ -46,7 +46,7 @@ public class DocumentElement { } } - public DocumentElement(String name, String type, Object expectedOutcome, boolean typeVerification, Listchildren) { + public DocumentElement(String name, String type, Object expectedOutcome, boolean typeVerification, List children) { this.id = UUID.randomUUID().toString(); this.name = name; this.expectedOutcome = expectedOutcome; @@ -62,6 +62,25 @@ public class DocumentElement { } } + + public DocumentElement(String name, String type, Object expectedOutcome, boolean typeVerification, boolean arrayVerification, List children) { + this.id = UUID.randomUUID().toString(); + this.name = name; + this.expectedOutcome = expectedOutcome; + this.type = type; + this.typeVerification = typeVerification; + this.arrayVerification = arrayVerification; + this.children = children == null ? this.children = new LinkedList<>() : children; + this.rowspan = 1; + this.contentVerification = "value_eq"; + if (StringUtils.equalsAny(type, "object", "array")) { + this.contentVerification = "none"; + } else if (expectedOutcome == null || StringUtils.isEmpty(expectedOutcome.toString())) { + this.contentVerification = "none"; + } + } + + public DocumentElement(String id, String name, String type, Object expectedOutcome, List children) { this.id = id; this.name = name; diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaToDocumentUtils.java b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaToDocumentUtils.java index a80d424cd1..985e7bfc57 100644 --- a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaToDocumentUtils.java +++ b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaToDocumentUtils.java @@ -74,7 +74,7 @@ public class JSONSchemaToDocumentUtils { Object value = null; boolean required = requiredList.contains(propertyName); if (object.has("default")) { - value = object.get("default") != null ? object.get("default").toString() : ""; + value = object.get("default") != null ? object.get("default").getAsString() : ""; concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); } else if (object.has("enum")) { try { @@ -97,7 +97,7 @@ public class JSONSchemaToDocumentUtils { } else if (propertyObjType.equals("string")) { // 先设置空值 if (object.has("default")) { - value = object.get("default") != null ? object.get("default").toString() : ""; + value = object.get("default") != null ? object.get("default").getAsString() : ""; } 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())) { value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); @@ -140,7 +140,7 @@ public class JSONSchemaToDocumentUtils { concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); } else if (propertyObjType.equals("array")) { List elements = new LinkedList<>(); - concept.add(new DocumentElement(propertyName, propertyObjType, "", requiredList.contains(propertyName), elements)); + concept.add(new DocumentElement(propertyName, propertyObjType, "", requiredList.contains(propertyName), true, elements)); JsonArray jsonArray = object.get("items").getAsJsonArray(); analyzeArray(propertyName, jsonArray, elements, requiredList); } else if (propertyObjType.equals("object")) {