summary refs log tree commit
path: root/lib/rack/query_parser.rb
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2016-06-23 11:47:12 +0800
committerMatthew Draper <matthew@trebex.net>2016-06-23 12:28:33 +0800
commit100745eeb069578aba2ab18969bfb845e880ab8e (patch)
tree2e240d26fd53c18fdafeb546ee9fd9143020b9fb /lib/rack/query_parser.rb
parent8ebe20c80ffabc7cbf797999e74baeb3315673fa (diff)
downloadrack-100745eeb069578aba2ab18969bfb845e880ab8e.tar.gz
Try harder when deciding whether to add a new array element
Only move to a new entry if the end key is taken; checking only the
top-level key is insufficient.
Diffstat (limited to 'lib/rack/query_parser.rb')
-rw-r--r--lib/rack/query_parser.rb15
1 files changed, 13 insertions, 2 deletions
diff --git a/lib/rack/query_parser.rb b/lib/rack/query_parser.rb
index 17b77128..be74bc06 100644
--- a/lib/rack/query_parser.rb
+++ b/lib/rack/query_parser.rb
@@ -102,8 +102,7 @@ module Rack
         child_key = $1
         params[k] ||= []
         raise ParameterTypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
-        first_key = child_key.gsub(/[\[\]]/, ' ').split.first
-        if params_hash_type?(params[k].last) && !params[k].last.key?(first_key)
+        if params_hash_type?(params[k].last) && !params_hash_has_key?(params[k].last, child_key)
           normalize_params(params[k].last, child_key, v, depth - 1)
         else
           params[k] << normalize_params(make_params, child_key, v, depth - 1)
@@ -135,6 +134,18 @@ module Rack
       obj.kind_of?(@params_class)
     end
 
+    def params_hash_has_key?(hash, key)
+      return false if key =~ /\[\]/
+
+      key.split(/[\[\]]+/).inject(hash) do |h, part|
+        next h if part == ''
+        return false unless params_hash_type?(h) && h.key?(part)
+        h[part]
+      end
+
+      true
+    end
+
     def unescape(s)
       Utils.unescape(s)
     end