Skip to content

Override Rack::Request#media_type instead of #content_type#16018

Closed
rafaelfranca wants to merge 1 commit intorails:masterfrom
rafaelfranca:rm-free-rack
Closed

Override Rack::Request#media_type instead of #content_type#16018
rafaelfranca wants to merge 1 commit intorails:masterfrom
rafaelfranca:rm-free-rack

Conversation

@rafaelfranca
Copy link
Member

Rack::Request#content_type is used by a lot of Rack::Request methods like #media_type_params and #content_charset. All these methods expect #content_type to be the full env['CONTENT_TYPE'] header.

Since we are overriding #content_type to return only the Mime::Type.to_s these methods stop to work since no information about media type parameters are returned on #content_type.

These is a subtle change of behaviour here since #content_type will return more information than before so I'm not sure we should apply this patch without a proper deprecation path pointing people to use media_type when expecting only the media type.

@jeremy @tenderlove let me know what you think about this change of behaviour.

@rafaelfranca
Copy link
Member Author

Ah, before this change:

>> stub_request('CONTENT_TYPE' => 'application/xml; charset=UTF-8').content_charset
# => nil
>> stub_request('CONTENT_TYPE' => 'application/xml; charset=UTF-8').media_type_params
# => {}

Rack::Request#content_type is used by a lot of Rack::Request methods
like #media_type_params and #content_charset. All these methods
expect #content_type to be the full env['CONTENT_TYPE'] header.

Since we are overriding #content_type to return only the Mime::Type.to_s
these methods stop to work since no information about media type
parameters are returned on #content_type.
@josevalim
Copy link
Contributor

👍

1 similar comment
@tenderlove
Copy link
Member

👍

@jeremy
Copy link
Member

jeremy commented Jul 2, 2014

Agreed in theory, but deprecation is impossible for a return-value behavior change like this. It'll cause many subtle bugs in engines, apps, etc.

May be better to find a new way forward, like providing a ContentTypeHeader object that with params, charset, mime_type, etc. And patch the other Request methods that rely on content_type returning the full header to use an internal content_type_header.

@rafaelfranca
Copy link
Member Author

Not sure if it is worth to patch the Request method, this will only become a endless race for rack compatibility. If they add a new method using content_type we will have to override it here again. The best thing to do in my opinion is stop to override this rack method.

Maybe in Rails 5 we can merge this patch as it is?

@rafaelfranca
Copy link
Member Author

Giving up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants