From 344779e02e5beff0841f31c8a6b96e7f54dfaf67 Mon Sep 17 00:00:00 2001 From: Evan Salter Date: Wed, 22 Feb 2023 09:20:04 -0600 Subject: [PATCH 1/2] Fix for negating a filter script --- lib/jsonpath/parser.rb | 1 + test/test_jsonpath.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/jsonpath/parser.rb b/lib/jsonpath/parser.rb index 84e0c47..94a28d1 100644 --- a/lib/jsonpath/parser.rb +++ b/lib/jsonpath/parser.rb @@ -99,6 +99,7 @@ def parse_exp(exp) elements.inject(@_current_node, &:__send__) end + return !el if operator&.strip == '!' && operand.nil? return (el ? true : false) if el.nil? || operator.nil? el = Float(el) rescue el diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 91e7026..1a5a6e0 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -67,6 +67,7 @@ def test_recognize_array_comma def test_recognize_filters assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new("$..book[?(@['isbn'])]").on(@object) + assert_equal [@object['store']['book'][0], @object['store']['book'][1], @object['store']['book'][4], @object['store']['book'][5], @object['store']['book'][6]], JsonPath.new("$..book[?(!@['isbn'])]").on(@object) assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] < 10)]").on(@object) assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] == 9)]").on(@object) assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] > 20)]").on(@object) @@ -157,6 +158,7 @@ def test_paths_with_numbers def test_recognized_dot_notation_in_filters assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[?(@.isbn)]').on(@object) + assert_equal [@object['store']['book'][0], @object['store']['book'][1], @object['store']['book'][4], @object['store']['book'][5], @object['store']['book'][6]], JsonPath.new("$..book[?(!@.isbn)]").on(@object) end def test_works_on_non_hash From 549d1419125fba1885ac71385ccfbcea03b53ab2 Mon Sep 17 00:00:00 2001 From: Evan Salter Date: Wed, 22 Feb 2023 09:28:56 -0600 Subject: [PATCH 2/2] Bump version --- lib/jsonpath/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jsonpath/version.rb b/lib/jsonpath/version.rb index 4b01dd4..784eaf7 100644 --- a/lib/jsonpath/version.rb +++ b/lib/jsonpath/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class JsonPath - VERSION = '1.1.2' + VERSION = '1.1.3' end