Permalink
Browse files

[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.
  • Loading branch information...
1 parent 4565cb7 commit 41bbac6ff4d161fc94ef2dc601e06f0a2779dbaf @bogdan bogdan committed May 2, 2014
@@ -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.
@@ -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
@@ -49,13 +49,15 @@ def test_array_values_are_not_sorted
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

0 comments on commit 41bbac6

Please sign in to comment.