Skip to content
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: 5 additions & 4 deletions src/main/java/io/lettuce/core/RedisJsonCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ Command<K, V, List<JsonValue>> jsonArrpop(K key, JsonPath jsonPath, int index) {

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
// OPTIONAL as per API
args.add(jsonPath.toString());

if (jsonPath != null) {
if (index != -1) {
// OPTIONAL as per API
args.add(jsonPath.toString());
args.add(index);
} else if (!jsonPath.isRootPath()) {
// OPTIONAL as per API
args.add(jsonPath.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void shouldCorrectlyConstructJsonArrpopNoIndex() {
}

@Test
void shouldCorrectlyConstructJsonArrpopRootPath() {
void shouldCorrectlyConstructJsonArrpopRootPathNoIndex() {
Command<String, String, List<JsonValue>> command = builder.jsonArrpop(MY_KEY, JsonPath.ROOT_PATH, -1);
ByteBuf buf = Unpooled.directBuffer();
command.encode(buf);
Expand All @@ -166,6 +166,16 @@ void shouldCorrectlyConstructJsonArrpopRootPath() {
.isEqualTo("*2\r\n" + "$11\r\n" + "JSON.ARRPOP\r\n" + "$15\r\n" + "bikes:inventory\r\n");
}

@Test
void shouldCorrectlyConstructJsonArrpopRootPath() {
Command<String, String, List<JsonValue>> command = builder.jsonArrpop(MY_KEY, JsonPath.ROOT_PATH, 1);
ByteBuf buf = Unpooled.directBuffer();
command.encode(buf);

assertThat(buf.toString(StandardCharsets.UTF_8)).isEqualTo("*4\r\n" + "$11\r\n" + "JSON.ARRPOP\r\n" + "$15\r\n"
+ "bikes:inventory\r\n" + "$1\r\n" + "$\r\n" + "$1\r\n" + "1\r\n");
}

@Test
void shouldCorrectlyConstructJsonArrtrim() {
JsonRangeArgs range = JsonRangeArgs.Builder.start(0).stop(1);
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/io/lettuce/core/json/RedisJsonIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,16 @@ public void jsonArrpopEmptyArray() {
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();
assertThat(redis.jsonGet("myKey").get(0).toString()).isEqualTo("[]");
}

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

@ParameterizedTest(name = "With {0} as path")
Expand Down