Skip to content

Commit 0796a4e

Browse files
authored
Preserve null values when parsing SearchReplies (#3518)
EncodedComplexOutput was skipping null values instead of passing them on. Then SearchReplyParser needs to store null values as they are and not try to decode them. This affected both RESP2 and RESP3 parsing. Added two integration tests in RediSearchAggregateIntegrationTests to verify that nulls in JSON documents are parsed correctly.
1 parent 4c67b15 commit 0796a4e

File tree

3 files changed

+180
-38
lines changed

3 files changed

+180
-38
lines changed

src/main/java/io/lettuce/core/output/EncodedComplexOutput.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ public EncodedComplexOutput(RedisCodec<K, V> codec, ComplexDataParser<T> parser)
2525

2626
@Override
2727
public void set(ByteBuffer bytes) {
28-
if (bytes != null) {
29-
data.storeObject(bytes.asReadOnlyBuffer());
30-
}
28+
data.storeObject(bytes == null ? null : bytes.asReadOnlyBuffer());
3129
}
3230

3331
@Override
3432
public void setSingle(ByteBuffer bytes) {
35-
if (bytes != null) {
36-
data.storeObject(bytes.asReadOnlyBuffer());
37-
}
33+
data.storeObject(bytes == null ? null : bytes.asReadOnlyBuffer());
3834
}
3935

4036
}

src/main/java/io/lettuce/core/search/SearchReplyParser.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,13 @@ private void parseResults(SearchReply<K, V> searchReply, List<Object> resultsLis
186186
ComplexData resultData = (ComplexData) resultsList.get(i);
187187
List<Object> resultEntries = resultData.getDynamicList();
188188

189-
Map<K, V> resultEntriesProcessed = IntStream.range(0, resultEntries.size() / 2).boxed()
190-
.collect(Collectors.toMap(idx -> codec.decodeKey((ByteBuffer) resultEntries.get(idx * 2)),
191-
idx -> codec.decodeValue((ByteBuffer) resultEntries.get(idx * 2 + 1))));
189+
for (int idx = 0; idx < resultEntries.size(); idx += 2) {
190+
K decodedKey = codec.decodeKey((ByteBuffer) resultEntries.get(idx));
191+
Object value = resultEntries.get(idx + 1);
192+
V decodedValue = value == null ? null : codec.decodeValue((ByteBuffer) value);
193+
searchResult.addFields(decodedKey, decodedValue);
194+
}
192195

193-
searchResult.addFields(resultEntriesProcessed);
194196
i++;
195197
}
196198

@@ -265,7 +267,7 @@ public SearchReply<K, V> parse(ComplexData data) {
265267
ComplexData extraAttributes = (ComplexData) resultEntry.get(EXTRA_ATTRIBUTES_KEY);
266268
extraAttributes.getDynamicMap().forEach((key, value) -> {
267269
K decodedKey = codec.decodeKey((ByteBuffer) key);
268-
V decodedValue = codec.decodeValue((ByteBuffer) value);
270+
V decodedValue = value == null ? null : codec.decodeValue((ByteBuffer) value);
269271
searchResult.addFields(decodedKey, decodedValue);
270272
});
271273
}

0 commit comments

Comments
 (0)