apache / apache/grails-core

Error in GrailsConcurrentLinkedMapCache.put when excluding null values (v 4.0.0)

Open
#14,167 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-cache
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

The GrailsConcurrentLinkedMapCache.put method is as follows:

public void put(Object key, Object value) {
this.store.put(key, toStoreValue(value));
}

If I configure the cache to not allow null entries, calling the above with ('aKey', null) will try to put a null value into the underlying ConcurrentLinkedHashMap, which causes a NPE to be thrown by that class's checkNotNull assertion on the value

I was able to get around this be subclassing GrailsConcurrentLinkedMapCache and overriding put with:

@Override
void put(Object key, Object value) {
if (allowNullValues || value) {
nativeCache.put(key, toStoreValue(value))
}
}

If I understand correctly, this was the intended behavior.

**Simple test:**
void 'test it'() {
given:
GrailsConcurrentLinkedMapCache cache = new GrailsConcurrentLinkedMapCache('name', 10, false)
when:
cache.put('key', null)
then:
null == cache.get('key')
}

**yields**

java.lang.NullPointerException
at com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap.checkNotNull(ConcurrentLinkedHashMap.java:254)
at com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap.put(ConcurrentLinkedHashMap.java:718)
at com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap.put(ConcurrentLinkedHashMap.java:698)
at grails.plugin.cache.GrailsConcurrentLinkedMapCache.put(GrailsConcurrentLinkedMapCache.java:115)
at com.rgatp.ng.disclosures.api.DecodedInterviewControllerSpec.test it(DecodedInterviewControllerSpec.groovy:19)

Contributor guide

Open the contributing guide

Research direction

Locate GrailsConcurrentLinkedMapCache.put, reported at GrailsConcurrentLinkedMapCache.java:115, and review the provided simple test for a cache configured to exclude null values. Run that test first; done means cache.put('key', null) completes without an exception and cache.get('key') returns null.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.