From c1774ea5aad2087bc044c839bb13585b287cfdda Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 12 Oct 2017 09:28:06 +0200 Subject: [PATCH] Bind byte arrays nested in an Iterable argument #627 Byte arrays passed within an Iterable are now unrolled and bound correctly. Previously, byte arrays were not bound and silently dropped. --- .../lettuce/core/dynamic/ParameterBinder.java | 18 ++++++++++++++++-- .../core/dynamic/RedisCommandsSyncTest.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/lettuce/core/dynamic/ParameterBinder.java b/src/main/java/io/lettuce/core/dynamic/ParameterBinder.java index f15f2719c8..d34a3542ca 100644 --- a/src/main/java/io/lettuce/core/dynamic/ParameterBinder.java +++ b/src/main/java/io/lettuce/core/dynamic/ParameterBinder.java @@ -136,12 +136,15 @@ private void bind(CommandArgs args, RedisCodec codec, Object } if (argument instanceof Iterable) { + for (Object argumentElement : (Iterable) argument) { bindArgument(args, argumentElement); } - } else { - bindArgument(args, argument); + + return; } + + bindArgument(args, argument); } /* @@ -151,14 +154,22 @@ private void bind(CommandArgs args, RedisCodec codec, Object @SuppressWarnings("unchecked") private static void bindArgument(CommandArgs args, Object argument) { + if (argument instanceof byte[]) { + args.add((byte[]) argument); + return; + } + if (argument instanceof String) { args.add((String) argument); + return; } if (argument instanceof Double) { args.add(((Double) argument)); + return; } else if (argument instanceof Number) { args.add(((Number) argument).longValue()); + return; } if (argument instanceof ProtocolKeyword) { @@ -229,7 +240,10 @@ private static void bindArgument(CommandArgs args, Object argument) if (argument instanceof CompositeArgument) { ((CompositeArgument) argument).build(args); + return; } + + throw new IllegalArgumentException("Cannot bind unsupported command argument " + args); } private static void bindValueRange(CommandArgs args, RedisCodec codec, Range range) { diff --git a/src/test/java/io/lettuce/core/dynamic/RedisCommandsSyncTest.java b/src/test/java/io/lettuce/core/dynamic/RedisCommandsSyncTest.java index 2ae07c9ae1..3be4ebd7ea 100644 --- a/src/test/java/io/lettuce/core/dynamic/RedisCommandsSyncTest.java +++ b/src/test/java/io/lettuce/core/dynamic/RedisCommandsSyncTest.java @@ -17,6 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -79,8 +80,23 @@ public void mgetAsValues() { assertThat(values.get(1)).isEqualTo(Value.empty()); } + @Test + public void mgetByteArray() { + + redis.set(key, value); + + RedisCommandFactory factory = new RedisCommandFactory(redis.getStatefulConnection()); + + MultipleExecutionModels api = factory.getCommands(MultipleExecutionModels.class); + + List values = api.mget(Collections.singleton(key.getBytes())); + assertThat(values).hasSize(1).contains(value.getBytes()); + } + interface MultipleExecutionModels extends Commands { + List mget(Iterable keys); + String get(String key); default byte[] getAsBytes() {