Skip to content
This repository
Browse code

don't mutate hash with fetch

  • Loading branch information...
commit 7171111d3af10c80e3b38658d4fa0aa36858677f 1 parent 52199d1
Doug Cole dougcole authored
9 actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -284,7 +284,14 @@ def [](key)
284 284 # params.fetch(:none, 'Francesco') # => "Francesco"
285 285 # params.fetch(:none) { 'Francesco' } # => "Francesco"
286 286 def fetch(key, *args)
287   - convert_hashes_to_parameters(key, super)
  287 + value = super
  288 + # Don't rely on +convert_hashes_to_parameters+
  289 + # so as to not mutate via a +fetch+
  290 + if value.is_a?(Hash)
  291 + value = self.class.new(value)
  292 + value.permit! if permitted?
  293 + end
  294 + value
288 295 rescue KeyError
289 296 raise ActionController::ParameterMissing.new(key)
290 297 end
6 actionpack/test/controller/parameters/parameters_permit_test.rb
@@ -147,6 +147,12 @@ def assert_filtered_out(params, key)
147 147 assert_equal :foo, e.param
148 148 end
149 149
  150 + test "fetch with a default value of a hash does not mutate the object" do
  151 + params = ActionController::Parameters.new({})
  152 + params.fetch :foo, {}
  153 + assert_equal nil, params[:foo]
  154 + end
  155 +
150 156 test "fetch doesnt raise ParameterMissing exception if there is a default" do
151 157 assert_equal "monkey", @params.fetch(:foo, "monkey")
152 158 assert_equal "monkey", @params.fetch(:foo) { "monkey" }

0 comments on commit 7171111

Please sign in to comment.
Something went wrong with that request. Please try again.