Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix json params parsing regression for non-object JSON content. #8855

Merged
merged 1 commit into from

3 participants

Dylan Thacker-Smith Jeremy Kemper Simon Mathieu
Dylan Thacker-Smith

Fixes #8845.

request.deep_munge requires a Hash to be passed to it, and the params_parser already wraps non-Hash objects, so it makes sense to just call deep_munge after wrapping the non-Hash object.

Jeremy Kemper
Owner

Looks good. We'll need a CHANGELOG entry and a backport to 3-2-stable as well. Thanks!

Dylan Thacker-Smith dylanahsmith referenced this pull request from a commit in dylanahsmith/rails
Dylan Thacker-Smith dylanahsmith Fix JSON params parsing regression for non-object JSON content.
Backports #8855.
7003b36
Dylan Thacker-Smith

@jeremy added changelog and backport to 3-2-stable.

Dylan Thacker-Smith dylanahsmith referenced this pull request from a commit in dylanahsmith/rails
Dylan Thacker-Smith dylanahsmith Fix JSON params parsing regression for non-object JSON content.
Backports #8855.
c669a9c
Jeremy Kemper
Owner

Thanks @dylanahsmith. Last thing: could you squash to a single commit and force-push? Clean git history :+1:

Dylan Thacker-Smith dylanahsmith referenced this pull request from a commit in dylanahsmith/rails
Dylan Thacker-Smith dylanahsmith Fix JSON params parsing regression for non-object JSON content.
Backports #8855.
eede4ab
Jeremy Kemper
Owner

:metal:

Jeremy Kemper jeremy merged commit 92fada9 into from
Simon Mathieu

I think you need to add

request.body.rewind if request.body.respond_to?(:rewind)

since ActiveSupport::JSON.decode is going to call request.body.read (at least when using unicorn). If you do not, accessing request.body.read from a controller action will return an empty string.

Bernard Potocki imanel referenced this pull request
Closed

Remove deep munge #13157

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
4 actionpack/CHANGELOG.md
View
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Fixed json params parsing regression for non-object JSON content.
+
+ *Dylan Smith*
+
* Extract `ActionDispatch::PerformanceTest` into https://github.com/rails/rails-perftest
You can add the gem to your Gemfile to keep using performance tests.
4 actionpack/lib/action_dispatch/middleware/params_parser.rb
View
@@ -50,9 +50,9 @@ def parse_formatted_parameters(env)
data = request.deep_munge(Hash.from_xml(request.body.read) || {})
data.with_indifferent_access
when :json
- data = request.deep_munge ActiveSupport::JSON.decode(request.body)
+ data = ActiveSupport::JSON.decode(request.body)
data = {:_json => data} unless data.is_a?(Hash)
- data.with_indifferent_access
+ request.deep_munge(data).with_indifferent_access
else
false
end
7 actionpack/test/dispatch/request/json_params_parsing_test.rb
View
@@ -122,6 +122,13 @@ def teardown
)
end
+ test "parses json with non-object JSON content" do
+ assert_parses(
+ {"user" => {"_json" => "string content" }, "_json" => "string content" },
+ "\"string content\"", { 'CONTENT_TYPE' => 'application/json' }
+ )
+ end
+
private
def assert_parses(expected, actual, headers = {})
with_test_routing(UsersController) do
Something went wrong with that request. Please try again.