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
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 @@ -1195,6 +1195,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