Repository navigation
Raises exception when respond_to called multiple times in incompatible way - #33446
Conversation
gmcgibbon
left a comment
There was a problem hiding this comment.
👍 Please add an entry to actionpack's changelog that describes this change.
There was a problem hiding this comment.
Should this error class be moved to a different file? Maybe moving it to actionpack/lib/action_controller/metal/mime_responds.rb would better separate concerns.
There was a problem hiding this comment.
I ended up searching for where other similar exceptions were defined (ex. ActionController::UnknownFormat) and stumbled upon actionpack/lib/action_controller/metal/exceptions.rb. So, I placed the exception there.
There was a problem hiding this comment.
Let me know if I'm not using language that isn't idiomatic with respond_to (or any other changes you would like).
gmcgibbon
left a comment
There was a problem hiding this comment.
💯 One last thing, please squash your commits and then this should be good to go. Thanks for taking the time to fix this!
|
CI is failing |
I’ll take a look and see what I must have mistyped when I moved/renames the exception. |
…e way
Nesting respond_to calls can lead to unexpected behavior, so it should be
avoided. Currently, the first respond_to format match sets the content-type
for the resulting response. But, if a nested respond_to occurs, it is possible
to match on a different format. For example:
respond_to do |outer_type|
outer_type.js do
respond_to do |inner_type|
inner_type.html { render body: "HTML" }
end
end
end
Browsers will often include */* in their Accept headers. In the above example,
such a request would result in the outer_type.js match setting the content-
type of the response to text/javascript, while the inner_type.html match will
cause the actual response to return "HTML".
This change tries to minimize potential breakage by only raising an exception
if the nested respond_to calls are in conflict with each other. So, something
like the following example would not raise an exception:
respond_to do |outer_type|
outer_type.js do
respond_to do |inner_type|
inner_type.js { render body: "JS" }
end
end
end
While the above is nested, it doesn't affect the content-type of the response.
A closer examination makes it look like the failures are not related to my changes. I'll squash the commits, push, and then see if CI passes. |
|
Yay..looks like a re-run of CI was all the was needed. |
Original PR was #27376, but it became stale.
Nesting respond_to calls can lead to unexpected behavior, so it should be
avoided. Currently, the first respond_to format match sets the content-type
for the resulting response. But, if a nested respond_to occurs, it is possible
to match on a different format. For example:
Browsers will often include / in their Accept headers. In the above example,
such a request would result in the outer_type.js match setting the content-
type of the response to text/javascript, while the inner_type.html match will
cause the actual response to return "HTML".
This change tries to minimize potential breakage by only raising an exception
if the nested respond_to calls are in conflict with each other. So, something
like the following example would not raise an exception:
While the above is nested, it doesn't affect the content-type of the response. If people think it simpler/more obvious to just raise anytime a double respond_to occurs (ala a double render) that seems fine too. I'm conjecturing this could be a breaking change for some people, which is why I wanted to minimize breakage by only raising when we are in actual conflict.
/cc @tenderlove - Since we had chatted about "expected behavior" of nested respond_to calls and the general feeling was "raise YugeError" 😄.
/cc @gmcgibbon - Since you had provided feedback in the original PR.