aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorVince DeVendra <vdevendra@patientslikeme.com>2018-03-24 12:23:37 -0400
committerVince DeVendra <vdevendra@patientslikeme.com>2018-03-24 12:23:37 -0400
commitd65d74b9158acf1ca79508a4ed49ffc712bdb983 (patch)
tree761f5a5f452173ae3b780225a93d6ceea6f84ee5 /actionpack
parentff6d498704cff88b823871d20c5dfcaa3345ead7 (diff)
downloadrails-d65d74b9158acf1ca79508a4ed49ffc712bdb983.tar.gz
rails-d65d74b9158acf1ca79508a4ed49ffc712bdb983.tar.bz2
rails-d65d74b9158acf1ca79508a4ed49ffc712bdb983.zip
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.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/metal/strong_parameters.rb3
-rw-r--r--actionpack/test/controller/parameters/accessors_test.rb8
2 files changed, 10 insertions, 1 deletions
diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb
index 75ca282804..4789252550 100644
--- a/actionpack/lib/action_controller/metal/strong_parameters.rb
+++ b/actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -590,7 +590,8 @@ module ActionController
# params2 = ActionController::Parameters.new(foo: [10, 11, 12])
# params2.dig(:foo, 1) # => 11
def dig(*keys)
- convert_value_to_parameters(@parameters.dig(*keys))
+ convert_hashes_to_parameters(keys.first, @parameters[keys.first])
+ @parameters.dig(*keys)
end
# Returns a new <tt>ActionController::Parameters</tt> instance that
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