Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
- '2.5'
- '2.6'
- '2.7'
- '3.2'
- '3.1'
- '3.0'
- ruby-head
- jruby-head
- truffleruby-head
Expand Down
5 changes: 3 additions & 2 deletions lib/jsonpath/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ def process_function_or_literal(exp, default = nil)
return Integer(exp) if exp[0] != '('
return nil unless @_current_node

identifiers = /@?((?<!\d)\.(?!\d)(\w+))+/.match(exp)
if !identifiers.nil? && !@_current_node.methods.include?(identifiers[2].to_sym)
identifiers = /@?(((?<!\d)\.(?!\d)(\w+))|\['(.*?)'\])+/.match(exp)
# to filter arrays with known/unknown name.
if (!identifiers.nil? && !(@_current_node.methods.include?(identifiers[2]&.to_sym) || @_current_node.methods.include?(identifiers[4]&.to_sym)))
exp_to_eval = exp.dup
begin
return JsonPath::Parser.new(@_current_node, @options).parse(exp_to_eval)
Expand Down
14 changes: 9 additions & 5 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,13 +885,17 @@ def test_complex_nested_grouping
path = "$..book[?((@['author'] == 'Evelyn Waugh' || @['author'] == 'Herman Melville') && (@['price'] == 33 || @['price'] == 9))]"
assert_equal [@object['store']['book'][2]], JsonPath.new(path).on(@object)
end

def test_complex_nested_grouping_unmatched_parent
path = "$..book[?((@['author'] == 'Evelyn Waugh' || @['author'] == 'Herman Melville' && (@['price'] == 33 || @['price'] == 9))]"
err = assert_raises(ArgumentError, 'should have raised an exception') { JsonPath.new(path).on(@object) }
assert_match(/unmatched parenthesis in expression: \(\(false \|\| false && \(false \|\| true\)\)/, err.message)

def test_nested_with_unknown_key
path = "$..[?(@.price == 9 || @.price == 33)].title"
assert_equal ["Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick"], JsonPath.new(path).on(@object)
end

def test_nested_with_unknown_key_filtered_array
path = "$..[?(@['price'] == 9 || @['price'] == 33)].title"
assert_equal ["Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick"], JsonPath.new(path).on(@object)
end

def test_runtime_error_frozen_string
skip('in ruby version below 2.2.0 this error is not raised') if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.0') || Gem::Version.new(RUBY_VERSION) > Gem::Version::new('2.6')
json = '
Expand Down