Skip to content

Commit

Permalink
Merge pull request #159 from mohanapriya2308/master
Browse files Browse the repository at this point in the history
To filter elements of an array with unknown name / Add ruby versions for test cases
  • Loading branch information
joshbuddy committed Sep 27, 2023
2 parents 4e31ac9 + c16dc73 commit db362cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
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

0 comments on commit db362cd

Please sign in to comment.