Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #461 from chanks/indifferent-params-in-arrays

Indifferent param hashes nested within arrays
  • Loading branch information...
commit 83f94aa50f13c0612935c975769d9d3167d5eceb 2 parents 80b98a2 + 0d6c14c
@rkh rkh authored
Showing with 34 additions and 5 deletions.
  1. +10 −5 lib/sinatra/base.rb
  2. +24 −0 test/routing_test.rb
View
15 lib/sinatra/base.rb
@@ -868,11 +868,16 @@ def static!
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|
- next unless value.is_a?(Hash)
- params[key] = indifferent_params(value)
+ 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
24 test/routing_test.rb
@@ -360,6 +360,30 @@ 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 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" => {
Please sign in to comment.
Something went wrong with that request. Please try again.