NIFI-7279 This closes #4160. Protect against NPE in RedisDistributedMapCacheClientService when value is null

Signed-off-by: Joe Witt <joewitt@apache.org>
This commit is contained in:
Bryan Bende 2020-03-25 15:06:13 -04:00 committed by Joe Witt
parent 5784888082
commit 975f2bdc4f
No known key found for this signature in database
GPG Key ID: 9093BF854F811A1A
2 changed files with 13 additions and 1 deletions

View File

@ -199,7 +199,7 @@ public class RedisDistributedMapCacheClientService extends AbstractControllerSer
return withConnection(redisConnection -> {
final byte[] k = serialize(key, keySerializer);
final byte[] v = redisConnection.get(k);
return valueDeserializer.deserialize(v);
return v == null ? null : valueDeserializer.deserialize(v);
});
}

View File

@ -168,6 +168,7 @@ public class ITRedisDistributedMapCacheClientService {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Serializer<String> stringSerializer = new StringSerializer();
final Deserializer<String> stringDeserializer = new StringDeserializer();
final Deserializer<String> stringDeserializerWithoutNullCheck = new StringDeserializerWithoutNullCheck();
final AtomicDistributedMapCacheClient cacheClient = context.getProperty(REDIS_MAP_CACHE).asControllerService(AtomicDistributedMapCacheClient.class);
@ -185,6 +186,10 @@ public class ITRedisDistributedMapCacheClientService {
final String retrievedValue = cacheClient.get(key, stringSerializer, stringDeserializer);
Assert.assertEquals(value, retrievedValue);
// verify get returns null for a key that doesn't exist
final String missingValue = cacheClient.get("does-not-exist", stringSerializer, stringDeserializerWithoutNullCheck);
Assert.assertEquals(null, missingValue);
// verify remove removes the entry and contains key returns false after
Assert.assertTrue(cacheClient.remove(key, stringSerializer));
Assert.assertFalse(cacheClient.containsKey(key, stringSerializer));
@ -261,4 +266,11 @@ public class ITRedisDistributedMapCacheClientService {
return input == null ? null : new String(input, StandardCharsets.UTF_8);
}
}
private static class StringDeserializerWithoutNullCheck implements Deserializer<String> {
@Override
public String deserialize(byte[] input) throws DeserializationException, IOException {
return new String(input, StandardCharsets.UTF_8);
}
}
}