Skip to content

Commit

Permalink
Merge pull request #9439 from senny/9435_router_params_as_integers
Browse files Browse the repository at this point in the history
convert non-string default params to strings in the router.
  • Loading branch information
pixeltrix committed Feb 26, 2013
2 parents 923ec86 + 794cbf3 commit 6a235fa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,5 +1,15 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* Skip valid encoding checks for non-String parameters that come
from the matched route's defaults.
Fixes #9435.

Example:

root to: 'main#posts', page: 1

*Yves Senn*

* Don't verify Regexp requirements for non-Regexp `:constraints`. * Don't verify Regexp requirements for non-Regexp `:constraints`.
Fixes #9432. Fixes #9432.


Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -31,6 +31,8 @@ def call(env)
# If any of the path parameters has a invalid encoding then # If any of the path parameters has a invalid encoding then
# raise since it's likely to trigger errors further on. # raise since it's likely to trigger errors further on.
params.each do |key, value| params.each do |key, value|
next unless value.respond_to?(:valid_encoding?)

unless value.valid_encoding? unless value.valid_encoding?
raise ActionController::BadRequest, "Invalid parameter: #{key} => #{value}" raise ActionController::BadRequest, "Invalid parameter: #{key} => #{value}"
end end
Expand Down
22 changes: 21 additions & 1 deletion actionpack/test/dispatch/routing_test.rb
Expand Up @@ -1346,7 +1346,7 @@ def test_nested_optional_path_shorthand
assert_equal 'en', @request.params[:locale] assert_equal 'en', @request.params[:locale]
end end


def test_default_params def test_default_string_params
draw do draw do
get 'inline_pages/(:id)', :to => 'pages#show', :id => 'home' get 'inline_pages/(:id)', :to => 'pages#show', :id => 'home'
get 'default_pages/(:id)', :to => 'pages#show', :defaults => { :id => 'home' } get 'default_pages/(:id)', :to => 'pages#show', :defaults => { :id => 'home' }
Expand All @@ -1366,6 +1366,26 @@ def test_default_params
assert_equal 'home', @request.params[:id] assert_equal 'home', @request.params[:id]
end end


def test_default_integer_params
draw do
get 'inline_pages/(:page)', to: 'pages#show', page: 1
get 'default_pages/(:page)', to: 'pages#show', defaults: { page: 1 }

defaults page: 1 do
get 'scoped_pages/(:page)', to: 'pages#show'
end
end

get '/inline_pages'
assert_equal 1, @request.params[:page]

get '/default_pages'
assert_equal 1, @request.params[:page]

get '/scoped_pages'
assert_equal 1, @request.params[:page]
end

def test_resource_constraints def test_resource_constraints
draw do draw do
resources :products, :constraints => { :id => /\d{4}/ } do resources :products, :constraints => { :id => /\d{4}/ } do
Expand Down

0 comments on commit 6a235fa

Please sign in to comment.