From d65d74b9158acf1ca79508a4ed49ffc712bdb983 Mon Sep 17 00:00:00 2001 From: Vince DeVendra Date: Sat, 24 Mar 2018 12:23:37 -0400 Subject: Make mutating params#dig mutate underlying params When #dig was called on a params object and return either a Hash or an Array, and that value was subsquently mutated, it would not modify the containing params object. That means that the behavior of `params.dig(:a, :b)[:c] = 1` did not match either `params[:a][:b][:c] = 1` nor `hash.dig(:a, :b)[:c] = 1`. Similarly to `ActionController::Parameters#[]`, use `#convert_hashes_to_parameters` to pre-convert values and insert them in the receiving params object prior to returning them. --- actionpack/test/controller/parameters/accessors_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index 07a897a103..674b2c6266 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -284,4 +284,12 @@ class ParametersAccessorsTest < ActiveSupport::TestCase value.is_a?(ActionController::Parameters) end end + + test "mutating #dig return value mutates underlying parameters" do + @params.dig(:person, :name)[:first] = "Bill" + assert_equal "Bill", @params.dig(:person, :name, :first) + + @params.dig(:person, :addresses)[0] = { city: "Boston", state: "Massachusetts" } + assert_equal "Boston", @params.dig(:person, :addresses, 0, :city) + end end -- cgit v1.2.3