Skip to content

Commit 23c0f84

Browse files
committed
Fix infix operators and filter expression literals
1 parent c28196a commit 23c0f84

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
**Fixes**
66

7-
- Fixed handling of JSONPath literals in filter expressions. We now raise a `JSONPathSyntaxError` if a filter expression literal is not part of a comparison or function expression. See [jsonpath-compliance-test-suite#81](https://github.com/jsonpath-standard/jsonpath-compliance-test-suite/pull/81).
7+
- Fixed handling of JSONPath literals in filter expressions. We now raise a `JSONPathSyntaxError` if a filter expression literal is not part of a comparison, membership or function expression. See [jsonpath-compliance-test-suite#81](https://github.com/jsonpath-standard/jsonpath-compliance-test-suite/pull/81).
88

99
**Features**
1010

jsonpath/parse.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ class Parser:
197197
]
198198
)
199199

200+
# Infix operators that accept filter expression literals.
201+
INFIX_LITERAL_OPERATORS = frozenset(
202+
[
203+
"==",
204+
">=",
205+
">",
206+
"<=",
207+
"<",
208+
"!=",
209+
"<>",
210+
"=~",
211+
"in",
212+
"contains",
213+
]
214+
)
215+
200216
PREFIX_OPERATORS = frozenset(
201217
[
202218
TOKEN_NOT,
@@ -530,14 +546,14 @@ def parse_infix_expression(
530546
self._raise_for_non_comparable_function(left, tok)
531547
self._raise_for_non_comparable_function(right, tok)
532548

533-
if operator not in self.COMPARISON_OPERATORS:
534-
if isinstance(left, Literal):
549+
if operator not in self.INFIX_LITERAL_OPERATORS:
550+
if isinstance(left, (Literal, Nil)):
535551
raise JSONPathSyntaxError(
536552
"filter expression literals outside of "
537553
"function expressions must be compared",
538554
token=tok,
539555
)
540-
if isinstance(right, Literal):
556+
if isinstance(right, (Literal, Nil)):
541557
raise JSONPathSyntaxError(
542558
"filter expression literals outside of "
543559
"function expressions must be compared",

tests/test_find.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ class Case:
8787
data=[{"a": True, "b": False}],
8888
want=[{"a": True, "b": False}],
8989
),
90+
Case(
91+
description="array contains literal",
92+
path="$[[email protected] contains 'foo']",
93+
data=[{"a": ["foo", "bar"]}, {"a": ["bar"]}],
94+
want=[
95+
{
96+
"a": ["foo", "bar"],
97+
}
98+
],
99+
),
100+
Case(
101+
description="literal in array",
102+
path="$[?'foo' in @.a]",
103+
data=[{"a": ["foo", "bar"]}, {"a": ["bar"]}],
104+
want=[
105+
{
106+
"a": ["foo", "bar"],
107+
}
108+
],
109+
),
90110
]
91111

92112

0 commit comments

Comments
 (0)