Skip to content

Detect custom deserialized NullValue instances in AbstractValueAdaptingCache #36727

Closed

Description

@Nyamiou

Hello,

I have an application based on Spring Boot 4.0.1 and that is using JCache with Redisson for the cache. I have a method with @CacheResult(cacheName = "foo") which can return null. When calling the method the first time with a param that make it return null there is no problem, the method returns null. However, when calling the method a second time, there is problem because I get the following error:

java.lang.ClassCastException: class org.springframework.cache.support.NullValue cannot be cast to class myClass (org.springframework.cache.support.NullValue and myClass are in unnamed module of loader 'app')

Note: I replaced the full class declaration with myClass here.

I gave a look to AbstractValueAdaptingCache which is the one responsible to convert null to NullValue.INSTANCE in the method toStoreValue(...) and is supposed to convert it back in the method fromStoreValue(...). But while debugging I found that in:

	protected @Nullable Object fromStoreValue(@Nullable Object storeValue) {
		if (this.allowNullValues && storeValue == NullValue.INSTANCE) {
			return null;
		}
		return storeValue;
	}

The storeValue I get is not NullValue.INSTANCE but another instance of NullValue (probably because Redisson de-serialized it into another instance), so the test storeValue == NullValue.INSTANCE fails.

I found out that in RedisCache this is worked around (with BINARY_NULL_VALUE) where any instance of NullValue is serialized as BINARY_NULL_VALUE and BINARY_NULL_VALUE is always de-serialized as NullValue.INSTANCE. But no such workaround seems to exists when using JCache.

Can't the test in fromStoreValue(...) be changed to storeValue instanceof NullValue so it wouldn't be affected by serialization/de-serialization? Or should I ask Redisson to implement a similar workaround?

Thank you

Activity

  1. changed the title [-]Bug with JCache and null values (java.lang.ClassCastException: class org.springframework.cache.support.NullValue cannot be cast to ?)[/-] [+]Issue with JCache and null values (java.lang.ClassCastException: class org.springframework.cache.support.NullValue cannot be cast to ?)[/+] on Apr 29, 2026
  2. self-assigned this
    on Apr 29, 2026
  3. added
    in: coreIssues in core modules (aop, beans, core, context, expression)
    and removed on Apr 29, 2026
  4. added this to the 7.0.8 milestone on Apr 29, 2026
  5. changed the title [-]Issue with JCache and null values (java.lang.ClassCastException: class org.springframework.cache.support.NullValue cannot be cast to ?)[/-] [+]Detect custom deserialized `NullValue` instances in `AbstractValueAdaptingCache`[/+] on Apr 29, 2026
  6. added
    status: backportedAn issue that has been backported to maintenance branches
    and removed
    for: backport-to-7.0.xMarks an issue as a candidate for backport to 7.0.x
    on Apr 29, 2026
  7. jhoeller commented on Apr 29, 2026

    @jhoeller
    Contributor

    We can indeed relax this check to instanceof NullValue, covering custom deserialized NullValue instances as well (not coming out of Java serialization but rather reflectively calling the private NullValue constructor which is what I suspect Redisson is doing here).

  8. added a commit that references this issue on Apr 29, 2026
    916cb64
  9. added a commit that references this issue on Apr 29, 2026
    8b5cbb7
  10. Nyamiou commented on Apr 30, 2026

    @Nyamiou
    Author

    That was fast! Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions