diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 5e430d20fa6227b8fc4c7c70bc584ef50ffc0ecc..2dc48f0084814fb12032983834a5b6e30292549c 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 13be0038c23e6a66ff7681c27bf975ee777d91c9..0611eb819d119768e7ef1bf0b7a6cd787a5c802d 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 f887a9e613878f407995a2108906df2cd4de43f2..7457c4655a9c1120d4b6f1d89bf8e8c9afe32264 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 @@ 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