Skip to content
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

if format is unknown NullMimeTypeObject is returned #8085

Merged
merged 1 commit into from Dec 22, 2012

Conversation

acapilleri
Copy link
Contributor

If a unknown format is passed in a request, the methods html?, xml?, json? ...etc
Nil Exception.

This patch add a class NullMimeTypeObject, that is returned when request.format is unknown
and it responds false to the methods that ends with '?'.

It refers to #7837, it's considered a improvement not a bug.

@acapilleri
Copy link
Contributor Author

@guilleiguaran I update the PR, it fails here

assert_equal request.format.html?, false
assert_equal request.format.xml?, false
assert_equal request.format.json?, false
assert !request.format.html?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assert ! in the other lines, the last one can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I test that the request.format.html? is equal to false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important that it returns a falsy value, that evaluates to false, so !request.format.foo? should suffice I believe.

@guilleiguaran
Copy link
Member

@carlosantoniodasilva @rails reviewers, this isn't ready for merge yet, I asked for the PR to continue working with @acapilleri in the solution and try to get everything green :)

@carlosantoniodasilva
Copy link
Member

@guilleiguaran @acapilleri ok!

@acapilleri
Copy link
Contributor Author

@guilleiguaran any news?

@steveklabnik
Copy link
Member

This is now out of date, and will need rebased.

@carlosantoniodasilva @guilleiguaran now that everything is green, is this good to go once rebased?

@acapilleri
Copy link
Contributor Author

