Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rack'
require 'grape'
require File.dirname(__FILE__) + '/helpers/extensions/hash'

module Grape
# An Endpoint is the proxy scope in which all routing
Expand Down Expand Up @@ -30,7 +31,11 @@ def self.call(env)
def params
@params ||= request.params.merge(env['rack.routing_args'] || {}).inject({}) do |h,(k,v)|
h[k.to_s] = v
h[k.to_sym] = v

# Also return a version of the parameters with symbols as hash keys
v_sym = (v.kind_of?(Hash) && v.respond_to?(:key_strings_to_symbols)) ? v.key_strings_to_symbols : v
h[k.to_sym] = v_sym

h
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/grape/helpers/extensions/hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Hash
# Recursively replace key names that should be symbols with symbols.
def key_strings_to_symbols
new_hash = Hash.new
self.each_pair do |k,v|
new_value = (v.kind_of?(Hash) && v.respond_to?(:key_strings_to_symbols)) ? v.key_strings_to_symbols : v
new_key = k.kind_of?(String) ? k.to_sym : k
new_hash[new_key] = new_value
end
new_hash
end
end
36 changes: 36 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ def app; subject end
get '/hey/12'
last_response.body.should == '12'
end

it 'returns a hashed version of parameters which are hashes with symbol keys' do
subject.get('/hey') do
"#{params[:location][:city]}, #{params[:location][:state]}"
end

get '/hey?location[city]=New%20York&location[state]=NY'
last_response.body.should == "New York, NY"
end

it 'returns a hashed version of parameters which are hashes with string keys' do
subject.get('/hey') do
"#{params['location']['city']}, #{params['location']['state']}"
end

get '/hey?location[city]=New%20York&location[state]=NY'
last_response.body.should == "New York, NY"
end

it 'returns a hashed version of parameters which are hashes with symbol keys multiple levels deep' do
subject.get('/hey') do
params[:location][:city][:neighborhood]
end

get '/hey?location[city][neighborhood]=Chelsea'
last_response.body.should == "Chelsea"
end

it 'returns a hashed version of parameters which are hashes with string keys multiple levels deep' do
subject.get('/hey') do
params['location']['city']['neighborhood']
end

get '/hey?location[city][neighborhood]=Chelsea'
last_response.body.should == "Chelsea"
end
end

describe '#error!' do
Expand Down