Skip to content

Commit

Permalink
don't mutate hash with fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
dougcole committed Oct 27, 2013
1 parent 52199d1 commit 7171111
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion actionpack/lib/action_controller/metal/strong_parameters.rb
Expand Up @@ -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
Expand Down
Expand Up @@ -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" }
Expand Down

0 comments on commit 7171111

Please sign in to comment.