Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Params middleware errors when a JSON array is posted #218

Merged
merged 1 commit into from Nov 30, 2012
Jump to file or symbol
Failed to load files and symbols.
+16 −1
Split
@@ -31,7 +31,12 @@ def retrieve_params(env)
when URL_ENCODED then
::Rack::Utils.parse_nested_query(body)
when JSON_ENCODED then
- MultiJson.load(body)
+ json = MultiJson.load(body)
+ if json.is_a?(Hash)
+ json
+ else
+ {'_json' => json}
+ end
else
{}
end
@@ -142,6 +142,16 @@
ret['foo'].should == 'bar'
end
+ it "parses json that does not evaluate to a hash" do
+ @env['CONTENT_TYPE'] = 'application/json'
+ @env['rack.input'] = StringIO.new
+ @env['rack.input'] << %|["foo","bar"]|
+ @env['rack.input'].rewind
+
+ ret = @params.retrieve_params(@env)
+ ret['_json'].should == ['foo', 'bar']
+ end
+
it "handles empty input gracefully on JSON" do
@env['CONTENT_TYPE'] = 'application/json'
@env['rack.input'] = StringIO.new