Add MIME type "application/jsonrequest" for compliance with JSON spec
Reported by Mike Subelsky | July 2nd, 2008 @ 04:31 PM | in 2.x
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.
Comments and changes to this ticket
-
Mike Subelsky July 2nd, 2008 @ 04:34 PM
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
-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
-
-
Mike Subelsky July 2nd, 2008 @ 07:19 PM
- no changes were found...
-
-
Mike Subelsky July 2nd, 2008 @ 07:21 PM
Updated patch: http://pastie.org/226332
(file uploading and @@@ still not working for me, sorry to gum up the works)
-
Mike Subelsky July 2nd, 2008 @ 07:22 PM
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 -
-
rick July 2nd, 2008 @ 07:27 PM
- → State changed from new to open
- → Assigned user changed from 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">. -

Repository July 2nd, 2008 @ 07:41 PM
- → State changed from open to resolved
(from [8f640c381d9d1b74f6a0fc3648c21da373661914]) Added application/jsonrequest as a synonym for application/json
[#536 state:resolved]
-

Yehuda Katz July 3rd, 2008 @ 08:19 PM
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.
-
Mike Subelsky July 4th, 2008 @ 01:15 PM
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.
-

Yehuda Katz July 4th, 2008 @ 09:45 PM
@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.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
