Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonArrpop fails with null return value (#3196) #3206

Merged
merged 5 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/io/lettuce/core/json/DefaultJsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -61,6 +62,10 @@ public JsonValue fromObject(Object object) {
}

private JsonValue parse(String value) {
if (value == null) {
return DelegateJsonValue.wrap(NullNode.getInstance());
}

ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(value);
Expand All @@ -72,6 +77,10 @@ private JsonValue parse(String value) {
}

private JsonValue parse(ByteBuffer byteBuffer) {
if (byteBuffer == null) {
return DelegateJsonValue.wrap(NullNode.getInstance());
}

ObjectMapper mapper = new ObjectMapper();
try {
byte[] bytes = new byte[byteBuffer.remaining()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public UnproccessedJsonValue(ByteBuffer bytes, JsonParser theParser) {

@Override
public String toString() {

if (unprocessedData == null) {
return null;
}

if (isDeserialized()) {
return jsonValue.toString();
}
Expand Down Expand Up @@ -161,7 +166,9 @@ private void lazilyDeserialize() {
try {
if (!isDeserialized()) {
jsonValue = parser.createJsonValue(unprocessedData);
unprocessedData.clear();
if (unprocessedData != null) {
unprocessedData.clear();
}
}
} finally {
lock.unlock();
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/lettuce/core/output/JsonValueListOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ public void set(ByteBuffer bytes) {
multi(1);
}

ByteBuffer fetched = ByteBuffer.allocate(bytes.remaining());
fetched.put(bytes);
fetched.flip();
ByteBuffer fetched = null;
if (bytes != null) {
fetched = ByteBuffer.allocate(bytes.remaining());
fetched.put(bytes);
fetched.flip();
}

output.add(parser.loadJsonValue(fetched));
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/lettuce/core/json/RedisJsonIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ void jsonArrpop(String path) {
"{\"id\":\"bike:3\",\"model\":\"Weywot\",\"description\":\"This bike gives kids aged six years and old");
}

@Test
public void jsonArrpopEmptyArray() {
JsonValue value = redis.getJsonParser().createJsonValue("[\"one\"]");
redis.jsonSet("myKey", JsonPath.ROOT_PATH, value);
List<JsonValue> result = redis.jsonArrpop("myKey");
assertThat(result.toString()).isEqualTo("[\"one\"]");
result = redis.jsonArrpop("myKey", JsonPath.ROOT_PATH, 0);
assertThat(result.get(0).isNull()).isTrue();
}

@ParameterizedTest(name = "With {0} as path")
@ValueSource(strings = { BIKE_COLORS_V1, BIKE_COLORS_V2 })
void jsonArrtrim(String path) {
Expand Down