From c3ad2c1c9d52920991dd3d335d50b7972c8c93c6 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 18 Nov 2021 17:09:05 +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=8Dxml=E6=96=AD=E8=A8=80=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=92=8C=E6=89=A7=E8=A1=8C=E9=97=AE=E9=A2=98=20--bug=3D1008058?= =?UTF-8?q?=20--user=3D=E8=B5=B5=E5=8B=87=20=E3=80=90=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E3=80=91-=E6=8E=A5=E5=8F=A3=E5=AE=9A?= =?UTF-8?q?=E4=B9=89-TEST-=E6=B7=BB=E5=8A=A0=E7=9A=84xml=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E6=96=AD=E8=A8=80=E5=90=8E=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E6=A0=A1=E9=AA=8C=E6=96=AD=E8=A8=80=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E5=8C=B9=E9=85=8D=E9=94=99=E8=AF=AF=20https://www.tap?= =?UTF-8?q?d.cn/55049933/s/1069958=20fix:=20=E9=81=97=E6=86=BE=E7=BC=BA?= =?UTF-8?q?=E9=99=B7issue=20=E7=BB=9F=E8=AE=A1=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=84=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/assertions/MsAssertions.java | 4 +- .../assertions/document/DocumentUtils.java | 4 +- .../commons/json/JSONToDocumentUtils.java | 93 +++++++++++++++---- .../assertion/document/DocumentBody.vue | 14 +-- 4 files changed, 86 insertions(+), 29 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java index 1666392869..bf56db71b5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java @@ -45,7 +45,7 @@ public class MsAssertions extends MsTestElement { private void addAssertions(HashTree hashTree) { // 增加JSON文档结构校验 if (this.getDocument() != null && this.getDocument().getType().equals("JSON")) { - if (StringUtils.isNotEmpty(this.getDocument().getData().getJsonFollowAPI())) { + if (StringUtils.isNotEmpty(this.getDocument().getData().getJsonFollowAPI()) && !this.getDocument().getData().getJsonFollowAPI().equals("false")) { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); this.getDocument().getData().setJson(apiDefinitionService.getDocument(this.getDocument().getData().getJsonFollowAPI(), "JSON")); } @@ -55,7 +55,7 @@ public class MsAssertions extends MsTestElement { } // 增加XML文档结构校验 if (this.getDocument() != null && this.getDocument().getType().equals("XML") && CollectionUtils.isNotEmpty(this.getDocument().getData().getXml())) { - if (StringUtils.isNotEmpty(this.getDocument().getData().getXmlFollowAPI())) { + if (StringUtils.isNotEmpty(this.getDocument().getData().getXmlFollowAPI()) && !this.getDocument().getData().getXmlFollowAPI().equals("false")) { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); this.getDocument().getData().setXml(apiDefinitionService.getDocument(this.getDocument().getData().getXmlFollowAPI(), "XML")); } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentUtils.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentUtils.java index 9c7ac012d0..bb14fc2266 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentUtils.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/document/DocumentUtils.java @@ -38,10 +38,10 @@ public class DocumentUtils { isTrue = getLength(subj, decimalFormatter) != getLength(item.getValue(), decimalFormatter); break; case "length_gt": - isTrue = getLength(subj, decimalFormatter) > getLength(item.getValue(), decimalFormatter); + isTrue = getLength(subj, decimalFormatter) < getLength(item.getValue(), decimalFormatter); break; case "length_lt": - isTrue = getLength(subj, decimalFormatter) < getLength(item.getValue(), decimalFormatter); + isTrue = getLength(subj, decimalFormatter) > getLength(item.getValue(), decimalFormatter); break; case "regular": Pattern pattern = JMeterUtils.getPatternCache().getPattern(expectedValue); diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONToDocumentUtils.java b/backend/src/main/java/io/metersphere/commons/json/JSONToDocumentUtils.java index 974ae72705..1806f29f77 100644 --- a/backend/src/main/java/io/metersphere/commons/json/JSONToDocumentUtils.java +++ b/backend/src/main/java/io/metersphere/commons/json/JSONToDocumentUtils.java @@ -10,11 +10,13 @@ import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; +import org.json.XML; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.LinkedList; import java.util.List; +import java.util.regex.Pattern; public class JSONToDocumentUtils { @@ -27,10 +29,20 @@ public class JSONToDocumentUtils { jsonDataFormatting((JSONObject) value, childrenElements); } else if (value instanceof JSONArray) { List childrenElements = new LinkedList<>(); - children.add(new DocumentElement("", "array", "", childrenElements)); + DocumentElement documentElement = new DocumentElement("", "array", "", childrenElements); + documentElement.setArrayVerification(true); + children.add(documentElement); jsonDataFormatting((JSONArray) value, childrenElements); } else { - children.add(new DocumentElement("", "string", value, null)); + String type = "string"; + if (value != null) { + if (isWholeNumber(value.toString())) { + type = "integer"; + } else if (isNumber(value.toString())) { + type = "number"; + } + } + children.add(new DocumentElement("", type, value, null)); } } } @@ -44,33 +56,58 @@ public class JSONToDocumentUtils { jsonDataFormatting((JSONObject) value, childrenElements); } else if (value instanceof JSONArray) { List childrenElements = new LinkedList<>(); - children.add(new DocumentElement(key, "array", "", childrenElements)); + DocumentElement documentElement = new DocumentElement(key, "array", "", childrenElements); + documentElement.setArrayVerification(true); + children.add(documentElement); jsonDataFormatting((JSONArray) value, childrenElements); } else { - children.add(new DocumentElement(key, "string", value, null)); + String type = "string"; + if (value != null) { + if (isWholeNumber(value.toString())) { + type = "integer"; + } else if (isNumber(value.toString())) { + type = "number"; + } + } + children.add(new DocumentElement(key, type, value, null)); } } } + private static List getJsonDocument(String json, String type) { + List roots = new LinkedList<>(); + List children = new LinkedList<>(); + Object typeObject = new JSONTokener(json).nextValue(); + if (typeObject instanceof net.sf.json.JSONArray) { + if (StringUtils.equals(type, "JSON")) { + roots.add(new DocumentElement().newRoot("array", children)); + JSONArray array = JSON.parseArray(json); + jsonDataFormatting(array, children); + } else { + JSONArray array = JSON.parseArray(json); + jsonDataFormatting(array, roots); + } + } else { + if (StringUtils.equals(type, "JSON")) { + roots.add(new DocumentElement().newRoot("object", children)); + JSONObject object = JSON.parseObject(json); + jsonDataFormatting(object, children); + } else { + JSONObject object = JSON.parseObject(json); + jsonDataFormatting(object, roots); + } + } + return roots; + } public static List getDocument(String json, String type) { try { if (StringUtils.equals(type, "JSON")) { - List roots = new LinkedList<>(); - List children = new LinkedList<>(); - Object typeObject = new JSONTokener(json).nextValue(); - if (typeObject instanceof net.sf.json.JSONArray) { - roots.add(new DocumentElement().newRoot("array", children)); - JSONArray array = JSON.parseArray(json); - jsonDataFormatting(array, children); - } else { - roots.add(new DocumentElement().newRoot("object", children)); - JSONObject object = JSON.parseObject(json); - jsonDataFormatting(object, children); - } - return roots; + return getJsonDocument(json, type); } else if (StringUtils.equals(type, "XML")) { - return getXmlDocument(json); + org.json.JSONObject xmlJSONObj = XML.toJSONObject(json); + String jsonPrettyPrintString = xmlJSONObj.toString(4); + return getJsonDocument(jsonPrettyPrintString, type); } else { return new LinkedList() {{ this.add(new DocumentElement().newRoot("object", null)); @@ -82,6 +119,16 @@ public class JSONToDocumentUtils { } } + public static boolean isNumber(String number) { + Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$"); + return StringUtils.isNotEmpty(number) && pattern.matcher(number).matches(); + } + + public static boolean isWholeNumber(String wholeNumber) { + Pattern pattern = Pattern.compile("[+-]?[0-9]+?"); + return StringUtils.isNotEmpty(wholeNumber) && pattern.matcher(wholeNumber).matches(); + } + /** * 从指定节点开始,递归遍历所有子节点 @@ -90,7 +137,15 @@ public class JSONToDocumentUtils { //递归遍历当前节点所有的子节点 List listElement = node.elements(); if (listElement.isEmpty()) { - children.add(new DocumentElement(node.getName(), "string", node.getTextTrim(), null)); + String type = "string"; + if (StringUtils.isNotEmpty(node.getTextTrim())) { + if (isWholeNumber(node.getText())) { + type = "integer"; + } else if (isNumber(node.getText())) { + type = "number"; + } + } + children.add(new DocumentElement(node.getName(), type, node.getTextTrim(), null)); } for (Element element : listElement) {//遍历所有一级子节点 List elementNodes = element.elements(); diff --git a/frontend/src/business/components/api/definition/components/assertion/document/DocumentBody.vue b/frontend/src/business/components/api/definition/components/assertion/document/DocumentBody.vue index b1f1486d9c..0a35c42a55 100644 --- a/frontend/src/business/components/api/definition/components/assertion/document/DocumentBody.vue +++ b/frontend/src/business/components/api/definition/components/assertion/document/DocumentBody.vue @@ -74,10 +74,10 @@ - + - + @@ -133,18 +133,18 @@ export default { }, created() { if (this.document.type === "JSON") { - this.checked = this.document.data.jsonFollowAPI ? true : false; + this.checked = this.document.data.jsonFollowAPI && this.document.data.jsonFollowAPI !== "false" ? true : false; } else if (this.document.type === "XML") { - this.checked = this.document.data.xmlFollowAPI ? true : false; + this.checked = this.document.data.xmlFollowAPI && this.document.data.xmlFollowAPI !== "false" ? true : false; } this.changeData(); }, watch: { 'document.type'() { if (this.document.type === "JSON") { - this.checked = this.document.data.jsonFollowAPI ? true : false; + this.checked = this.document.data.jsonFollowAPI && this.document.data.jsonFollowAPI !== "false" ? true : false; } else if (this.document.type === "XML") { - this.checked = this.document.data.xmlFollowAPI ? true : false; + this.checked = this.document.data.xmlFollowAPI && this.document.data.xmlFollowAPI !== "false" ? true : false; } this.changeData(); } @@ -162,6 +162,8 @@ export default { } }, checkedAPI() { + this.document.data.jsonFollowAPI = ""; + this.document.data.xmlFollowAPI = ""; this.changeData(); }, reload() {