From 139787e7ab83d658ecb62b0ff9ad010a2dd45b49 Mon Sep 17 00:00:00 2001 From: Anupama Kumari Date: Tue, 10 Oct 2023 16:29:38 +0530 Subject: [PATCH 1/2] symbolize_keys option doesn't work as the README says --- lib/jsonpath.rb | 6 +++++- test/test_jsonpath.rb | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 0bd509d..0388f62 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -80,7 +80,7 @@ def join(join_path) def on(obj_or_str, opts = {}) a = enum_on(obj_or_str).to_a - if opts[:symbolize_keys] + if symbolize_keys?(opts) a.map! do |e| e.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; } end @@ -153,4 +153,8 @@ def set_max_nesting return unless @opts[:max_nesting].is_a?(Integer) && @opts[:max_nesting] > MAX_NESTING_ALLOWED @opts[:max_nesting] = false end + + def symbolize_keys?(opts) + opts.fetch(:symbolize_keys, @opts&.dig(:symbolize_keys)) + end end diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 5ab02d7..5ad999d 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -1312,4 +1312,10 @@ def test_extractore_with_dollar_key assert_equal ["success"], JsonPath.on(json, "$.test.$") assert_equal ["123"], JsonPath.on(json, "$.test.a") end + + def test_symbolize_key + data = { "store" => { "book" => [{"category" => "test"}]}} + assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]', symbolize_keys: true).on(data) + assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]').on(data, symbolize_keys: true) + end end From b8bd29f89d55ae7c6dcfd5c528228e4d0afd6776 Mon Sep 17 00:00:00 2001 From: Anupama Kumari Date: Tue, 10 Oct 2023 16:40:05 +0530 Subject: [PATCH 2/2] test sample correction --- test/test_jsonpath.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 5ad999d..80fa0bb 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -1314,7 +1314,7 @@ def test_extractore_with_dollar_key end def test_symbolize_key - data = { "store" => { "book" => [{"category" => "test"}]}} + data = { "store" => { "book" => [{"category" => "reference"}]}} assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]', symbolize_keys: true).on(data) assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]').on(data, symbolize_keys: true) end