From 8dd321309799d937c613fc915fc66b55f323d230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Flar=20Eker?= Date: Sat, 28 Feb 2026 13:20:03 +0300 Subject: [PATCH 1/2] Fix JSONPath syntax error with in filter after other filters When parsing filter expressions, the parser was unconditionally requiring a closing parenthesis even when the filter wasn't wrapped in parentheses. This caused errors when an 'in' filter followed other filter conditions, since the 'in' filter has its own parenthetical syntax. Only enforce the closing parenthesis requirement when the filter itself is wrapped in parentheses. --- .../com/alibaba/fastjson2/JSONPathParser.java | 6 ++- .../JSONPath_in_filter_issue3997.java | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPath_in_filter_issue3997.java 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/jsonpath/JSONPath_in_filter_issue3997.java b/core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPath_in_filter_issue3997.java new file mode 100644 index 0000000000..8497588698 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPath_in_filter_issue3997.java @@ -0,0 +1,41 @@ +package com.alibaba.fastjson2.jsonpath; + +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 JSONPath_in_filter_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)); + } +} From ed4d084ae6c6801cc4f2297e09c73547c3ad4e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Flar=20Eker?= Date: Sat, 28 Feb 2026 14:34:33 +0300 Subject: [PATCH 2/2] Move test to issues_3900 directory and rename to Issue3997 --- .../Issue3997.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename core/src/test/java/com/alibaba/fastjson2/{jsonpath/JSONPath_in_filter_issue3997.java => issues_3900/Issue3997.java} (95%) diff --git a/core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPath_in_filter_issue3997.java b/core/src/test/java/com/alibaba/fastjson2/issues_3900/Issue3997.java similarity index 95% rename from core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPath_in_filter_issue3997.java rename to core/src/test/java/com/alibaba/fastjson2/issues_3900/Issue3997.java index 8497588698..b39a80a4bb 100644 --- a/core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPath_in_filter_issue3997.java +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3900/Issue3997.java @@ -1,4 +1,4 @@ -package com.alibaba.fastjson2.jsonpath; +package com.alibaba.fastjson2.issues_3900; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; @@ -8,7 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -public class JSONPath_in_filter_issue3997 { +public class Issue3997 { @Test public void testInFilterAfterOtherFilter() { JSONArray riskRequests = JSONArray.of(