Skip to content

Commit 5f8c3fd

Browse files
authored
json.arrpop forces index=-1 with root path (#3214) (#3217)
* Fix json.arrpop with root path Fix IntegrationTest Add Integration Test Fix test Update the logic Update logic Update the logic Correct the test Typo * Add the root path even index = -1 * Only add root path if index != -1
1 parent 7dd5548 commit 5f8c3fd

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/main/java/io/lettuce/core/RedisJsonCommandBuilder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ Command<K, V, List<JsonValue>> jsonArrpop(K key, JsonPath jsonPath, int index) {
106106

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

109-
if (jsonPath != null && !jsonPath.isRootPath()) {
110-
// OPTIONAL as per API
111-
args.add(jsonPath.toString());
112-
109+
if (jsonPath != null) {
113110
if (index != -1) {
114111
// OPTIONAL as per API
112+
args.add(jsonPath.toString());
115113
args.add(index);
114+
} else if (!jsonPath.isRootPath()) {
115+
// OPTIONAL as per API
116+
args.add(jsonPath.toString());
116117
}
117118
}
118119

src/test/java/io/lettuce/core/RedisJsonCommandBuilderUnitTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void shouldCorrectlyConstructJsonArrpopNoIndex() {
157157
}
158158

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

169+
@Test
170+
void shouldCorrectlyConstructJsonArrpopRootPath() {
171+
Command<String, String, List<JsonValue>> command = builder.jsonArrpop(MY_KEY, JsonPath.ROOT_PATH, 1);
172+
ByteBuf buf = Unpooled.directBuffer();
173+
command.encode(buf);
174+
175+
assertThat(buf.toString(StandardCharsets.UTF_8)).isEqualTo("*4\r\n" + "$11\r\n" + "JSON.ARRPOP\r\n" + "$15\r\n"
176+
+ "bikes:inventory\r\n" + "$1\r\n" + "$\r\n" + "$1\r\n" + "1\r\n");
177+
}
178+
169179
@Test
170180
void shouldCorrectlyConstructJsonArrtrim() {
171181
JsonRangeArgs range = JsonRangeArgs.Builder.start(0).stop(1);

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,16 @@ public void jsonArrpopEmptyArray() {
163163
redis.jsonSet("myKey", JsonPath.ROOT_PATH, value);
164164
List<JsonValue> result = redis.jsonArrpop("myKey");
165165
assertThat(result.toString()).isEqualTo("[\"one\"]");
166-
result = redis.jsonArrpop("myKey", JsonPath.ROOT_PATH, 0);
167-
assertThat(result.get(0).isNull()).isTrue();
166+
assertThat(redis.jsonGet("myKey").get(0).toString()).isEqualTo("[]");
167+
}
168+
169+
@Test
170+
public void jsonArrpopWithRootPathAndIndex() {
171+
JsonValue value = redis.getJsonParser().createJsonValue("[\"one\",\"two\",\"three\"]");
172+
redis.jsonSet("myKey", JsonPath.ROOT_PATH, value);
173+
List<JsonValue> result = redis.jsonArrpop("myKey", JsonPath.ROOT_PATH, 1);
174+
assertThat(result.toString()).isEqualTo("[\"two\"]");
175+
assertThat(redis.jsonGet("myKey").get(0).toString()).isEqualTo("[\"one\",\"three\"]");
168176
}
169177

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

0 commit comments

Comments
 (0)