Skip to content

Commit

Permalink
Move the parameter wrapper code to the ActionController::Railtie class
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Sep 17, 2021
1 parent 1b0c914 commit 4db4207
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
10 changes: 7 additions & 3 deletions actionpack/lib/action_controller/railtie.rb
Expand Up @@ -12,6 +12,7 @@ class Railtie < Rails::Railtie # :nodoc:
config.action_controller = ActiveSupport::OrderedOptions.new
config.action_controller.raise_on_open_redirects = false
config.action_controller.log_query_tags_around_actions = true
config.action_controller.wrap_parameters_by_default = false

config.eager_load_namespaces << ActionController

Expand Down Expand Up @@ -62,15 +63,18 @@ class Railtie < Rails::Railtie # :nodoc:
extend ::AbstractController::Railties::RoutesHelpers.with(app.routes)
extend ::ActionController::Railties::Helpers

wrap_parameters format: [:json] if options.wrap_parameters_by_default

# Configs used in other initializers
options = options.except(
filtered_options = options.except(
:log_query_tags_around_actions,
:permit_all_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}="
if respond_to?(k)
send(k, v)
Expand Down
8 changes: 7 additions & 1 deletion guides/source/configuring.md
Expand Up @@ -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
updated via an `around_filter`. The default value is `true`.

#### `config.action_controller.wrap_parameters_by_default`

This comment has been minimized.

Copy link
@ghiculescu

ghiculescu Sep 17, 2021

Member

should the next item (ActionController::Base.wrap_parameters on line 897) be removed?


Configures the [`ParamsWrapper`](https://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html) to wrap json
request by default.

#### `ActionController::Base.wrap_parameters`

Configures the [`ParamsWrapper`](https://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html). This can be called at
Expand Down Expand Up @@ -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_record.verify_foreign_keys_for_fixtures`: `true`
- `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:

Expand Down Expand Up @@ -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.active_storage.video_preview_arguments`: `"-y -vframes 1 -f image2"`
- `config.active_storage.variant_processor`: `:mini_magick`
- `config.action_controller.wrap_parameters_by_default`: `false`

### Configuring a Database

Expand Down
6 changes: 1 addition & 5 deletions railties/lib/rails/application/configuration.rb
Expand Up @@ -238,11 +238,7 @@ def load_defaults(target_version)
if respond_to?(:action_controller)
action_controller.raise_on_open_redirects = true

# This can't use the standard configuration pattern because you can call
# wrap_parameters at the top level or on any controller.
ActiveSupport.on_load(:action_controller) do
ActionController::Base.wrap_parameters format: [:json]
end
action_controller.wrap_parameters_by_default = true
end
else
raise "Unknown version #{target_version.to_s.inspect}"
Expand Down
Expand Up @@ -86,5 +86,5 @@

# 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.
# To disable parameter wrapping entirely, call `ActionController::Base.wrap_parameters false` in an initializer.
# ActionController::Base.wrap_parameters format: [:json]
# To disable parameter wrapping entirely, set this config to `false`.
# Rails.application.config.action_controller.wrap_parameters_by_default = true
16 changes: 5 additions & 11 deletions railties/test/application/configuration_test.rb
Expand Up @@ -3415,25 +3415,19 @@ def new(app); self; end
add_to_config 'config.load_defaults "6.1"'

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

app "production"

assert_equal [:json], ActionController::Base._wrapper_options.format
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
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

app "production"
Expand All @@ -3442,7 +3436,7 @@ def new(app); self; end
end

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"

Expand Down

0 comments on commit 4db4207

Please sign in to comment.