Params processing shouldn't error on arrays of objects other than hashes.

This commit is contained in:
Chris Hanks 2012-02-01 13:32:07 -08:00
parent 3f480092f1
commit 0d6c14c45e
2 changed files with 22 additions and 7 deletions

View File

@ -868,13 +868,16 @@ module Sinatra
end
# Enable string or symbol key access to the nested params hash.
def indifferent_params(params)
params = indifferent_hash.merge(params)
params.each do |key, value|
case value
when Hash then params[key] = indifferent_params(value)
when Array then params[key] = value.map { |item| indifferent_params(item) }
end
def indifferent_params(object)
case object
when Hash
new_hash = indifferent_hash
object.each { |key, value| new_hash[key] = indifferent_params(value) }
new_hash
when Array
object.map { |item| indifferent_params(item) }
else
object
end
end

View File

@ -372,6 +372,18 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'well, alright', body
end
it "supports arrays within params" do
mock_app {
get '/foo' do
assert_equal ['A', 'B'], params['bar']
'looks good'
end
}
get '/foo?bar[]=A&bar[]=B'
assert ok?
assert_equal 'looks good', body
end
it "supports deeply nested params" do
expected_params = {
"emacs" => {