Skip to content

Commit 2952e9a

Browse files
byrootErol
andcommitted
Fix #to_query to not include = for nil values
Fix: rails#28322 This is more consistent with the behavior of `Rack::Utils.parse_nested_query`. Co-Authored-By: Erol Fornoles <[email protected]>
1 parent 8e7627a commit 2952e9a

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

activesupport/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* `nil.to_query("key")` now returns `key`.
2+
3+
Previously it would return `key=`, preventing round tripping with `Rack::Utils.parse_nested_query`.
4+
5+
*Erol Fornoles*
6+
17
* Avoid wrapping redis in a `ConnectionPool` when using `ActiveSupport::Cache::RedisCacheStore` if the `:redis`
28
option is already a `ConnectionPool`.
39

activesupport/lib/active_support/core_ext/object/to_query.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def to_query(key)
1616
end
1717

1818
class NilClass
19+
# Returns a CGI-escaped +key+.
20+
def to_query(key)
21+
CGI.escape(key.to_param)
22+
end
23+
1924
# Returns +self+.
2025
def to_param
2126
self

activesupport/test/core_ext/object/to_query_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_array_values_are_not_sorted
4949
end
5050

5151
def test_empty_array
52-
assert_equal "person%5B%5D=", [].to_query("person")
52+
assert_equal "person%5B%5D", [].to_query("person")
5353
end
5454

5555
def test_nested_empty_hash
@@ -59,7 +59,7 @@ def test_nested_empty_hash
5959
a: 1, b: { c: 3, d: {} }
6060
assert_query_equal "",
6161
a: { b: { c: {} } }
62-
assert_query_equal "b%5Bc%5D=false&b%5Be%5D=&b%5Bf%5D=&p=12",
62+
assert_query_equal "b%5Bc%5D=false&b%5Be%5D&b%5Bf%5D=&p=12",
6363
p: 12, b: { c: false, e: nil, f: "" }
6464
assert_query_equal "b%5Bc%5D=3&b%5Bf%5D=",
6565
b: { c: 3, k: {}, f: "" }

0 commit comments

Comments
 (0)