From 916cb645818caf6cbf054a43cd79f11a8c336c3f 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 --- .../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 23b4d7b777c..bcd2bb529cd 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 @@ -84,7 +84,7 @@ public abstract class AbstractValueAdaptingCache implements Cache { * @return the value to return to the user */ protected @Nullable 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