diff --git a/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/main/java/org/apache/nifi/redis/service/RedisDistributedMapCacheClientService.java b/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/main/java/org/apache/nifi/redis/service/RedisDistributedMapCacheClientService.java index 145c69f85b..5cca23b73b 100644 --- a/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/main/java/org/apache/nifi/redis/service/RedisDistributedMapCacheClientService.java +++ b/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/main/java/org/apache/nifi/redis/service/RedisDistributedMapCacheClientService.java @@ -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); }); } diff --git a/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisDistributedMapCacheClientService.java b/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisDistributedMapCacheClientService.java index 9d43e67177..5e0ffd5614 100644 --- a/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisDistributedMapCacheClientService.java +++ b/nifi-nar-bundles/nifi-redis-bundle/nifi-redis-extensions/src/test/java/org/apache/nifi/redis/service/ITRedisDistributedMapCacheClientService.java @@ -168,6 +168,7 @@ public class ITRedisDistributedMapCacheClientService { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final Serializer stringSerializer = new StringSerializer(); final Deserializer stringDeserializer = new StringDeserializer(); + final Deserializer 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 { + @Override + public String deserialize(byte[] input) throws DeserializationException, IOException { + return new String(input, StandardCharsets.UTF_8); + } + } }