mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #37617 from Edouard-chin/ec-respond-to-contenttype
Modify respond_to behaviour always setting the response's content type based on the request format
This commit is contained in:
commit
4fe767535d
4 changed files with 69 additions and 1 deletions
|
@ -1,3 +1,23 @@
|
|||
* `respond_to#any` no longer returns a response's Content-Type based on the
|
||||
request format but based on the block given.
|
||||
|
||||
Example:
|
||||
|
||||
```ruby
|
||||
def my_action
|
||||
respond_to do |format|
|
||||
format.any { render(json: { foo: 'bar' }) }
|
||||
end
|
||||
end
|
||||
|
||||
get('my_action.csv')
|
||||
```
|
||||
The previous behaviour was to respond with a `text/csv` Content-Type which
|
||||
is inaccurate since a JSON response is being rendered.
|
||||
Now it correctly returns a `application/json` Content-Type.
|
||||
|
||||
* Edouard Chin*
|
||||
|
||||
* Replaces (back)slashes in failure screenshot image paths with dashes.
|
||||
|
||||
If a failed test case contained a slash or a backslash, a screenshot would be created in a
|
||||
|
|
|
@ -209,7 +209,7 @@ module ActionController #:nodoc:
|
|||
raise ActionController::RespondToMismatchError
|
||||
end
|
||||
_process_format(format)
|
||||
_set_rendered_content_type format
|
||||
_set_rendered_content_type(format) unless collector.any_response?
|
||||
response = collector.response
|
||||
response.call if response
|
||||
else
|
||||
|
@ -268,6 +268,10 @@ module ActionController #:nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
def any_response?
|
||||
!@responses.fetch(format, false) && @responses[Mime::ALL]
|
||||
end
|
||||
|
||||
def response
|
||||
response = @responses.fetch(format, @responses[Mime::ALL])
|
||||
if response.is_a?(VariantCollector) # `format.html.phone` - variant inline syntax
|
||||
|
|
|
@ -157,6 +157,13 @@ class RespondToController < ActionController::Base
|
|||
end
|
||||
end
|
||||
|
||||
def handle_any_doesnt_set_request_content_type
|
||||
respond_to do |type|
|
||||
type.html { render body: "HTML" }
|
||||
type.any { render json: { foo: "bar" } }
|
||||
end
|
||||
end
|
||||
|
||||
def handle_any_any
|
||||
respond_to do |type|
|
||||
type.html { render body: "HTML" }
|
||||
|
@ -549,6 +556,12 @@ class RespondToControllerTest < ActionController::TestCase
|
|||
assert_equal "Either JS or XML", @response.body
|
||||
end
|
||||
|
||||
def test_handle_any_doesnt_set_request_content_type
|
||||
@request.accept = "text/csv"
|
||||
get :handle_any_doesnt_set_request_content_type
|
||||
assert_equal "application/json", @response.media_type
|
||||
end
|
||||
|
||||
def test_handle_any_any
|
||||
@request.accept = "*/*"
|
||||
get :handle_any_any
|
||||
|
|
|
@ -72,6 +72,37 @@ The new Rails version might have different configuration defaults than the previ
|
|||
|
||||
To allow you to upgrade to new defaults one by one, the update task has created a file `config/initializers/new_framework_defaults.rb`. Once your application is ready to run with new defaults, you can remove this file and flip the `config.load_defaults` value.
|
||||
|
||||
Upgrading from Rails 6.0 to Rails 6.1
|
||||
-------------------------------------
|
||||
|
||||
### Response's Content-Type when using `respond_to#any`
|
||||
|
||||
The Content-Type header returned in the response can differ from what Rails 6.0 returned,
|
||||
more specifically if your application uses `respond_to { |format| format.any }`.
|
||||
The Content-Type will now be based on the given block rather than the request's format.
|
||||
|
||||
Example:
|
||||
|
||||
```ruby
|
||||
def my_action
|
||||
respond_to do |format|
|
||||
format.any { render(json: { foo: 'bar' }) }
|
||||
end
|
||||
end
|
||||
|
||||
get('my_action.csv')
|
||||
```
|
||||
|
||||
Previous behaviour was returning a `text/csv` response's Content-Type which is inaccurate since a JSON response is being rendered.
|
||||
Current behaviour correctly returns a `application/json` response's Content-Type.
|
||||
|
||||
If your application relies on the previous incorrect behaviour, you are encouraged to specify
|
||||
which formats your action accepts, i.e.
|
||||
|
||||
```ruby
|
||||
format.any(:xml, :json) { render request.format.to_sym => @people }
|
||||
```
|
||||
|
||||
|
||||
Upgrading from Rails 5.2 to Rails 6.0
|
||||
-------------------------------------
|
||||
|
|
Loading…
Reference in a new issue