Skip to content

Commit b1f6cf7

Browse files
committed
Merge pull request rails#4 from jmbejar/rails-api
Rails api - Wrap parameters
2 parents 2686beb + e18d447 commit b1f6cf7

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

actionpack/lib/action_controller/api.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ def self.without_modules(*modules)
128128

129129
# Add instrumentations hooks at the bottom, to ensure they instrument
130130
# all the methods properly.
131-
Instrumentation
131+
Instrumentation,
132+
133+
# Params wrapper should come before instrumentation so they are
134+
# properly showed in logs
135+
ParamsWrapper
132136
]
133137

134138
MODULES.each do |mod|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'abstract_unit'
2+
3+
class ParamsWrapperForApiTest < ActionController::TestCase
4+
class UsersController < ActionController::API
5+
attr_accessor :last_parameters
6+
7+
wrap_parameters :person, format: [:json]
8+
9+
def test
10+
self.last_parameters = params.except(:controller, :action)
11+
head :ok
12+
end
13+
end
14+
15+
class Person; end
16+
17+
tests UsersController
18+
19+
def test_specify_wrapper_name
20+
@request.env['CONTENT_TYPE'] = 'application/json'
21+
post :test, params: { 'username' => 'sikachu' }
22+
23+
expected = { 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }}
24+
assert_equal expected, @controller.last_parameters
25+
end
26+
end

railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# Be sure to restart your server when you modify this file.
22

3-
<%- unless options[:api] -%>
43
# This file contains settings for ActionController::ParamsWrapper which
54
# is enabled by default.
65

76
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
87
ActiveSupport.on_load(:action_controller) do
9-
wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
8+
wrap_parameters format: [:json]
109
end
11-
<%- end -%>
1210
<%- unless options.skip_active_record? -%>
1311

1412
# To enable root element in JSON for ActiveRecord objects.

railties/test/generators/api_app_generator_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ def test_api_modified_files
4747

4848
assert_file "config/initializers/cors.rb"
4949

50-
assert_file "config/initializers/wrap_parameters.rb" do |content|
51-
assert_no_match(/wrap_parameters/, content)
52-
end
50+
assert_file "config/initializers/wrap_parameters.rb"
5351

5452
assert_file "app/controllers/application_controller.rb", /ActionController::API/
5553
end

0 commit comments

Comments
 (0)