Skip to content

Commit 341cdad

Browse files
authored
Exception on zpopmin (#3199) (#3206)
* Handle Null values better when working with JSON * Incomplete submit after some refactoring * Sanitize the CI output when running on CI * Plishing * Never submit irrelevant changes in your PR
1 parent 09a42b1 commit 341cdad

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/main/java/io/lettuce/core/json/DefaultJsonParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.core.JsonProcessingException;
1111
import com.fasterxml.jackson.databind.JsonNode;
1212
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.fasterxml.jackson.databind.node.NullNode;
1314

1415
import java.io.IOException;
1516
import java.nio.ByteBuffer;
@@ -61,6 +62,10 @@ public JsonValue fromObject(Object object) {
6162
}
6263

6364
private JsonValue parse(String value) {
65+
if (value == null) {
66+
return DelegateJsonValue.wrap(NullNode.getInstance());
67+
}
68+
6469
ObjectMapper mapper = new ObjectMapper();
6570
try {
6671
JsonNode root = mapper.readTree(value);
@@ -72,6 +77,10 @@ private JsonValue parse(String value) {
7277
}
7378

7479
private JsonValue parse(ByteBuffer byteBuffer) {
80+
if (byteBuffer == null) {
81+
return DelegateJsonValue.wrap(NullNode.getInstance());
82+
}
83+
7584
ObjectMapper mapper = new ObjectMapper();
7685
try {
7786
byte[] bytes = new byte[byteBuffer.remaining()];

src/main/java/io/lettuce/core/json/UnproccessedJsonValue.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public UnproccessedJsonValue(ByteBuffer bytes, JsonParser theParser) {
4545

4646
@Override
4747
public String toString() {
48+
49+
if (unprocessedData == null) {
50+
return null;
51+
}
52+
4853
if (isDeserialized()) {
4954
return jsonValue.toString();
5055
}
@@ -161,7 +166,9 @@ private void lazilyDeserialize() {
161166
try {
162167
if (!isDeserialized()) {
163168
jsonValue = parser.createJsonValue(unprocessedData);
164-
unprocessedData.clear();
169+
if (unprocessedData != null) {
170+
unprocessedData.clear();
171+
}
165172
}
166173
} finally {
167174
lock.unlock();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ public void set(ByteBuffer bytes) {
3939
multi(1);
4040
}
4141

42-
ByteBuffer fetched = ByteBuffer.allocate(bytes.remaining());
43-
fetched.put(bytes);
44-
fetched.flip();
42+
ByteBuffer fetched = null;
43+
if (bytes != null) {
44+
fetched = ByteBuffer.allocate(bytes.remaining());
45+
fetched.put(bytes);
46+
fetched.flip();
47+
}
48+
4549
output.add(parser.loadJsonValue(fetched));
4650
}
4751

src/test/java/io/lettuce/core/json/RedisJsonIntegrationTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ void jsonArrpop(String path) {
157157
"{\"id\":\"bike:3\",\"model\":\"Weywot\",\"description\":\"This bike gives kids aged six years and old");
158158
}
159159

160+
@Test
161+
public void jsonArrpopEmptyArray() {
162+
JsonValue value = redis.getJsonParser().createJsonValue("[\"one\"]");
163+
redis.jsonSet("myKey", JsonPath.ROOT_PATH, value);
164+
List<JsonValue> result = redis.jsonArrpop("myKey");
165+
assertThat(result.toString()).isEqualTo("[\"one\"]");
166+
result = redis.jsonArrpop("myKey", JsonPath.ROOT_PATH, 0);
167+
assertThat(result.get(0).isNull()).isTrue();
168+
}
169+
160170
@ParameterizedTest(name = "With {0} as path")
161171
@ValueSource(strings = { BIKE_COLORS_V1, BIKE_COLORS_V2 })
162172
void jsonArrtrim(String path) {

0 commit comments

Comments
 (0)