From 93d1de7dd8d5cd0429eddec61d6dc26cf0d8ec9b Mon Sep 17 00:00:00 2001 From: Max Albrecht <1@178.is> Date: Mon, 21 Sep 2015 20:19:49 +0200 Subject: add failing test for rack/rack#951 --- test/spec_utils.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/spec_utils.rb b/test/spec_utils.rb index 17a12115..780aa47a 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -206,6 +206,11 @@ describe Rack::Utils do Rack::Utils.parse_nested_query("x[y][][z]=1&x[y][][w]=a&x[y][][z]=2&x[y][][w]=3"). must_equal "x" => {"y" => [{"z" => "1", "w" => "a"}, {"z" => "2", "w" => "3"}]} + Rack::Utils.parse_nested_query("x[][y]=1&x[][z][w]=a&x[][y]=2&x[][z][w]=b"). + must_equal "x" => [{"y" => "1", "z" => {"w" => "a"}}, {"y" => "2", "z" => {"w" => "b"}}] + Rack::Utils.parse_nested_query("x[][z][w]=a&x[][y]=1&x[][z][w]=b&x[][y]=2"). + must_equal "x" => [{"y" => "1", "z" => {"w" => "a"}}, {"y" => "2", "z" => {"w" => "b"}}] + lambda { Rack::Utils.parse_nested_query("x[y]=1&x[y]z=2") }. must_raise(Rack::Utils::ParameterTypeError). message.must_equal "expected Hash (got String) for param `y'" -- cgit v1.2.3-24-ge0c7 From 7f00781a7b9c75ddb86d0ab52132885ee88d5bac Mon Sep 17 00:00:00 2001 From: Graham Conzett Date: Mon, 19 Oct 2015 13:53:34 -0400 Subject: Fix normalize_params parsing arrays of hashes Account for child_key being a key representing a nested hash Closes rack/rack#951 --- lib/rack/query_parser.rb | 3 ++- test/spec_utils.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/rack/query_parser.rb b/lib/rack/query_parser.rb index 72a521f3..1578ca55 100644 --- a/lib/rack/query_parser.rb +++ b/lib/rack/query_parser.rb @@ -96,7 +96,8 @@ 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) - if params_hash_type?(params[k].last) && !params[k].last.key?(child_key) + first_key = child_key.gsub(/[\[\]]/, ' ').split.first + if params_hash_type?(params[k].last) && !params[k].last.key?(first_key) normalize_params(params[k].last, child_key, v, depth - 1) else params[k] << normalize_params(make_params, child_key, v, depth - 1) diff --git a/test/spec_utils.rb b/test/spec_utils.rb index 780aa47a..6750932f 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -311,7 +311,7 @@ describe Rack::Utils do message.must_equal "value must be a Hash" end - should 'perform the inverse function of #parse_nested_query' do + it 'perform the inverse function of #parse_nested_query' do [{"foo" => nil, "bar" => ""}, {"foo" => "bar", "baz" => ""}, {"foo" => ["1", "2"]}, -- cgit v1.2.3-24-ge0c7 From b1bf5a35409a3c6da2f1b7c2e1e41c9b842c0fe7 Mon Sep 17 00:00:00 2001 From: Graham Conzett Date: Fri, 12 Feb 2016 15:21:12 -0500 Subject: Add more failing specs for ararys --- test/spec_utils.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/spec_utils.rb b/test/spec_utils.rb index 6750932f..9601cbaf 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -305,13 +305,15 @@ describe Rack::Utils do must_equal 'x[y][][z]=1&x[y][][z]=2' Rack::Utils.build_nested_query('x' => { 'y' => [{ 'z' => '1', 'w' => 'a' }, { 'z' => '2', 'w' => '3' }] }). must_equal 'x[y][][z]=1&x[y][][w]=a&x[y][][z]=2&x[y][][w]=3' + Rack::Utils.build_nested_query({"foo" => ["1", ["2"]]}). + must_equal 'foo[]=1&foo[][]=2' lambda { Rack::Utils.build_nested_query("foo=bar") }. must_raise(ArgumentError). message.must_equal "value must be a Hash" end - it 'perform the inverse function of #parse_nested_query' do + it 'performs the inverse function of #parse_nested_query' do [{"foo" => nil, "bar" => ""}, {"foo" => "bar", "baz" => ""}, {"foo" => ["1", "2"]}, @@ -328,7 +330,8 @@ describe Rack::Utils do {"x" => {"y" => [{"v" => {"w" => "1"}}]}}, {"x" => {"y" => [{"z" => "1", "v" => {"w" => "2"}}]}}, {"x" => {"y" => [{"z" => "1"}, {"z" => "2"}]}}, - {"x" => {"y" => [{"z" => "1", "w" => "a"}, {"z" => "2", "w" => "3"}]}} + {"x" => {"y" => [{"z" => "1", "w" => "a"}, {"z" => "2", "w" => "3"}]}}, + {"foo" => ["1", ["2"]]}, ].each { |params| qs = Rack::Utils.build_nested_query(params) Rack::Utils.parse_nested_query(qs).must_equal params -- cgit v1.2.3-24-ge0c7 From a8a908f1ab62f279eb18d3869c3433eb67037e0a Mon Sep 17 00:00:00 2001 From: "Ryan T. Hosford" Date: Fri, 4 Mar 2016 20:04:40 -0600 Subject: Fixes #1015 - Handles the edge case - Adds a test for #1015 --- lib/rack/query_parser.rb | 8 +++++++- test/spec_utils.rb | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/rack/query_parser.rb b/lib/rack/query_parser.rb index 1578ca55..17b77128 100644 --- a/lib/rack/query_parser.rb +++ b/lib/rack/query_parser.rb @@ -82,7 +82,13 @@ module Rack k = $1 || ''.freeze after = $' || ''.freeze - return if k.empty? + if k.empty? + if !v.nil? && name == "[]".freeze + return Array(v) + else + return + end + end if after == ''.freeze params[k] = v diff --git a/test/spec_utils.rb b/test/spec_utils.rb index 9601cbaf..b24762c9 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -211,6 +211,9 @@ describe Rack::Utils do Rack::Utils.parse_nested_query("x[][z][w]=a&x[][y]=1&x[][z][w]=b&x[][y]=2"). must_equal "x" => [{"y" => "1", "z" => {"w" => "a"}}, {"y" => "2", "z" => {"w" => "b"}}] + Rack::Utils.parse_nested_query("data[books][][data][page]=1&data[books][][data][page]=2"). + must_equal "data" => { "books" => [{ "data" => { "page" => "1"}}, { "data" => { "page" => "2"}}] } + lambda { Rack::Utils.parse_nested_query("x[y]=1&x[y]z=2") }. must_raise(Rack::Utils::ParameterTypeError). message.must_equal "expected Hash (got String) for param `y'" -- cgit v1.2.3-24-ge0c7