Skip to content

Commit dd42326

Browse files
committed
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
1 parent b85dbd7 commit dd42326

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/main/java/org/springframework/data/redis/connection/ReactiveHashCommands.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ public List<ByteBuffer> getFields() {
283283
* @see <a href="https://redis.io/commands/hget">Redis Documentation: HGET</a>
284284
*/
285285
default Mono<ByteBuffer> hGet(ByteBuffer key, ByteBuffer field) {
286-
return hMGet(key, Collections.singletonList(field)).flatMapIterable(Function.identity()).next();
286+
return hMGet(key, Collections.singletonList(field)).filter(it -> !it.contains(null))
287+
.flatMapIterable(Function.identity()).next();
287288
}
288289

289290
/**

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveHashCommands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Flux<MultiValueResponse<HGetCommand, ByteBuffer>> hMGet(Publisher<HGetCom
106106
if (command.getFields().size() == 1) {
107107
ByteBuffer key = command.getFields().iterator().next();
108108
result = cmd.hget(command.getKey(), key.duplicate()).map(value -> KeyValue.fromNullable(key, value))
109-
.map(Collections::singletonList).onErrorReturn(Collections.emptyList());
109+
.defaultIfEmpty(KeyValue.empty(key)).map(Collections::singletonList);
110110
} else {
111111
result = cmd.hmget(command.getKey(), command.getFields().stream().toArray(ByteBuffer[]::new)).collectList();
112112
}

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveHashCommandsIntegrationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.ByteBuffer;
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.Arrays;
25+
import java.util.Collections;
2526
import java.util.HashMap;
2627
import java.util.LinkedHashMap;
2728
import java.util.Map;
@@ -110,12 +111,19 @@ void hMGetShouldReturnValueForFields() {
110111
}).verifyComplete();
111112
}
112113

113-
@ParameterizedRedisTest // DATAREDIS-525
114+
@ParameterizedRedisTest // DATAREDIS-525, GH-2210
114115
void hMGetShouldReturnNullValueForFieldsThatHaveNoValue() {
115116

116117
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
117118
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
118119

120+
connection.hashCommands().hMGet(KEY_1_BBUFFER, Collections.singletonList(FIELD_1_BBUFFER)).as(StepVerifier::create)
121+
.expectNext(Collections.singletonList(VALUE_1_BBUFFER)).verifyComplete();
122+
123+
connection.hashCommands().hMGet(KEY_1_BBUFFER, Collections.singletonList(FIELD_2_BBUFFER))
124+
.as(StepVerifier::create)
125+
.expectNext(Collections.singletonList(null)).verifyComplete();
126+
119127
connection.hashCommands().hMGet(KEY_1_BBUFFER, Arrays.asList(FIELD_1_BBUFFER, FIELD_2_BBUFFER, FIELD_3_BBUFFER))
120128
.as(StepVerifier::create)
121129
.expectNext(Arrays.asList(VALUE_1_BBUFFER, null, VALUE_3_BBUFFER)).verifyComplete();

0 commit comments

Comments
 (0)