@steveklabnik in the current PR test_send_file_headers_with_bad_symbol is the only red, but it could be delete because with this PR, `this_type_is_not_registeredshould not raiseArgumentError`` . I'm waiting for a confirm and I will update the PR

@acapilleri
Copy link
Contributor Author

@guilleiguaran any thought?

@guilleiguaran
Copy link
Member

@acapilleri hey, thanks for ping me, I will ping to @jeremy to have some extra eyes in this one before of merge it 😄

@acapilleri
Copy link
Contributor Author

@guilleiguaran 😄, I think that is important my last comment to @steveklabnik, where I explain my doubt,
anyway thanks! Ping me when you will have a feedback and I will update

@pixeltrix
Copy link
Contributor

@acapilleri I think the NullMimeTypeObject class name is a bit unwieldy - any reason we can't just use NullType since it's namespaced inside of Mime anyway. @guilleiguaran what do you think? Also do we want to return true for nil?.

@acapilleri
Copy link
Contributor Author

@pixeltrix Thanks for you code review, what class name do you think could be fine?

@pixeltrix
Copy link
Contributor

@acapilleri Mime::NullType should be fine. The reason why I think it should return true for nil? is code like this won't break:

if request.format.nil?
  # do stuff
end

Ideally the NullType object would act like NilClass unfortunately that appears to be hardwired into MRI.

@guilleiguaran
Copy link
Member

@pixeltrix right, I tried it already to fix the issue associated to this PR, isn't possible create a object that acts like NilClass.

@acapilleri Please rebase this and update the name to Mime::NullType and add nil? method. I'll merge this if no one have more concerns about it.

@acapilleri
Copy link
Contributor Author

@guilleiguaran done.

@steveklabnik
Copy link
Member

It still doesn't merge cleanly.

If a request has an unknown format, the methods html?, xml?, json? ...etc
not raise an Exception.

This patch add a class Mime::NullType, that is returned when  request.format is unknown
and it responds false to the methods that ends with '?' and true to 'nil?'.

It refers to rails#7837, this issue is considered a improvement not a bug.
@acapilleri
Copy link
Contributor Author

updated thanks.

guilleiguaran added a commit that referenced this pull request Dec 22, 2012
if format is unknown NullMimeTypeObject is returned
@guilleiguaran guilleiguaran merged commit cba0588 into rails:master Dec 22, 2012
@guilleiguaran
Copy link
Member

Thanks for your contribution!!!

acapilleri added a commit to acapilleri/rails that referenced this pull request Nov 29, 2013
This PR fixes rails#13064 regression bug introduced by the rails#8085

Ideally the NullType object would act like NilClass unfortunately
that appears to be hardwired into MRI, as we know nil.to_a -> []
acapilleri added a commit to acapilleri/rails that referenced this pull request Dec 2, 2013
This PR fixes rails#13064 regression bug introduced by the rails#8085

Now in _process_format when the format is a Mime::NullType nothing is written in self.content_type.
In this way the method Response#assign_default_content_type_and_charset can
write the the default mime_type.
acapilleri added a commit to acapilleri/rails that referenced this pull request Dec 3, 2013
This PR fixes rails#13064 regression bug introduced by the rails#8085

Now in _process_format when the format is a Mime::NullType nothing is written in self.content_type.
In this way the method Response#assign_default_content_type_and_charset can
write the the default mime_type.
acapilleri added a commit to acapilleri/rails that referenced this pull request Dec 3, 2013
This PR fixes rails#13064 regression bug introduced by the rails#8085

Now in _process_format when the format is a Mime::NullType nothing is written in self.content_type.
In this way the method Response#assign_default_content_type_and_charset can
write the the default mime_type.
acapilleri added a commit to acapilleri/rails that referenced this pull request Dec 4, 2013
This PR is the backport of rails#13141.
It fixes rails#13064 regression bug introduced by the rails#8085.
carlosantoniodasilva added a commit to carlosantoniodasilva/rails that referenced this pull request Dec 19, 2013
TLDR: always return an object that responds to the query methods from
request.format, and do not touch Mime::Type[] lookup to avoid bugs.

---

Long version:

The initial issue was about being able to do checks like
request.format.html? for request with an unknown format, where
request.format would be nil.

This is where the issue came from at first in rails#7837 and rails#8085
(merged in cba0588), but the
implementation went down the path of adding this to the mime type
lookup logic.

This unfortunately introduced subtle bugs, for instance in the merged
commit a test related to send_file had to be changed to accomodate the
introduction of the NullType.

Later another bug was found in rails#13064, related to the content-type being
shown as #<Mime::NullType:...> for templates with localized extensions
but no format included. This one was fixed in rails#13133, merged in
43962d6.

Besides that, custom handlers were not receiving the proper template
formats anymore when passing through the rendering process, because of
the NullType addition. That was found while migrating an application
from 3.2 to 4.0 that uses the Markerb gem (a custom handler that
generates both text and html emails from a markdown template).

---

This changes the implementation moving away from returning this null
object from the mime lookup, and still fixes the initial issue where
request.format.zomg? would raise an exception for unknown formats due to
request.format being nil.
carlosantoniodasilva added a commit to carlosantoniodasilva/rails that referenced this pull request Dec 20, 2013
TLDR: always return an object that responds to the query methods from
request.format, and do not touch Mime::Type[] lookup to avoid bugs.

---

Long version:

The initial issue was about being able to do checks like
request.format.html? for request with an unknown format, where
request.format would be nil.

This is where the issue came from at first in rails#7837 and rails#8085
(merged in cba0588), but the
implementation went down the path of adding this to the mime type
lookup logic.

This unfortunately introduced subtle bugs, for instance in the merged
commit a test related to send_file had to be changed to accomodate the
introduction of the NullType.

Later another bug was found in rails#13064, related to the content-type being
shown as #<Mime::NullType:...> for templates with localized extensions
but no format included. This one was fixed in rails#13133, merged in
43962d6.

Besides that, custom handlers were not receiving the proper template
formats anymore when passing through the rendering process, because of
the NullType addition. That was found while migrating an application
from 3.2 to 4.0 that uses the Markerb gem (a custom handler that
generates both text and html emails from a markdown template).

---

This changes the implementation moving away from returning this null
object from the mime lookup, and still fixes the initial issue where
request.format.zomg? would raise an exception for unknown formats due to
request.format being nil.

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/lib/abstract_controller/rendering.rb
	actionpack/lib/action_controller/metal/rendering.rb
carlosantoniodasilva added a commit that referenced this pull request Dec 23, 2013
TLDR: always return an object that responds to the query methods from
request.format, and do not touch Mime::Type[] lookup to avoid bugs.

---

Long version:

The initial issue was about being able to do checks like
request.format.html? for request with an unknown format, where
request.format would be nil.

This is where the issue came from at first in #7837 and #8085
(merged in cba0588), but the
implementation went down the path of adding this to the mime type
lookup logic.

This unfortunately introduced subtle bugs, for instance in the merged
commit a test related to send_file had to be changed to accomodate the
introduction of the NullType.

Later another bug was found in #13064, related to the content-type being
shown as #<Mime::NullType:...> for templates with localized extensions
but no format included. This one was fixed in #13133, merged in
43962d6.

Besides that, custom handlers were not receiving the proper template
formats anymore when passing through the rendering process, because of
the NullType addition. That was found while migrating an application
from 3.2 to 4.0 that uses the Markerb gem (a custom handler that
generates both text and html emails from a markdown template).

---

This changes the implementation moving away from returning this null
object from the mime lookup, and still fixes the initial issue where
request.format.zomg? would raise an exception for unknown formats due to
request.format being nil.
carlosantoniodasilva added a commit that referenced this pull request Dec 23, 2013
TLDR: always return an object that responds to the query methods from
request.format, and do not touch Mime::Type[] lookup to avoid bugs.

---

Long version:

The initial issue was about being able to do checks like
request.format.html? for request with an unknown format, where
request.format would be nil.

This is where the issue came from at first in #7837 and #8085
(merged in cba0588), but the
implementation went down the path of adding this to the mime type
lookup logic.

This unfortunately introduced subtle bugs, for instance in the merged
commit a test related to send_file had to be changed to accomodate the
introduction of the NullType.

Later another bug was found in #13064, related to the content-type being
shown as #<Mime::NullType:...> for templates with localized extensions
but no format included. This one was fixed in #13133, merged in
43962d6.

Besides that, custom handlers were not receiving the proper template
formats anymore when passing through the rendering process, because of
the NullType addition. That was found while migrating an application
from 3.2 to 4.0 that uses the Markerb gem (a custom handler that
generates both text and html emails from a markdown template).

---

This changes the implementation moving away from returning this null
object from the mime lookup, and still fixes the initial issue where
request.format.zomg? would raise an exception for unknown formats due to
request.format being nil.

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/lib/abstract_controller/rendering.rb
	actionpack/lib/action_controller/metal/rendering.rb
tienn-zz pushed a commit to square/rails that referenced this pull request Jul 2, 2014
TLDR: always return an object that responds to the query methods from
request.format, and do not touch Mime::Type[] lookup to avoid bugs.

---

Long version:

The initial issue was about being able to do checks like
request.format.html? for request with an unknown format, where
request.format would be nil.

This is where the issue came from at first in rails#7837 and rails#8085
(merged in cba0588), but the
implementation went down the path of adding this to the mime type
lookup logic.

This unfortunately introduced subtle bugs, for instance in the merged
commit a test related to send_file had to be changed to accomodate the
introduction of the NullType.

Later another bug was found in rails#13064, related to the content-type being
shown as #<Mime::NullType:...> for templates with localized extensions
but no format included. This one was fixed in rails#13133, merged in
43962d6.

Besides that, custom handlers were not receiving the proper template
formats anymore when passing through the rendering process, because of
the NullType addition. That was found while migrating an application
from 3.2 to 4.0 that uses the Markerb gem (a custom handler that
generates both text and html emails from a markdown template).

---

This changes the implementation moving away from returning this null
object from the mime lookup, and still fixes the initial issue where
request.format.zomg? would raise an exception for unknown formats due to
request.format being nil.

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/lib/abstract_controller/rendering.rb
	actionpack/lib/action_controller/metal/rendering.rb
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.

None yet

6 participants