From 8b5cbb7496f1434fdbc3dc52a0e19b75fae88f68 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 29 Apr 2026 21:52:04 +0200 Subject: [PATCH] Detect custom deserialized NullValue instances Closes gh-36727 (cherry picked from commit 916cb645818caf6cbf054a43cd79f11a8c336c3f) --- .../cache/support/AbstractValueAdaptingCache.java | 2 +- .../context/testfixture/cache/AbstractCacheTests.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/spring-context/src/main/java/org/springframework/cache/support/AbstractValueAdaptingCache.java b/spring-context/src/main/java/org/springframework/cache/support/AbstractValueAdaptingCache.java index be75b83aa22..dc13b9b2639 100644 --- a/spring-context/src/main/java/org/springframework/cache/support/AbstractValueAdaptingCache.java +++ b/spring-context/src/main/java/org/springframework/cache/support/AbstractValueAdaptingCache.java @@ -87,7 +87,7 @@ public abstract class AbstractValueAdaptingCache implements Cache { */ @Nullable protected Object fromStoreValue(@Nullable Object storeValue) { - if (this.allowNullValues && storeValue == NullValue.INSTANCE) { + if (this.allowNullValues && storeValue instanceof NullValue) { return null; } return storeValue; diff --git a/spring-context/src/testFixtures/java/org/springframework/context/testfixture/cache/AbstractCacheTests.java b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/cache/AbstractCacheTests.java index b869d3bec5b..29a9443cfd7 100644 --- a/spring-context/src/testFixtures/java/org/springframework/context/testfixture/cache/AbstractCacheTests.java +++ b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/cache/AbstractCacheTests.java @@ -24,7 +24,9 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; +import org.springframework.beans.BeanUtils; import org.springframework.cache.Cache; +import org.springframework.cache.support.NullValue; import static org.assertj.core.api.Assertions.assertThat; @@ -72,6 +74,12 @@ public abstract class AbstractCacheTests { assertThat(cache.get(key).get()).isNull(); assertThat(cache.get(key, String.class)).isNull(); assertThat(cache.get(key, Object.class)).isNull(); + + cache.put(key, BeanUtils.instantiateClass(NullValue.class)); + assertThat(cache.get(key)).isNotNull(); + assertThat(cache.get(key).get()).isNull(); + assertThat(cache.get(key, String.class)).isNull(); + assertThat(cache.get(key, Object.class)).isNull(); } @Test