Skip to content

Commit 533d9e9

Browse files
michaelconanmichaelmior
authored andcommitted
fix: update field filter to resolve wildcard path issue
- check data to filter is dictionary type before iterating fields to apply function - add test case and parameters to validate existing examples and identified bug
1 parent 73ed421 commit 533d9e9

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

jsonpath_ng/jsonpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def _update_base(self, data, val, create):
649649
return data
650650

651651
def filter(self, fn, data):
652-
if data is not None:
652+
if data is not None and isinstance(data, dict):
653653
for field in self.reified_fields(DatumInContext.wrap(data)):
654654
if field in data:
655655
if fn(data[field]):

tests/test_jsonpath.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_
188188
assert data_copy2 == expected_value
189189

190190

191+
filter_test_cases = (
192+
# Docs examples
193+
("foo[*].baz", {'foo': [{'baz': 1}, {'baz': 2}]}, lambda d: True, {'foo': [{}, {}]}),
194+
("foo[*].baz", {'foo': [{'baz': 1}, {'baz': 2}]}, lambda d: d == 2, {'foo': [{'baz': 1}, {}]}),
195+
# Wildcard issue fix
196+
("*.baz", {"flag": False, "foo": {"bar": 1, "baz": 2}}, lambda d: True, {"flag": False, "foo": {"bar": 1}}),
197+
)
198+
199+
200+
@pytest.mark.parametrize(
201+
"expression, data, filter_function, expected_value",
202+
filter_test_cases,
203+
)
204+
@parsers
205+
def test_filter(parse: Callable[[str], JSONPath], expression: str, data, filter_function: Callable, expected_value):
206+
data_copy = copy.deepcopy(data)
207+
parse(expression).filter(filter_function, data_copy)
208+
assert data_copy == expected_value
209+
210+
191211
find_test_cases = (
192212
#
193213
# * (star)

0 commit comments

Comments
 (0)