From aba600885088ed4922c4ddb538ea3c003de2cef5 Mon Sep 17 00:00:00 2001 From: Gannon McGibbon Date: Thu, 31 Mar 2022 18:10:23 -0400 Subject: [PATCH] Don't delegate ActionController::Parameters#values to hash Instead, cast any nested hashes into ActionController::Parameters. --- actionpack/CHANGELOG.md | 6 +++++- .../action_controller/metal/strong_parameters.rb | 15 ++++++--------- .../test/controller/parameters/accessors_test.rb | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 29d7be226c3..658f9b9519b 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,7 @@ +* Make ActionController::Parameters#values cast nested hashes into parameters. + + *Gannon McGibbon* + * Introduce `html:` and `screenshot:` kwargs for system test screenshot helper Use these as an alternative to the already-available environment variables. @@ -26,7 +30,7 @@ * Fix `authenticate_with_http_basic` to allow for missing password. Before Rails 7.0 it was possible to handle basic authentication with only a username. - + ```ruby authenticate_with_http_basic do |token, _| ApiClient.authenticate(token) diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index dc5790c5ac0..b5b1233b39a 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -222,15 +222,7 @@ module ActionController # value?(value) # # Returns true if the given value is present for some key in the parameters. - - ## - # :method: values - # - # :call-seq: - # values() - # - # Returns a new array of the values of the parameters. - delegate :keys, :key?, :has_key?, :member?, :values, :has_value?, :value?, :empty?, :include?, + delegate :keys, :key?, :has_key?, :member?, :has_value?, :value?, :empty?, :include?, :as_json, :to_s, :each_key, to: :@parameters # By default, never raise an UnpermittedParameters exception if these @@ -403,6 +395,11 @@ module ActionController self end + # Returns a new array of the values of the parameters. + def values + to_enum(:each_value).to_a + end + # Attribute that keeps track of converted arrays, if any, to avoid double # looping in the common use case permit + mass-assignment. Defined in a # method to instantiate it only if needed. diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index 2f7a9b52025..a54e9b40066 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -312,8 +312,8 @@ class ParametersAccessorsTest < ActiveSupport::TestCase end test "values returns an array of the values of the params" do - params = ActionController::Parameters.new(city: "Chicago", state: "Illinois") - assert_equal ["Chicago", "Illinois"], params.values + params = ActionController::Parameters.new(city: "Chicago", state: "Illinois", person: ActionController::Parameters.new(first_name: "David")) + assert_equal ["Chicago", "Illinois", ActionController::Parameters.new(first_name: "David")], params.values end test "values_at retains permitted status" do