Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #141: nesting of 101 too deep #160

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test cases
anupama-kumari committed Jul 5, 2023
commit da4e1dc479b3c317eed459d7a6eec9eda21b6e4e
5 changes: 3 additions & 2 deletions lib/jsonpath.rb
Original file line number Diff line number Diff line change
@@ -12,20 +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)
@opts[:max_nesting] = false if @opts[:max_nesting] > 100
@opts[:max_nesting] = false if @opts[:max_nesting] > MAX_NESTING_ALLOWED
scanner = StringScanner.new(path.strip)
@path = []
until scanner.eos?
15 changes: 15 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
@@ -1195,6 +1195,21 @@ 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 example_object
{ 'store' => {
'book' => [