Skip to content

Commit

Permalink
Merge pull request #2336 from dkinzer/deep-dup-to_h-search_state
Browse files Browse the repository at this point in the history
Do not mutate search_state internal params on .to_h
  • Loading branch information
jcoyne committed Sep 25, 2020
2 parents 4b00515 + 6c1b3fc commit 95afa3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/blacklight/search_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.normalize_params(untrusted_params = {})
end

def to_hash
@params.dup
@params.deep_dup
end
alias to_h to_hash

Expand Down
12 changes: 12 additions & 0 deletions spec/lib/blacklight/search_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
expect(params).to eq('q' => 'foo')
end
end

context 'deleting deep item from to_h' do
let(:params) { { foo: { bar: [] } } }

it 'does not mutate search_state to deep mutate search_state.to_h' do
params = search_state.to_h
params[:foo][:bar] << 'buzz'

expect(search_state.to_h).to eq('foo' => { 'bar' => [] })
expect(params).to eq('foo' => { 'bar' => ['buzz'] })
end
end
end

describe 'interface compatibility with params' do
Expand Down

0 comments on commit 95afa3d

Please sign in to comment.