From 326e6527497126b2ea3627e377b6a4b5c9191bef Mon Sep 17 00:00:00 2001 From: Hincu Petru Date: Mon, 3 Feb 2014 09:51:05 +0000 Subject: [PATCH] Fixed "Hash#to_param confused by empty hash values #13892" --- .../lib/active_support/core_ext/object/to_param.rb | 1 + activesupport/test/core_ext/object/to_param_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+) 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 3b137ce6ae..e40846e7d6 100644 --- a/activesupport/lib/active_support/core_ext/object/to_param.rb +++ b/activesupport/lib/active_support/core_ext/object/to_param.rb @@ -51,6 +51,7 @@ class Hash # # This method is also aliased as +to_query+. def to_param(namespace = nil) + return (namespace ? nil.to_query(namespace) : '') if empty? collect do |key, value| value.to_query(namespace ? "#{namespace}[#{key}]" : key) end.sort! * '&' diff --git a/activesupport/test/core_ext/object/to_param_test.rb b/activesupport/test/core_ext/object/to_param_test.rb index bd7c6c422a..eae68ed184 100644 --- a/activesupport/test/core_ext/object/to_param_test.rb +++ b/activesupport/test/core_ext/object/to_param_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'active_support/core_ext/object/to_param' +require 'active_support/core_ext/object/to_query' class ToParamTest < ActiveSupport::TestCase def test_object @@ -16,4 +17,14 @@ def test_boolean assert_equal true, true.to_param assert_equal false, false.to_param end + + def test_nested_empty_hash + hash1 = {a: 1, b: {c: 3, d: {}}}.to_param + hash2 = {p: 12, b: {c: 3, e: nil, f: ''}}.to_param + hash3 = {b: {c: 3, k: {}, f: '' }}.to_param + + assert_equal 'a=1&b[c]=3&b[d]=', CGI::unescape(hash1) + assert_equal 'b[c]=3&b[e]=&b[f]=&p=12', CGI::unescape(hash2) + assert_equal 'b[c]=3&b[f]=&b[k]=', CGI::unescape(hash3) + end end -- GitLab