Browse files

don't mutate hash with fetch

  • Loading branch information...
1 parent 52199d1 commit 7171111d3af10c80e3b38658d4fa0aa36858677f @dougcole dougcole committed Oct 27, 2013
View
9 actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -284,7 +284,14 @@ def [](key)
# 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
View
6 actionpack/test/controller/parameters/parameters_permit_test.rb
@@ -147,6 +147,12 @@ def assert_filtered_out(params, key)
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" }

0 comments on commit 7171111

Please sign in to comment.