Move the parameter wrapper code to the ActionController::Railtie class

This commit is contained in:
Rafael Mendonça França 2021-09-17 17:11:56 -04:00
parent 1b0c914133
commit 4db42073cd
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
5 changed files with 22 additions and 22 deletions

View File

@ -12,6 +12,7 @@ module ActionController
config.action_controller = ActiveSupport::OrderedOptions.new config.action_controller = ActiveSupport::OrderedOptions.new
config.action_controller.raise_on_open_redirects = false config.action_controller.raise_on_open_redirects = false
config.action_controller.log_query_tags_around_actions = true config.action_controller.log_query_tags_around_actions = true
config.action_controller.wrap_parameters_by_default = false
config.eager_load_namespaces << ActionController config.eager_load_namespaces << ActionController
@ -62,15 +63,18 @@ module ActionController
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes) extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
extend ::ActionController::Railties::Helpers extend ::ActionController::Railties::Helpers
wrap_parameters format: [:json] if options.wrap_parameters_by_default
# Configs used in other initializers # Configs used in other initializers
options = options.except( filtered_options = options.except(
:log_query_tags_around_actions, :log_query_tags_around_actions,
:permit_all_parameters, :permit_all_parameters,
:action_on_unpermitted_parameters, :action_on_unpermitted_parameters,
:always_permitted_parameters :always_permitted_parameters,
:wrap_parameters_by_default
) )
options.each do |k, v| filtered_options.each do |k, v|
k = "#{k}=" k = "#{k}="
if respond_to?(k) if respond_to?(k)
send(k, v) send(k, v)

View File

@ -889,6 +889,11 @@ Raises an `ArgumentError` when an unpermitted open redirect occurs. The default
Determines whether controller context for query tags will be automatically Determines whether controller context for query tags will be automatically
updated via an `around_filter`. The default value is `true`. updated via an `around_filter`. The default value is `true`.
#### `config.action_controller.wrap_parameters_by_default`
Configures the [`ParamsWrapper`](https://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html) to wrap json
request by default.
#### `ActionController::Base.wrap_parameters` #### `ActionController::Base.wrap_parameters`
Configures the [`ParamsWrapper`](https://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html). This can be called at Configures the [`ParamsWrapper`](https://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html). This can be called at
@ -1690,7 +1695,7 @@ Accepts a string for the HTML tag used to wrap attachments. Defaults to `"action
- `config.active_storage.video_preview_arguments`: `"-vf 'select=eq(n\\,0)+eq(key\\,1)+gt(scene\\,0.015),loop=loop=-1:size=2,trim=start_frame=1' -frames:v 1 -f image2"` - `config.active_storage.video_preview_arguments`: `"-vf 'select=eq(n\\,0)+eq(key\\,1)+gt(scene\\,0.015),loop=loop=-1:size=2,trim=start_frame=1' -frames:v 1 -f image2"`
- `config.active_record.verify_foreign_keys_for_fixtures`: `true` - `config.active_record.verify_foreign_keys_for_fixtures`: `true`
- `config.active_storage.variant_processor`: `:vips` - `config.active_storage.variant_processor`: `:vips`
- `ActionController::Base.wrap_parameters`: `format: [:json]` - `config.action_controller.wrap_parameters_by_default`: `true`
#### For '6.1', defaults from previous versions below and: #### For '6.1', defaults from previous versions below and:
@ -1773,6 +1778,7 @@ Accepts a string for the HTML tag used to wrap attachments. Defaults to `"action
- `config.action_mailer.smtp_timeout`: `nil` - `config.action_mailer.smtp_timeout`: `nil`
- `config.active_storage.video_preview_arguments`: `"-y -vframes 1 -f image2"` - `config.active_storage.video_preview_arguments`: `"-y -vframes 1 -f image2"`
- `config.active_storage.variant_processor`: `:mini_magick` - `config.active_storage.variant_processor`: `:mini_magick`
- `config.action_controller.wrap_parameters_by_default`: `false`
### Configuring a Database ### Configuring a Database

View File

@ -238,11 +238,7 @@ module Rails
if respond_to?(:action_controller) if respond_to?(:action_controller)
action_controller.raise_on_open_redirects = true action_controller.raise_on_open_redirects = true
# This can't use the standard configuration pattern because you can call action_controller.wrap_parameters_by_default = true
# wrap_parameters at the top level or on any controller.
ActiveSupport.on_load(:action_controller) do
ActionController::Base.wrap_parameters format: [:json]
end
end end
else else
raise "Unknown version #{target_version.to_s.inspect}" raise "Unknown version #{target_version.to_s.inspect}"

View File

@ -86,5 +86,5 @@
# Enable parameter wrapping for JSON. # Enable parameter wrapping for JSON.
# Previously this was set in an initializer. It's fine to keep using that initializer if you've customized it. # Previously this was set in an initializer. It's fine to keep using that initializer if you've customized it.
# To disable parameter wrapping entirely, call `ActionController::Base.wrap_parameters false` in an initializer. # To disable parameter wrapping entirely, set this config to `false`.
# ActionController::Base.wrap_parameters format: [:json] # Rails.application.config.action_controller.wrap_parameters_by_default = true

View File

@ -3415,7 +3415,7 @@ module ApplicationTests
add_to_config 'config.load_defaults "6.1"' add_to_config 'config.load_defaults "6.1"'
app_file "config/initializers/new_framework_defaults_7_0.rb", <<-RUBY app_file "config/initializers/new_framework_defaults_7_0.rb", <<-RUBY
ActionController::Base.wrap_parameters format: [:json] Rails.application.config.action_controller.wrap_parameters_by_default = true
RUBY RUBY
app "production" app "production"
@ -3423,17 +3423,11 @@ module ApplicationTests
assert_equal [:json], ActionController::Base._wrapper_options.format assert_equal [:json], ActionController::Base._wrapper_options.format
end end
test "ParamsWrapper can be changed from the default" do
add_to_config "ActionController::Base.wrap_parameters format: [:xml]"
app "production"
assert_equal [:xml], ActionController::Base._wrapper_options.format
end
test "ParamsWrapper can be changed from the default in the initializer that was created prior to Rails 7" do test "ParamsWrapper can be changed from the default in the initializer that was created prior to Rails 7" do
app_file "config/initializers/wrap_parameters.rb", <<-RUBY app_file "config/initializers/wrap_parameters.rb", <<-RUBY
ActionController::Base.wrap_parameters format: [:xml] ActiveSupport.on_load(:action_controller) do
wrap_parameters format: [:xml]
end
RUBY RUBY
app "production" app "production"
@ -3442,7 +3436,7 @@ module ApplicationTests
end end
test "ParamsWrapper can be turned off" do test "ParamsWrapper can be turned off" do
add_to_config "ActionController::Base.wrap_parameters false" add_to_config "Rails.application.config.action_controller.wrap_parameters_by_default = false"
app "production" app "production"