From 0d085afd391193a63c005b8f0fef5ed4be79910c Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 13 Dec 2019 12:06:06 -0600 Subject: Ensure that Hashie::Arrays are not deconverted In order for `#dig` to work properly, we need Arrays to be `Hashie::Array`s to be aware of the key conversion. Our original `Mash#convert_value` method was deconverting `Hashie::Array`s into normal Arrays and causing `#dig` to behave in an unexpected manner. --- CHANGELOG.md | 1 + lib/hashie/mash.rb | 2 -- spec/hashie/mash_spec.rb | 9 +++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b46c63..d1edf2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ scheme are considered to be bugs. ### Fixed * [#467](https://github.com/intridea/hashie/pull/467): Fixed `DeepMerge#deep_merge` mutating nested values within the receiver - [@michaelherold](https://github.com/michaelherold). +* [#505](https://github.com/hashie/hashie/pull/505): Ensure that `Hashie::Array`s are not deconverted within `Hashie::Mash`es to make `Mash#dig` work properly - [@michaelherold](https://github.com/michaelherold). * Your contribution here. ### Security diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index f832e98..97d6fa6 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -382,8 +382,6 @@ module Hashie when ::Hash val = val.dup if duping self.class.new(val) - when Array - val.map { |e| convert_value(e) } when ::Array Array.new(val.map { |e| convert_value(e) }) else diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index fa4e47b..4629124 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -982,6 +982,7 @@ describe Hashie::Mash do with_minimum_ruby('2.3.0') do describe '#dig' do subject { described_class.new(a: { b: 1 }) } + it 'accepts both string and symbol as key' do expect(subject.dig(:a, :b)).to eq(1) expect(subject.dig('a', 'b')).to eq(1) @@ -994,6 +995,14 @@ describe Hashie::Mash do expect(subject.dig('1', :b)).to eq(1) end end + + context 'when the Mash wraps a Hashie::Array' do + it 'handles digging into an array' do + mash = described_class.new(alphabet: { first_three: Hashie::Array['a', 'b', 'c'] }) + + expect(mash.dig(:alphabet, :first_three, 0)).to eq 'a' + end + end end end -- cgit v1.2.1