Skip to content

Commit

Permalink
Merge pull request #160 from anupama-kumari/ak_nesting_too_deep
Browse files Browse the repository at this point in the history
Fix issue #141: nesting of 101 too deep
  • Loading branch information
joshbuddy committed Sep 27, 2023
2 parents db362cd + 50d0bbf commit 2a1fe15
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
# into a token array.
class JsonPath
PATH_ALL = '$..*'
MAX_NESTING_ALLOWED = 100

DEFAULT_OPTIONS = {
:default_path_leaf_to_null => false,
:symbolize_keys => false,
:use_symbols => false,
:allow_send => true,
:max_nesting => 100
:max_nesting => MAX_NESTING_ALLOWED
}

attr_accessor :path

def initialize(path, opts = {})
@opts = DEFAULT_OPTIONS.merge(opts)
set_max_nesting
scanner = StringScanner.new(path.strip)
@path = []
until scanner.eos?
Expand Down Expand Up @@ -146,4 +148,9 @@ def self.process_object(obj_or_str, opts = {})
def deep_clone
Marshal.load Marshal.dump(self)
end

def set_max_nesting
return unless @opts[:max_nesting].is_a?(Integer) && @opts[:max_nesting] > MAX_NESTING_ALLOWED
@opts[:max_nesting] = false
end
end
25 changes: 25 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,31 @@ def test_with_max_nesting_false
assert_equal [{}], JsonPath.new('$.a.b.c', max_nesting: false).on(json)
end

def test_initialize_with_max_nesting_exceeding_limit
json = {
a: {
b: {
c: {
}
}
}
}.to_json

json_obj = JsonPath.new('$.a.b.c', max_nesting: 105)
assert_equal [{}], json_obj.on(json)
assert_equal false, json_obj.instance_variable_get(:@opts)[:max_nesting]
end

def test_initialize_without_max_nesting_exceeding_limit
json_obj = JsonPath.new('$.a.b.c', max_nesting: 90)
assert_equal 90, json_obj.instance_variable_get(:@opts)[:max_nesting]
end

def test_initialize_with_max_nesting_false_limit
json_obj = JsonPath.new('$.a.b.c', max_nesting: false)
assert_equal false, json_obj.instance_variable_get(:@opts)[:max_nesting]
end

def example_object
{ 'store' => {
'book' => [
Expand Down

0 comments on commit 2a1fe15

Please sign in to comment.