Skip to content

Raises exception when respond_to called multiple times in incompatible way - #33446

Merged
schneems merged 1 commit into
rails:masterfrom
ptoomey3:nested-respond-to
Jul 31, 2018
Merged

schneems merged 1 commit into
rails:masterfrom
ptoomey3:nested-respond-to

Conversation

@ptoomey3

@ptoomey3 ptoomey3 commented Jul 26, 2018 •

Copy link
Copy Markdown
Contributor

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:

    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. 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.

@gmcgibbon gmcgibbon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍 Please add an entry to actionpack's changelog that describes this change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread actionpack/CHANGELOG.md Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Let me know if I'm not using language that isn't idiomatic with respond_to (or any other changes you would like).

@gmcgibbon gmcgibbon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

💯 One last thing, please squash your commits and then this should be good to go. Thanks for taking the time to fix this!

@schneems

Copy link
Copy Markdown
Member

CI is failing

@ptoomey3

Copy link
Copy Markdown
Contributor Author

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.
@ptoomey3

Copy link
Copy Markdown
Contributor Author

I’ll take a look and see what I must have mistyped when I moved/renames the exception.

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.

@ptoomey3

Copy link
Copy Markdown
Contributor Author

Yay..looks like a re-run of CI was all the was needed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants