mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #12656 from dougcole/fix_strong_parameters_fetch
don't let StrongParameters mutate the hash with fetch
This commit is contained in:
commit
094e31ce67
2 changed files with 14 additions and 1 deletions
|
@ -284,7 +284,14 @@ module ActionController
|
|||
# params.fetch(:none, 'Francesco') # => "Francesco"
|
||||
# params.fetch(:none) { 'Francesco' } # => "Francesco"
|
||||
def fetch(key, *args)
|
||||
convert_hashes_to_parameters(key, super)
|
||||
value = super
|
||||
# Don't rely on +convert_hashes_to_parameters+
|
||||
# so as to not mutate via a +fetch+
|
||||
if value.is_a?(Hash)
|
||||
value = self.class.new(value)
|
||||
value.permit! if permitted?
|
||||
end
|
||||
value
|
||||
rescue KeyError
|
||||
raise ActionController::ParameterMissing.new(key)
|
||||
end
|
||||
|
|
|
@ -147,6 +147,12 @@ class ParametersPermitTest < ActiveSupport::TestCase
|
|||
assert_equal :foo, e.param
|
||||
end
|
||||
|
||||
test "fetch with a default value of a hash does not mutate the object" do
|
||||
params = ActionController::Parameters.new({})
|
||||
params.fetch :foo, {}
|
||||
assert_equal nil, params[:foo]
|
||||
end
|
||||
|
||||
test "fetch doesnt raise ParameterMissing exception if there is a default" do
|
||||
assert_equal "monkey", @params.fetch(:foo, "monkey")
|
||||
assert_equal "monkey", @params.fetch(:foo) { "monkey" }
|
||||
|
|
Loading…
Reference in a new issue