From 744de357c3ae529fc942fbf35603d6cd7313860e Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Mon, 23 Nov 2020 12:08:30 +0800 Subject: [PATCH] =?UTF-8?q?fit(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20js?= =?UTF-8?q?onpath=20=E6=96=AD=E8=A8=80=E6=8E=A8=E8=8D=90=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=BD=AC=E4=B9=89=E6=AD=A3=E5=88=99=E7=89=B9=E6=AE=8A=E5=AD=97?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/JsonPathUtils.java | 18 ++++++++++++++++++ .../components/assertion/ApiAssertions.vue | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java b/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java index 6109d7fd60..d978056402 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java @@ -12,6 +12,7 @@ import java.util.stream.Collectors; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONPath; +import org.apache.commons.lang3.StringUtils; public class JsonPathUtils { @@ -68,9 +69,26 @@ public class JsonPathUtils { .compareTo( (String)b.get("json_path") ) ); + // 正则特殊字符转义 + allJsons.forEach(item -> { + item.put("regular_expression", escapeExprSpecialWord((String) item.get("json_value"))); + }); + return allJsons; } + public static String escapeExprSpecialWord(String keyword) { + if (StringUtils.isNotBlank(keyword)) { + String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"}; + for (String key : fbsArr) { + if (keyword.contains(key)) { + keyword = keyword.replace(key, "\\" + key); + } + } + } + return keyword; + } + private static String formatJson(String json_path){ String ret=""; String reg = ".(\\d{1,3}).{0,1}"; diff --git a/frontend/src/business/components/api/test/components/assertion/ApiAssertions.vue b/frontend/src/business/components/api/test/components/assertion/ApiAssertions.vue index c301aec0f5..ab9cb388f0 100644 --- a/frontend/src/business/components/api/test/components/assertion/ApiAssertions.vue +++ b/frontend/src/business/components/api/test/components/assertion/ApiAssertions.vue @@ -107,7 +107,7 @@ export default { jsonPathList.forEach(jsonPath => { let jsonItem = new JSONPath(); jsonItem.expression = jsonPath.json_path; - jsonItem.expect = jsonPath.json_value; + jsonItem.expect = jsonPath.regular_expression; jsonItem.setJSONPathDescription(); this.assertions.jsonPath.push(jsonItem); });