Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONPathParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please move this unit test to the test/java/com/alibaba/fastjson2/issues_3900 directory and rename the class to Issue3997? Thanks

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, moved it

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Let's wait for @wenshao to review this, as he handles all write operations.

@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));
}
}