Skip to content

Commit f4cf0dc

Browse files
authored
Merge pull request rails#52984 from jhawthorn/json_format_options
Avoid passing extraneous options to Renderers like `render json:`
2 parents 2edf657 + 98ad644 commit f4cf0dc

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

actionpack/lib/action_controller/metal/renderers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def _render_to_body_with_renderer(options)
154154
end
155155

156156
add :json do |json, options|
157-
json = json.to_json(options) unless json.kind_of?(String)
157+
json_options = options.except(:callback, :content_type, :status)
158+
json = json.to_json(json_options) unless json.kind_of?(String)
158159

159160
if options[:callback].present?
160161
if media_type.nil? || media_type == Mime[:json]

actionpack/test/controller/render_json_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def to_json(options = {})
1717
end
1818
end
1919

20+
class InspectOptions
21+
def as_json(options = {})
22+
{ options: options }
23+
end
24+
end
25+
2026
class TestController < ActionController::Base
2127
protect_from_forgery
2228

@@ -59,6 +65,10 @@ def render_json_with_extra_options
5965
def render_json_without_options
6066
render json: JsonRenderable.new
6167
end
68+
69+
def render_json_inspect_options
70+
render json: InspectOptions.new
71+
end
6272
end
6373

6474
tests TestController
@@ -124,4 +134,9 @@ def test_render_json_calls_to_json_from_object
124134
get :render_json_without_options
125135
assert_equal '{"a":"b"}', @response.body
126136
end
137+
138+
def test_render_json_avoids_view_options
139+
get :render_json_inspect_options
140+
assert_equal '{"options":{}}', @response.body
141+
end
127142
end

actionview/lib/action_view/layouts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def _implied_layout_name
347347
end
348348
end
349349

350-
def _normalize_options(options) # :nodoc:
350+
def _process_render_template_options(options) # :nodoc:
351351
super
352352

353353
if _include_layout?(options)

actionview/lib/action_view/rendering.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def view_renderer # :nodoc:
118118

119119
def render_to_body(options = {})
120120
_process_options(options)
121+
_process_render_template_options(options)
121122
_render_template(options)
122123
end
123124

@@ -173,8 +174,7 @@ def _normalize_args(action = nil, options = {})
173174
end
174175

175176
# Normalize options.
176-
def _normalize_options(options)
177-
options = super(options)
177+
def _process_render_template_options(options)
178178
if options[:partial] == true
179179
options[:partial] = action_name
180180
end
@@ -184,7 +184,6 @@ def _normalize_options(options)
184184
end
185185

186186
options[:template] ||= (options[:action] || action_name).to_s
187-
options
188187
end
189188
end
190189
end

actionview/test/actionpack/abstract/abstract_controller_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ def self.layout(formats)
212212
end
213213

214214
def render_to_body(options = {})
215-
options[:_layout] = options[:layout] || _default_layout({})
216215
super
217216
end
218217
end

0 commit comments

Comments
 (0)