diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONPathParser.java b/core/src/main/java/com/alibaba/fastjson2/JSONPathParser.java index 9a8f0af6cb..2f3b1eb37d 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONPathParser.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONPathParser.java @@ -860,8 +860,10 @@ JSONPathSegment parseFilter() { filterNests--; segment = parseFilterRest(segment); } - if (!jsonReader.nextIfMatch(')')) { - throw new JSONException(jsonReader.info("jsonpath syntax error")); + if (parentheses) { + if (!jsonReader.nextIfMatch(')')) { + throw new JSONException(jsonReader.info("jsonpath syntax error")); + } } return segment; diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3900/Issue3997.java b/core/src/test/java/com/alibaba/fastjson2/issues_3900/Issue3997.java new file mode 100644 index 0000000000..b39a80a4bb --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3900/Issue3997.java @@ -0,0 +1,41 @@ +package com.alibaba.fastjson2.issues_3900; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.JSONPath; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue3997 { + @Test + public void testInFilterAfterOtherFilter() { + JSONArray riskRequests = JSONArray.of( + JSONObject.of("requestRiskType", "05", "customerType", "01", "customerName", "Alice"), + JSONObject.of("requestRiskType", "05", "customerType", "04", "customerName", "Bob"), + JSONObject.of("requestRiskType", "05", "customerType", "02", "customerName", "Carol"), + JSONObject.of("requestRiskType", "06", "customerType", "01", "customerName", "Dave") + ); + JSONObject root = JSONObject.of("riskRequests", riskRequests); + + Object result = JSONPath.of("$.riskRequests[?(@.requestRiskType=='05' && @.customerType in ('01','04'))].customerName") + .eval(root); + assertEquals("[\"Alice\",\"Bob\"]", JSON.toJSONString(result)); + } + + @Test + public void testInFilterBeforeOtherFilter() { + JSONArray riskRequests = JSONArray.of( + JSONObject.of("requestRiskType", "05", "customerType", "01", "customerName", "Alice"), + JSONObject.of("requestRiskType", "05", "customerType", "04", "customerName", "Bob"), + JSONObject.of("requestRiskType", "05", "customerType", "02", "customerName", "Carol"), + JSONObject.of("requestRiskType", "06", "customerType", "01", "customerName", "Dave") + ); + JSONObject root = JSONObject.of("riskRequests", riskRequests); + + Object result = JSONPath.of("$.riskRequests[?(@.customerType in ('01','04') && @.requestRiskType=='05')].customerName") + .eval(root); + assertEquals("[\"Alice\",\"Bob\"]", JSON.toJSONString(result)); + } +}