diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 238a7bd..914b44f 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -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? @@ -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 diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index db8a5e4..026cda6 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -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' => [