mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Extend indifferent_params to support hashes nested within arrays.
This commit is contained in:
parent
80b98a22a8
commit
3f480092f1
2 changed files with 16 additions and 2 deletions
|
@ -871,8 +871,10 @@ module Sinatra
|
|||
def indifferent_params(params)
|
||||
params = indifferent_hash.merge(params)
|
||||
params.each do |key, value|
|
||||
next unless value.is_a?(Hash)
|
||||
params[key] = indifferent_params(value)
|
||||
case value
|
||||
when Hash then params[key] = indifferent_params(value)
|
||||
when Array then params[key] = value.map { |item| indifferent_params(item) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -360,6 +360,18 @@ class RoutingTest < Test::Unit::TestCase
|
|||
assert_equal 'well, alright', body
|
||||
end
|
||||
|
||||
it "exposes params nested within arrays with indifferent hash" do
|
||||
mock_app {
|
||||
get '/testme' do
|
||||
assert_equal 'baz', params['bar'][0]['foo']
|
||||
assert_equal 'baz', params['bar'][0][:foo]
|
||||
'well, alright'
|
||||
end
|
||||
}
|
||||
get '/testme?bar[][foo]=baz'
|
||||
assert_equal 'well, alright', body
|
||||
end
|
||||
|
||||
it "supports deeply nested params" do
|
||||
expected_params = {
|
||||
"emacs" => {
|
||||
|
|
Loading…
Reference in a new issue