Skip to content

format.any {} changed behaviour in 4.0.4 #14462

Closed
yob opened this Issue Mar 24, 2014 · 1 comment

3 participants

@yob
yob commented Mar 24, 2014

We have a controller action that looks something like this:

def foo
  respond_to do |format|
    format.json { head :ok }
    format.any {                        
      render 'result', :formats => [:html]
    }
  end
end

The intention is to return a 200 with no content for JSON requests, and return an HTML page for all other requests (/controller/foo, /controller/foo.html, /controller/foo.php, /controller/foo.anything, etc).

In rails 4.0.3, it worked as intended.

In rails 4.0.4, the behaviour changed.

requests to recognosed mime types (/controller/foo.html, /controller/foo.xml, etc) still render the HTML output, but requests to unrecognised formats ( /controller/foo.php, /controller/foo.anything ) now raise a ActionController::UnknownFormat exception.

It looks like this change was introduced with 56962ba.

In 4.0.4, lib/action_dispatch/http/mime_negotiation.rb:57 calls Mime[parameters[:format]] which returns nil, so @env["action_dispatch.request.formats"] is set to an empty array. which means line 108 loops over an empty array and is a no-op. If I modify @env["action_dispatch.request.formats"] to include a Mime::NullType.instance, then the old behaviour is restored.

@rafaelfranca rafaelfranca added this to the 4.0.5 milestone Mar 24, 2014
@carlosantoniodasilva
Ruby on Rails member

Thanks for reporting, we'll be taking a look.

@rafaelfranca rafaelfranca added a commit that referenced this issue Apr 14, 2014
@rafaelfranca rafaelfranca Return null type format when format is not know
When requesting a controller with the following code with a unknown format:

    def my_action
      respond_to do |format|
        format.json { head :ok }
        format.any { render text: 'Default response' }
      end
    end

we should render the default response instead of raising ActionController::UnknownFormat

Fixes #14462
75509d1
@rafaelfranca rafaelfranca added a commit that referenced this issue Apr 14, 2014
@rafaelfranca rafaelfranca Return null type format when format is not know
When requesting a controller with the following code with a unknown format:

    def my_action
      respond_to do |format|
        format.json { head :ok }
        format.any { render text: 'Default response' }
      end
    end

we should render the default response instead of raising ActionController::UnknownFormat

Fixes #14462

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/test/controller/mime/respond_with_test.rb
320124f
@rafaelfranca rafaelfranca added a commit that closed this issue Apr 14, 2014
@rafaelfranca rafaelfranca Return null type format when format is not know
When requesting a controller with the following code with a unknown format:

    def my_action
      respond_to do |format|
        format.json { head :ok }
        format.any { render text: 'Default response' }
      end
    end

we should render the default response instead of raising ActionController::UnknownFormat

Fixes #14462

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/test/controller/mime/respond_with_test.rb

Conflicts:
	actionpack/CHANGELOG.md
4d21e49
@ttosch ttosch pushed a commit that referenced this issue Jan 19, 2015
@rafaelfranca rafaelfranca Return null type format when format is not know
When requesting a controller with the following code with a unknown format:

    def my_action
      respond_to do |format|
        format.json { head :ok }
        format.any { render text: 'Default response' }
      end
    end

we should render the default response instead of raising ActionController::UnknownFormat

Fixes #14462

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/test/controller/mime/respond_with_test.rb
7b2d3f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.