Skip to content
Browse files

don't mutate hash with fetch

  • Loading branch information...
1 parent 6e257ae commit 7e1e52c75359724fdc9e2500e9e6c1903392f49f @fxn fxn committed Dec 23, 2013
Showing with 9 additions and 3 deletions.
  1. +3 −3 lib/action_controller/parameters.rb
  2. +6 −0 test/parameters_permit_test.rb
View
6 lib/action_controller/parameters.rb
@@ -79,7 +79,7 @@ def [](key)
end
def fetch(key, *args)
- convert_hashes_to_parameters(key, super)
+ convert_hashes_to_parameters(key, super, false)
rescue KeyError, IndexError
raise ActionController::ParameterMissing.new(key)
end
@@ -110,9 +110,9 @@ def convert_value(value)
private
- def convert_hashes_to_parameters(key, value)
+ def convert_hashes_to_parameters(key, value, assign_if_converted=true)
converted = convert_value_to_parameters(value)
- self[key] = converted unless converted.equal?(value)
+ self[key] = converted if assign_if_converted && !converted.equal?(value)
converted
end
View
6 test/parameters_permit_test.rb
@@ -334,6 +334,12 @@ def assert_filtered_out(params, key)
assert_equal "prop0", params[:properties]["0"]
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 'hashes in array values get wrapped' do
params = ActionController::Parameters.new(foo: [{}, {}])
params[:foo].each do |hash|

0 comments on commit 7e1e52c

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