Skip to content

Commit

Permalink
Remove #to_hash deprecation message when params are permitted:
Browse files Browse the repository at this point in the history
- `#to_hash` deprecation makes things a bit tricky for a developer to find out what in their app they need to modify in order to safely turn on the `raise_on_unfiltered_parameters` flag in production. The message is the same whether the params are permitted or not
- Removed the deprecation message when `#to_hash` gets called on a permitted params
  • Loading branch information
Edouard-chin committed Oct 5, 2017
1 parent 589edb8 commit 55cbc18
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def to_h
# safe_params = params.permit(:name)
# safe_params.to_hash # => {"name"=>"Senjougahara Hitagi"}
def to_hash
if self.class.raise_on_unfiltered_parameters
if self.class.raise_on_unfiltered_parameters || permitted?
to_h.to_hash
else
message = <<-DEPRECATE.squish
Expand Down
26 changes: 11 additions & 15 deletions actionpack/test/controller/parameters/parameters_permit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ def walk_permitted params
end
end

test "#to_hash when params are permitted" do
assert_not_deprecated do
assert_equal({ "person" => { "age" => "32" } }, @params.permit(person: [:age]).to_hash)
end
end

test "to_hash raises UnfilteredParameters on unfiltered params if raise_on_unfiltered_parameters is true" do
begin
old_value = ActionController::Parameters.raise_on_unfiltered_parameters
Expand All @@ -382,28 +388,18 @@ def walk_permitted params
test "to_hash returns converted hash on permitted params" do
@params.permit!

assert_deprecated do
assert_instance_of Hash, @params.to_hash
end
assert_deprecated do
assert_not_kind_of ActionController::Parameters, @params.to_hash
end
assert_instance_of Hash, @params.to_hash
assert_not_kind_of ActionController::Parameters, @params.to_hash
end

test "to_hash returns converted hash when .permit_all_parameters is set" do
begin
ActionController::Parameters.permit_all_parameters = true
params = ActionController::Parameters.new(crab: "Senjougahara Hitagi")

assert_deprecated do
assert_instance_of Hash, params.to_hash
end
assert_deprecated do
assert_not_kind_of ActionController::Parameters, params.to_hash
end
assert_deprecated do
assert_equal({ "crab" => "Senjougahara Hitagi" }, params.to_hash)
end
assert_instance_of Hash, params.to_hash
assert_not_kind_of ActionController::Parameters, params.to_hash
assert_equal({ "crab" => "Senjougahara Hitagi" }, params.to_hash)
ensure
ActionController::Parameters.permit_all_parameters = false
end
Expand Down

0 comments on commit 55cbc18

Please sign in to comment.