Skip to content

Commit bd95177

Browse files
committed
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
1 parent 7ddb830 commit bd95177

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,17 @@ 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-
113-
if (index != -1) {
114-
// OPTIONAL as per API
115-
args.add(index);
109+
if (jsonPath != null) {
110+
if (!jsonPath.isRootPath()) {
111+
args.add(jsonPath.toString());
112+
if (index != -1) {
113+
args.add(index);
114+
}
115+
} else {
116+
if (index != -1) {
117+
args.add(jsonPath.toString());
118+
args.add(index);
119+
}
116120
}
117121
}
118122

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)