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

Fix empty LUA table reply #3924

Merged
merged 3 commits into from
Aug 18, 2024
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
7 changes: 5 additions & 2 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ public Object build(Object data) {

if (data instanceof List) {
final List list = (List) data;
if (list.isEmpty()) return Collections.emptyMap();
if (list.isEmpty()) {
return list == Protocol.PROTOCOL_EMPTY_MAP ? Collections.emptyMap() : Collections.emptyList();
}

if (list.get(0) instanceof KeyValue) {
return ((List<KeyValue>) data).stream()
Expand All @@ -397,8 +399,9 @@ public Object build(Object data) {
}
} else if (data instanceof byte[]) {
return STRING.build(data);
} else {
return data;
}
return data;
}
};

Expand Down
22 changes: 14 additions & 8 deletions src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -49,6 +50,8 @@ public final class Protocol {
public static final byte[] POSITIVE_INFINITY_BYTES = "+inf".getBytes();
public static final byte[] NEGATIVE_INFINITY_BYTES = "-inf".getBytes();

static final List<KeyValue> PROTOCOL_EMPTY_MAP = Collections.unmodifiableList(new ArrayList<>(0));

private static final String ASK_PREFIX = "ASK ";
private static final String MOVED_PREFIX = "MOVED ";
private static final String CLUSTERDOWN_PREFIX = "CLUSTERDOWN ";
Expand Down Expand Up @@ -192,7 +195,6 @@ private static byte[] processBulkReply(final RedisInputStream is) {
}

private static List<Object> processMultiBulkReply(final RedisInputStream is) {
// private static List<Object> processMultiBulkReply(final int num, final RedisInputStream is) {
final int num = is.readIntCrLf();
if (num == -1) return null;
final List<Object> ret = new ArrayList<>(num);
Expand All @@ -206,16 +208,20 @@ private static List<Object> processMultiBulkReply(final RedisInputStream is) {
return ret;
}

// private static List<Object> processMultiBulkReply(final RedisInputStream is) {
// private static List<Object> processMultiBulkReply(final int num, final RedisInputStream is) {
private static List<KeyValue> processMapKeyValueReply(final RedisInputStream is) {
final int num = is.readIntCrLf();
if (num == -1) return null;
final List<KeyValue> ret = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
ret.add(new KeyValue(process(is), process(is)));
switch (num) {
case -1:
return null;
case 0:
return PROTOCOL_EMPTY_MAP;
default:
final List<KeyValue> ret = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
ret.add(new KeyValue(process(is), process(is)));
}
return ret;
}
return ret;
}

public static Object read(final RedisInputStream is) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ public void scriptExistsWithBrokenConnection() {
deadClient.close();
}

@Test
public void emptyLuaTableReply() {
Object reply = jedis.eval("return {}");
assertEquals(Collections.emptyList(), reply);
}

@Test
public void functionLoadAndDelete() {
String engine = "Lua";
Expand Down
Loading