Repository navigation
[type:fix] guard the rule handle of the general context plugin - #7167
Conversation
CommonHandleCache.obtainHandle returns null on a miss, so a rule without a cached handle made doExecute dereference it and fail the request with a NullPointerException instead of passing it through the chain. The other plugins of this set already guard their handle this way.
Aias00
left a comment
There was a problem hiding this comment.
APPROVE — the NPE is real and the guard is the right fix.
Verified against CommonHandleCache on master:
public V obtainHandle(final K key) {
return cached.get(key); // ConcurrentHashMap#get -> null on a miss
}
public void cachedHandle(final K key, final V value) {
Optional.ofNullable(key).ifPresent(k -> Optional.ofNullable(value).ifPresent(v -> cached.put(k, v)));
}So obtainHandle genuinely returns null when no handle was ever cached for the rule, and cachedHandle never stores a null — meaning the previous generalContextHandleMap.isEmpty() dereferenced a null map. GeneralContextPluginDataHandler only caches when the rule carries a handle, so a rule registered without one (or one whose handle failed to parse into a map) hits this path directly. Adding the null check is correct, and handleSelectorIfNull-style pass-through (chain.execute) is the right behaviour for "no general-context configuration".
The regression test does the right thing too: it removes the handle from the cache rather than mocking the cache, then asserts the chain is still invoked once and no GENERAL_CONTEXT attribute is set — that would fail before the fix with an NPE.
One thing worth considering separately (not blocking): obtainHandle(key) returning null is a footgun that every caller has to remember to null-check, and several other plugins call it. Returning an empty collection (or adding an obtainHandleOrDefault) would remove the whole class of NPE rather than patching each call site. Up to you whether to fold that in here or file it.
Motivation
Fixes #6657.
GeneralContextPlugin.doExecutedereferenced the rule handle map without a null check:CommonHandleCache.obtainHandlereturnsnullon a miss, so a rule that reaches the plugin without a cachedhandle makes it throw a
NullPointerException— the request fails instead of continuing through the chain.Every other plugin of this set guards its handle before using it (
ContextPathPlugin,RewritePlugin, ...),this one was the exception.
What changed
A rule without a cached handle now passes through the chain untouched, exactly like a rule with an empty
handle map does today.
Testing
BUILD SUCCESS, checkstyle clean,GeneralContextPluginTest4/4 including the new case: with no cached handlefor the rule the request reaches the chain unchanged and no general context attribute is added to the exchange.