From 41bbac6ff4d161fc94ef2dc601e06f0a2779dbaf Mon Sep 17 00:00:00 2001
From: Bogdan Gusiev <agresso@gmail.com>
Date: Fri, 2 May 2014 20:57:59 +0300
Subject: [Fixes #14948] Hash#to_query: Changed a way how empty hash and empty
 array are serialized

Empty Hash or Array should not present in serialization result

    {a: []}.to_query # => ""
    {a: {}}.to_query # => ""

For more info see #14948.
---
 activesupport/CHANGELOG.md                                   | 11 +++++++++++
 activesupport/lib/active_support/core_ext/object/to_param.rb | 12 ++++++------
 activesupport/test/core_ext/object/to_query_test.rb          |  8 +++++---
 3 files changed, 22 insertions(+), 9 deletions(-)

(limited to 'activesupport')

diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 5e430d20fa..2dc48f0084 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,14 @@
+*   Fixed backward compatibility isues introduced in 326e652
+
+    Empty Hash or Array should not present in serialization result
+
+        {a: []}.to_query # => ""
+        {a: {}}.to_query # => ""
+
+    For more info see #14948.
+
+    *Bogdan Gusiev*
+
 *   Add `SecureRandom::uuid_v3` and `SecureRandom::uuid_v5` to support stable
     UUID fixtures on PostgreSQL.
 
diff --git a/activesupport/lib/active_support/core_ext/object/to_param.rb b/activesupport/lib/active_support/core_ext/object/to_param.rb
index 13be0038c2..0611eb819d 100644
--- a/activesupport/lib/active_support/core_ext/object/to_param.rb
+++ b/activesupport/lib/active_support/core_ext/object/to_param.rb
@@ -51,12 +51,12 @@ class Hash
   #
   # This method is also aliased as +to_query+.
   def to_param(namespace = nil)
-    if empty?
-      namespace ? nil.to_query(namespace) : ''
-    else
-      collect do |key, value|
+    collect do |key, value|
+      unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
         value.to_query(namespace ? "#{namespace}[#{key}]" : key)
-      end.sort! * '&'
-    end
+      else
+        nil
+      end
+    end.compact.sort! * '&'
   end
 end
diff --git a/activesupport/test/core_ext/object/to_query_test.rb b/activesupport/test/core_ext/object/to_query_test.rb
index f887a9e613..7457c4655a 100644
--- a/activesupport/test/core_ext/object/to_query_test.rb
+++ b/activesupport/test/core_ext/object/to_query_test.rb
@@ -49,13 +49,15 @@ class ToQueryTest < ActiveSupport::TestCase
   def test_nested_empty_hash
     assert_equal '',
       {}.to_query
-    assert_query_equal 'a=1&b%5Bc%5D=3&b%5Bd%5D=',
+    assert_query_equal 'a=1&b%5Bc%5D=3',
       { a: 1, b: { c: 3, d: {} } }
+    assert_query_equal '',
+      { a: {b: {c: {}}} }
     assert_query_equal 'b%5Bc%5D=false&b%5Be%5D=&b%5Bf%5D=&p=12',
       { p: 12, b: { c: false, e: nil, f: '' } }
-    assert_query_equal 'b%5Bc%5D=3&b%5Bf%5D=&b%5Bk%5D=',
+    assert_query_equal 'b%5Bc%5D=3&b%5Bf%5D=',
       { b: { c: 3, k: {}, f: '' } }
-    assert_query_equal 'a%5B%5D=&b=3',
+    assert_query_equal 'b=3',
       {a: [], b: 3}
   end
 
-- 
cgit v1.2.3