Permalink
Browse files

backporting deep_munge

  • Loading branch information...
1 parent d549df7 commit 61eed87ce32caf534bf1f52dd8134097b4ad9e1b @tenderlove tenderlove committed Jan 24, 2013
@@ -491,5 +491,26 @@ def normalize_parameters(value)
value
end
end
+ protected
+
+ # Remove nils from the params hash
+ def deep_munge(hash)
+ hash.each_value do |v|
+ case v
+ when Array
+ v.grep(Hash) { |x| deep_munge(x) }
+ when Hash
+ deep_munge(v)
+ end
+ end
+
+ keys = hash.keys.find_all { |k| hash[k] == [nil] }
+ keys.each { |k| hash[k] = nil }
+ hash
+ end
+
+ def parse_query(qs)
+ deep_munge(super)
+ end
end
end
@@ -81,7 +81,12 @@ def teardown
end
test "query string without equal" do
- assert_parses({ "action" => nil }, "action")
+ assert_parses({"action" => nil}, "action")
+ assert_parses({"action" => {"foo" => nil}}, "action[foo]")
+ assert_parses({"action" => {"foo" => { "bar" => nil }}}, "action[foo][bar]")
+ assert_parses({"action" => {"foo" => { "bar" => nil }}}, "action[foo][bar][]")
+ assert_parses({"action" => {"foo" => nil}}, "action[foo][]")
+ assert_parses({"action"=>{"foo"=>[{"bar"=>nil}]}}, "action[foo][][bar]")
end
test "query string with empty key" do

1 comment on commit 61eed87

Contributor

grosser commented on 61eed87 Jan 28, 2013

Does not seems to work:

# Gemfile
gem "rails", "2.3.16"

# console
Rails.version # 2.3.16

# bash
curl -L -v -X POST "http://localhost:3000/ping/host" -H "Content-Type: application/json" -d '{"xxx":[], "yyy":[null]}'

# PingController
  def host
    raise params.inspect
  end

# server log
 Processing PingController#host (for 127.0.0.1 at 2013-01-28 15:40:08) [POST]
15:40:09 web.1      |   Parameters: {"xxx"=>[], "yyy"=>[nil], "controller"=>"ping", "action"=>"host"}
15:40:09 web.1      | 
15:40:09 web.1      | RuntimeError ({"xxx"=>[], "yyy"=>[nil], "controller"=>"ping", "action"=>"host"}):
15:40:09 web.1      |   app/controllers/ping_controller.rb:27:in `host'
Please sign in to comment.