Return single-element list with empty value when ReactiveHashOperations.hMGet(…) for a single key returns no value.

We now return a list containing a single empty KeyValue element when ReactiveHashOperations.hMGet(…) called for a single key returns no value.

Previously, the code used onErrorReturn(…) which returned the wrong value and suppressed errors.

Closes #2210
This commit is contained in:
Mark Paluch
2021-12-10 09:54:19 +01:00
parent b85dbd782f
commit dd42326fa9
3 changed files with 12 additions and 3 deletions

View File

@@ -283,7 +283,8 @@ public interface ReactiveHashCommands {
* @see <a href="https://redis.io/commands/hget">Redis Documentation: HGET</a>
*/
default Mono<ByteBuffer> hGet(ByteBuffer key, ByteBuffer field) {
return hMGet(key, Collections.singletonList(field)).flatMapIterable(Function.identity()).next();
return hMGet(key, Collections.singletonList(field)).filter(it -> !it.contains(null))
.flatMapIterable(Function.identity()).next();
}
/**

View File

@@ -106,7 +106,7 @@ class LettuceReactiveHashCommands implements ReactiveHashCommands {
if (command.getFields().size() == 1) {
ByteBuffer key = command.getFields().iterator().next();
result = cmd.hget(command.getKey(), key.duplicate()).map(value -> KeyValue.fromNullable(key, value))
.map(Collections::singletonList).onErrorReturn(Collections.emptyList());
.defaultIfEmpty(KeyValue.empty(key)).map(Collections::singletonList);
} else {
result = cmd.hmget(command.getKey(), command.getFields().stream().toArray(ByteBuffer[]::new)).collectList();
}

View File

@@ -22,6 +22,7 @@ import reactor.test.StepVerifier;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -110,12 +111,19 @@ public class LettuceReactiveHashCommandsIntegrationTests extends LettuceReactive
}).verifyComplete();
}
@ParameterizedRedisTest // DATAREDIS-525
@ParameterizedRedisTest // DATAREDIS-525, GH-2210
void hMGetShouldReturnNullValueForFieldsThatHaveNoValue() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
connection.hashCommands().hMGet(KEY_1_BBUFFER, Collections.singletonList(FIELD_1_BBUFFER)).as(StepVerifier::create)
.expectNext(Collections.singletonList(VALUE_1_BBUFFER)).verifyComplete();
connection.hashCommands().hMGet(KEY_1_BBUFFER, Collections.singletonList(FIELD_2_BBUFFER))
.as(StepVerifier::create)
.expectNext(Collections.singletonList(null)).verifyComplete();
connection.hashCommands().hMGet(KEY_1_BBUFFER, Arrays.asList(FIELD_1_BBUFFER, FIELD_2_BBUFFER, FIELD_3_BBUFFER))
.as(StepVerifier::create)
.expectNext(Arrays.asList(VALUE_1_BBUFFER, null, VALUE_3_BBUFFER)).verifyComplete();