Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add MIME type "application/jsonrequest" for compliance with JSON spec

#536

Currently Rails only recognizes "application/json" as a valid MIME type for JSON requests, but according to the JSON spec the MIME type should be "application/jsonrequest".

This patch merely adds "application/jsonrequest" as a synonym for "application/json", and also reworks the JSON parsing test slightly so that alternate MIME types can be passed in.

Reported by Mike Subelsky · July 2nd, 2008 @ 04:31 PM

State: resolved
Milestone: 2.x
Assigned to: Rick Rick
Importance: none

Activity

  1. Mike Subelsky
    Mike Subelsky

    For some reason Lighthouse is not upload the patch as an attachment (in Firefox and Safari) so I am pasting it below.

    diff --git a/actionpack/lib/action_controller/mime_types.rb b/actionpack/lib/action_controller/mime_types.rb

    index 01a266d..2d7fba1 100644

    --- a/actionpack/lib/action_controller/mime_types.rb

    +++ b/actionpack/lib/action_controller/mime_types.rb

    @@ -17,4 +17,5 @@ Mime::Type.register "multipart/form-data", :multipart_form

    Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form

    1. http://www.ietf.org/rfc/rfc4627.txt

    -Mime::Type.register "application/json", :json, %w( text/x-json )

    \ No newline at end of file

    +# http://www.json.org/JSONRequest....

    +Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )

    \ No newline at end of file

    diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb

    index 2bd489b..5d677ca 100644

    --- a/actionpack/test/controller/request_test.rb

    +++ b/actionpack/test/controller/request_test.rb

    @@ -909,15 +909,21 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest

    end

    class JsonParamsParsingTest < Test::Unit::TestCase

    • def test_hash_params
    • person = parse_body({:person => {:name => "David"}}.to_json)[:person]
    • def test_hash_params_for_application_json
    • person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person]
    • assert_kind_of Hash, person
    • assert_equal 'David', person['name']
    • end

    +

    • def test_hash_params_for_application_jsonrequest
    • person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person]

    assert_kind_of Hash, person

    assert_equal 'David', person['name']

    end

    private

    • def parse_body(body)
    • env = { 'CONTENT_TYPE' => 'application/json',
    • def parse_body(body, content_type)
    • env = { 'CONTENT_TYPE' => content_type,

    'CONTENT_LENGTH' => body.size.to_s }

    cgi = ActionController::Integration::Session::StubCGI.new(env, body)

    ActionController::CgiRequest.new(cgi).request_parameters

    July 2nd, 2008 @ 04:34 PM

  2. Mike Subelsky
  3. Mike Subelsky
  4. Mike Subelsky
  5. Mike Subelsky
    Mike Subelsky

    Updated patch: http://pastie.org/226332

    (file uploading and @@@ still not working for me, sorry to gum up the works)

    July 2nd, 2008 @ 07:21 PM

  6. Mike Subelsky
    Mike Subelsky
    From 644571c993b9baf3ff73e0b423a924dcbc2ffeef Mon Sep 17 00:00:00 2001
    From: Mike Subelsky <github@mikeshop.net>
    Date: Wed, 2 Jul 2008 14:15:43 -0400
    Subject: [PATCH] Added application/jsonrequest as a synonym for application/json
    
    ---
     actionpack/lib/action_controller/mime_types.rb |    3 ++-
     actionpack/test/controller/request_test.rb     |   14 ++++++++++----
     2 files changed, 12 insertions(+), 5 deletions(-)
    
    diff --git a/actionpack/lib/action_controller/mime_types.rb b/actionpack/lib/action_controller/mime_types.rb
    index 01a266d..2d7fba1 100644
    --- a/actionpack/lib/action_controller/mime_types.rb
    +++ b/actionpack/lib/action_controller/mime_types.rb
    @@ -17,4 +17,5 @@ Mime::Type.register "multipart/form-data", :multipart_form
     Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form
     
     # http://www.ietf.org/rfc/rfc4627.txt
    -Mime::Type.register "application/json", :json, %w( text/x-json )
    \ No newline at end of file
    +# http://www.json.org/JSONRequest....
    +Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )
    \ No newline at end of file
    diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
    index 2bd489b..81794a5 100644
    --- a/actionpack/test/controller/request_test.rb
    +++ b/actionpack/test/controller/request_test.rb
    @@ -909,15 +909,21 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest
     end
     
     class JsonParamsParsingTest < Test::Unit::TestCase
    -  def test_hash_params
    -    person = parse_body({:person => {:name => "David"}}.to_json)[:person]
    +  def test_hash_params_for_application_json
    +    person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person]
    +    assert_kind_of Hash, person
    +    assert_equal 'David', person['name']
    +  end
    +
    +  def test_hash_params_for_application_jsonrequest
    +    person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person]    
         assert_kind_of Hash, person
         assert_equal 'David', person['name']
       end
     
       private
    -    def parse_body(body)
    -      env = { 'CONTENT_TYPE'   => 'application/json',
    +    def parse_body(body,content_type)
    +      env = { 'CONTENT_TYPE'   => content_type,
                   'CONTENT_LENGTH' => body.size.to_s }
           cgi = ActionController::Integration::Session::StubCGI.new(env, body)
           ActionController::CgiRequest.new(cgi).request_parameters
    -- 
    1.5.5.3
    
    

    July 2nd, 2008 @ 07:22 PM

  7. Rick
  8. Rick
    Rick
    • State changed from new to open
    • Assigned user set to Rick
    test_line_offset(RenderTest)
        [./test/controller/render_test.rb:247:in `test_line_offset'
         ./test/../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:91:in `__send__'
         ./test/../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:91:in `run']:
    <"1"> expected but was
    <"75">.
    

    July 2nd, 2008 @ 07:27 PM

  9. Repository
    Repository
    • State changed from open to resolved

    (from [8f640c381d9d1b74f6a0fc3648c21da373661914]) Added application/jsonrequest as a synonym for application/json

    [#536 state:resolved]

    http://github.com/rails/rails/co...

    July 2nd, 2008 @ 07:41 PM

  10. Yehuda Katz
    Yehuda Katz

    The "spec" referred to by the opener of this ticket is not the JSON spec. It is a proposal by Douglas Crockford, that, as far as I know, has not been submitted to any standards body.

    More important are the specs that have actually been accepted by standards bodies, which contradict this "spec".

    RFC 4627 (The application/json Media Type for JavaScript Object Notation) also written by Douglas Crockford, is a registered IANA mime-type.

    The HTTP spec defines the Accept header:

    "The Accept request-header field can be used to specify certain media types which are acceptable for the response."

    application/jsonrequest is not a media type. It is a hack to get around some perceived security issues. Additionally, JSONRequest has not been accepted by any major browser at the current time. Instead, Firefox and Safari are implementing the Access Control spec at the W3C (http://www.w3.org/TR/access-cont... and IE is implementing its own XDR spec (some elucidated at http://code.msdn.microsoft.com/R...).

    In other words, it's fine to add this synonym, but it's frankly not an Accept type that is currently in use, nor part of any accepted spec.

    July 3rd, 2008 @ 08:19 PM

  11. Mike Subelsky
    Mike Subelsky

    Yehuda, that makes a lot of sense. I didn't have a lot of JSON background before starting this project, but I am assuming that if I mistakenly put too much authority into Crockford's document, a few other people after me might as well.

    July 4th, 2008 @ 01:15 PM

  12. Yehuda Katz
    Yehuda Katz

    @mike the beauty is that only browsers have to implement this. Since no browser implemented it, there's no risk of the bizzaro content-type being requested.

    July 4th, 2008 @ 09:45 PM

  13. dawson17
    dawson17
    • Importance changed from to

    Thanks for clarifying these things about mime application/json. It was interesting and helpful.

    November 28th, 2010 @ 11:52 PM