-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change ActionDispatch::Response#content_type
returning Content-Type header as it is
#36034
Conversation
See #16018. I'm not sure how we end up with that much code since I opened that PR but it seems we are doing too much work to get the |
In my understanding, this not always the case. Until #35709, require "bundler/inline"
gemfile(true) do
# gem "rails", "6.0.0.rc1"
gem "rails", "5.2.2.1"
end
require "minitest/autorun"
class BugTest < Minitest::Test
def test_content_type
resp = ActionDispatch::Response.new(200, "Content-Type" => "application/fhir+json; fhirVersion=1; charset=utf-8;")
assert_equal "application/fhir+json; fhirVersion=1", resp.content_type
end
end 5.2.2.1
6.0.0
I have doubts whether this was an intentional behavior, but I'm concerned about incompatibilities due to a change in behavior. |
@rafaelfranca so that would mean apps should generally use |
Depends on what they want. If they want all the things they expect in #35709 they should be using |
5ec5adb
to
724b564
Compare
Response#content_type
to the original behaviorActionDispatch::Response#content_type
returning Content-Type header as it is
I updated PR based on #16018. |
… header as it is Since rails#35709, `Response#conten_type` returns only MIME type correctly. It is a documented behavior that this method only returns MIME type, so this change seems appropriate. https://github.com/rails/rails/blob/39de7fac0507070e3c5f8b33fbad6fced84d97ed/actionpack/lib/action_dispatch/http/response.rb#L245-L249 But unfortunately, some users expect this method to return all Content-Type that does not contain charset. This seems to be breaking changes. We can change this behavior with the deprecate cycle. But, in that case, a method needs that include Content-Type with additional parameters. And that method name is probably the `content_type` seems to properly. So I changed the new behavior to more appropriate `media_type` method. And `Response#content_type` changed (as the method name) to return Content-Type header as it is. Fixes rails#35709. [Rafael Mendonça França & Yuuji Yaginuma ]
Change `ActionDispatch::Response#content_type` returning Content-Type header as it is
def content_type | ||
type = super | ||
type&.empty? ? nil : type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is a bit hard to understand. Is it not enough super.presence
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I fixed with 22274d3. Thanks!
@@ -92,11 +92,11 @@ def test_should_render_formatted_xml_erb_template | |||
|
|||
def test_should_render_xml_but_keep_custom_content_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@y-yagi : Should we rename the test description here from content_type
to media_type
? I made the changes but was not sure to raise PR.
At other places, it looked fine to me. Only test desc here should maybe change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is test is to confirm that content_type
takes precedence when specifying render
and content_type
.
render xml: "<blah/>", content_type: "application/atomsvc+xml" |
So I think the current test name is appropriate.
This change just landed into our latest |
Thanks for your feedback. I think your point is correct. I will try to add a config to control the behavior. |
rails/rails#36034 brings this breaking change since Rails 6+ Rails 6.0 release notes: https://github.com/rails/rails/blob/6-0-stable/actionpack/CHANGELOG.md
rails/rails#36034 brings this breaking change since Rails 6+ Rails 6.0 release notes: https://github.com/rails/rails/blob/6-0-stable/actionpack/CHANGELOG.md
I changed return value of `ActionDispatch::Response#content_type` in rails#36034. But this change seems to an obstacle to upgrading. rails#36034 (comment) Therefore, I restored the behavior of `ActionDispatch::Response#content_type` to 5.2 and deprecated old behavior. Also, made it possible to control the behavior with the config.
Before Rails 6 RC2, the `ActionDispatch::Response#content_type` method would return only the media part of the `Content-Type` header, without any other parts. Now the `#content_type` method returns the entire header - as it is - and `#media_type` should be used instead to get the previous behavior. Ref: - rails/rails#36034 - rails/rails#36854
Before Rails 6 RC2, the `ActionDispatch::Response#content_type` method would return only the media part of the `Content-Type` header, without any other parts. Now the `#content_type` method returns the entire header - as it is - and `#media_type` should be used instead to get the previous behavior. Ref: - rails/rails#36034 - rails/rails#36854
* Fix specs on Rails 6 RC2 `ActiveRecord::MigrationContext` now has a `schema_migration` attribute. Ref: https://github.com/rails/rails/pull/36439/files#diff-8d3c44120f7b67ff79e2fbe6a40d0ad6R1018 * Use `media_type` instead of `content_type` Before Rails 6 RC2, the `ActionDispatch::Response#content_type` method would return only the media part of the `Content-Type` header, without any other parts. Now the `#content_type` method returns the entire header - as it is - and `#media_type` should be used instead to get the previous behavior. Ref: - rails/rails#36034 - rails/rails#36854 * Use render template instead of render file Render file will need the full path in order to avoid security breaches. In this particular case, there's no need to use render file, it's ok to use render template. Ref: rails/rails#35688 * Don't set `represent_boolean_as_integer` on Rails 6 * Update comments [ci skip]
Rails 6 changed the usage of content_type. It now includes the complete header in contrary to media_type that only returns the MIME type. See rails/rails#36034
rails/rails#36034 brings this breaking change since Rails 6+ Rails 6.0 release notes: https://github.com/rails/rails/blob/6-0-stable/actionpack/CHANGELOG.md
Change `ActionDispatch::Response#content_type` returning Content-Type header as it is rails/rails#36034
Change `ActionDispatch::Response#content_type` returning Content-Type header as it is rails/rails#36034
Since #35549,
Response#conten_type
returns only MIME type correctly.It is a documented behavior that this method only returns MIME type, so this change seems appropriate.
rails/actionpack/lib/action_dispatch/http/response.rb
Lines 245 to 249 in 39de7fa
But unfortunately, some users expect this method to return all Content-Type that does not contain charset. This seems to be breaking changes.
We can change this behavior with the deprecate cycle.
But, in that case, a method needs that include Content-Type with additional parameters. And that method name is probably the
content_type
seems to properly.So I changed the new behavior to more appropriate another method(
mime_type
) and restoredResponse#conten_type
to the original behavior.The original
conten_type
method seems to have some problems with charset parsing (it implicitly expects charset to be at the end). So I think we should consider the appropriate behavior again later.Fixes #35709.