Skip to content

Commit a650dd0

Browse files
committed
Fix rails#3737 AS::expand_cache_key generates wrong key in certain situations (part 2)
`nil` and `false` both expand to `""` (empty string), while `true` expands to `"true"`; `false` should expand to `"false"`
1 parent d8e6dc9 commit a650dd0

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def self.expand_cache_key(key, namespace = nil)
8484
case
8585
when key.respond_to?(:cache_key) then key.cache_key
8686
when key.is_a?(Array) then key.map { |element| expand_cache_key(element) }.to_param
87-
when key then key.to_param
87+
else key.to_param
8888
end.to_s
8989

9090
expanded_cache_key

activesupport/test/caching_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,33 @@ def test_expand_cache_key_rails_cache_id_should_win_over_rails_app_version
4242
end
4343
end
4444

45-
def test_respond_to_cache_key
45+
def test_expand_cache_key_respond_to_cache_key
4646
key = 'foo'
4747
def key.cache_key
4848
:foo_key
4949
end
5050
assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key(key)
5151
end
5252

53-
def test_array_with_something_that_responds_to_cache_key
53+
def test_expand_cache_key_array_with_something_that_responds_to_cache_key
5454
key = 'foo'
5555
def key.cache_key
5656
:foo_key
5757
end
5858
assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key([key])
5959
end
6060

61+
def test_expand_cache_key_of_nil
62+
assert_equal '', ActiveSupport::Cache.expand_cache_key(nil)
63+
end
64+
65+
def test_expand_cache_key_of_false
66+
assert_equal 'false', ActiveSupport::Cache.expand_cache_key(false)
67+
end
68+
69+
def test_expand_cache_key_of_true
70+
assert_equal 'true', ActiveSupport::Cache.expand_cache_key(true)
71+
end
6172
end
6273

6374
class CacheStoreSettingTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)