Skip to content

Commit 184c962

Browse files
authored
Merge pull request rails#53270 from joshuay03/fix-only-except-validation-with-string-values
[Fix rails#53269] `:only` and `:except` validation incorrectly handles string values
2 parents f86f52a + 084fb84 commit 184c962

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ def singleton?; false; end
12681268

12691269
private
12701270
def invalid_only_except_options(options, valid_actions)
1271-
options.values_at(:only, :except).flatten.compact.uniq - valid_actions
1271+
options.values_at(:only, :except).flatten.compact.uniq.map(&:to_sym) - valid_actions
12721272
end
12731273
end
12741274

actionpack/test/controller/resources_test.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,17 @@ def test_resource_has_only_show_action
816816
end
817817
end
818818

819+
def test_resource_has_only_show_action_with_string_value
820+
with_routing do |set|
821+
set.draw do
822+
resources :products, only: "show"
823+
end
824+
825+
assert_resource_allowed_routes("products", {}, { id: "1" }, :show, [:index, :new, :create, :edit, :update, :destroy])
826+
assert_resource_allowed_routes("products", { format: "xml" }, { id: "1" }, :show, [:index, :new, :create, :edit, :update, :destroy])
827+
end
828+
end
829+
819830
def test_singleton_resource_has_only_show_action
820831
with_routing do |set|
821832
set.draw do
@@ -827,6 +838,17 @@ def test_singleton_resource_has_only_show_action
827838
end
828839
end
829840

841+
def test_singleton_resource_has_only_show_action_with_string_value
842+
with_routing do |set|
843+
set.draw do
844+
resource :account, only: "show"
845+
end
846+
847+
assert_singleton_resource_allowed_routes("accounts", {}, :show, [:index, :new, :create, :edit, :update, :destroy])
848+
assert_singleton_resource_allowed_routes("accounts", { format: "xml" }, :show, [:index, :new, :create, :edit, :update, :destroy])
849+
end
850+
end
851+
830852
def test_resource_does_not_have_destroy_action
831853
with_routing do |set|
832854
set.draw do
@@ -1113,7 +1135,7 @@ def test_invalid_only_option_for_resources
11131135
assert_raise(ArgumentError, match: expected_message) do
11141136
with_routing do |set|
11151137
set.draw do
1116-
resources :products, only: [:foo, :bar]
1138+
resources :products, only: [:foo, "bar"]
11171139
end
11181140
end
11191141
end
@@ -1124,7 +1146,7 @@ def test_invalid_only_option_for_singleton_resource
11241146
assert_raise(ArgumentError, match: expected_message) do
11251147
with_routing do |set|
11261148
set.draw do
1127-
resource :products, only: [:foo, :bar]
1149+
resource :products, only: [:foo, "bar"]
11281150
end
11291151
end
11301152
end

0 commit comments

Comments
 (0)