Skip to content

Commit

Permalink
[Fix rails#51463] Raise an error when invalid :on or :except opti…
Browse files Browse the repository at this point in the history
…ons are given to `#resource` or `#resouces`
  • Loading branch information
joshuay03 committed Apr 2, 2024
1 parent ee4a371 commit 8b6140c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,7 @@
* Raise an `ArgumentError` when invalid `:on` or `:except` options are passed into `#resouce` and `#resources`.

*Joshua Young*

* Request Forgery takes relative paths into account.

*Stefan Wienert*
Expand Down
4 changes: 4 additions & 0 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -1175,6 +1175,10 @@ def initialize(entities, api_only, shallow, options = {})
raise ArgumentError, ":param option can't contain colons"
end

if (invalid_actions = (options.values_at(:only, :except).flatten.compact - default_actions)).any?
raise ArgumentError, "expected :only and :except to be one or more of #{default_actions}, got #{invalid_actions}"
end

@name = entities.to_s
@path = (options[:path] || @name).to_s
@controller = (options[:controller] || @name).to_s
Expand Down
45 changes: 45 additions & 0 deletions actionpack/test/controller/resources_test.rb
Expand Up @@ -1109,6 +1109,51 @@ def test_singleton_resource_name_is_not_singularized
end
end

def test_invalid_only_option
expected_message = "expected :only and :except to be one or more of [:index, :create, :new, :show, :update, :destroy, :edit], got [:foo, :bar]"
assert_raise(ArgumentError, match: expected_message) do
with_routing do |set|
set.draw do
resources :products, only: [:foo, :bar]
end
end
end
end

def test_invalid_only_option_singleton
expected_message = "expected :only and :except to be one or more of [:show, :create, :update, :destroy, :new, :edit], got [:foo, :bar]"
assert_raise(ArgumentError, match: expected_message) do
with_routing do |set|
set.draw do
resource :products, only: [:foo, :bar]
end
end
end
end

def test_invalid_except_option
expected_message = "expected :only and :except to be one or more of [:index, :create, :new, :show, :update, :destroy, :edit], got [:foo]"

assert_raise(ArgumentError, match: expected_message) do
with_routing do |set|
set.draw do
resources :products, except: :foo
end
end
end
end

def test_invalid_except_option_singleton
expected_message = "expected :only and :except to be one or more of [:show, :create, :update, :destroy, :new, :edit], got [:foo]"
assert_raise(ArgumentError, match: expected_message) do
with_routing do |set|
set.draw do
resource :products, except: :foo
end
end
end
end

private
def with_restful_routing(*args)
options = args.extract_options!
Expand Down

0 comments on commit 8b6140c

Please sign in to comment.