Create an array when multiple instances of the same query parameter are used #519

Closed
wants to merge 1 commit into
from
View
10 lib/rack/utils.rb
@@ -108,7 +108,15 @@ def normalize_params(params, name, v = nil)
return if k.empty?
if after == ""
- params[k] = v
+ if cur = params[k]
+ if cur.class == Array
+ params[k] << v
+ else
+ params[k] = [cur, v]
+ end
+ else
+ params[k] = v
+ end
elsif after == "[]"
params[k] ||= []
raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
View
4 test/spec_utils.rb
@@ -134,7 +134,7 @@ def kcodeu
should.equal "foo" => "\"bar\""
Rack::Utils.parse_nested_query("foo=bar&foo=quux").
- should.equal "foo" => "quux"
+ should.equal "foo" => ["bar", "quux"]
Rack::Utils.parse_nested_query("foo&foo=").
should.equal "foo" => ""
Rack::Utils.parse_nested_query("foo=1&bar=2").
@@ -170,7 +170,7 @@ def kcodeu
Rack::Utils.parse_nested_query("x[y][z][]=1").
should.equal "x" => {"y" => {"z" => ["1"]}}
Rack::Utils.parse_nested_query("x[y][z]=1&x[y][z]=2").
- should.equal "x" => {"y" => {"z" => "2"}}
+ should.equal "x" => {"y" => {"z" => ["1", "2"]}}
Rack::Utils.parse_nested_query("x[y][z][]=1&x[y][z][]=2").
should.equal "x" => {"y" => {"z" => ["1", "2"]}}