diff --git a/extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/codecs/Codecs.java b/extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/codecs/Codecs.java index f08485acd6673..4c99de87a09ad 100644 --- a/extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/codecs/Codecs.java +++ b/extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/codecs/Codecs.java @@ -22,6 +22,9 @@ public static Codec getDefaultCodecFor(Class clazz) { if (clazz.equals(String.class)) { return (Codec) StringCodec.INSTANCE; } + if (clazz.equals(byte[].class)) { + return (Codec) ByteArrayCodec.INSTANCE; + } // JSON by default return new JsonCodec<>(clazz); } @@ -114,4 +117,23 @@ public Integer decode(byte[] item) { } } + public static class ByteArrayCodec implements Codec { + + public static ByteArrayCodec INSTANCE = new ByteArrayCodec(); + + private ByteArrayCodec() { + // Avoid direct instantiation; + } + + @Override + public byte[] encode(byte[] item) { + return item; + } + + @Override + public byte[] decode(byte[] item) { + return item; + } + } + } diff --git a/extensions/redis-client/runtime/src/test/java/io/quarkus/redis/datasource/ValueCommandsTest.java b/extensions/redis-client/runtime/src/test/java/io/quarkus/redis/datasource/ValueCommandsTest.java index 0bd8815e307a2..387794660d75e 100644 --- a/extensions/redis-client/runtime/src/test/java/io/quarkus/redis/datasource/ValueCommandsTest.java +++ b/extensions/redis-client/runtime/src/test/java/io/quarkus/redis/datasource/ValueCommandsTest.java @@ -20,6 +20,8 @@ import io.quarkus.redis.datasource.value.SetArgs; import io.quarkus.redis.datasource.value.ValueCommands; import io.quarkus.redis.runtime.datasource.BlockingRedisDataSourceImpl; +import io.vertx.core.json.DecodeException; +import io.vertx.core.json.Json; public class ValueCommandsTest extends DatasourceTestBase { @@ -274,5 +276,10 @@ void binary() { commands.set(key, content); byte[] bytes = commands.get(key); assertThat(bytes).isEqualTo(content); + + // Verify that we do not get through the JSON codec (which would base64 encode the byte[]) + ValueCommands cmd = ds.value(String.class); + String str = cmd.get(key); + assertThatThrownBy(() -> Json.decodeValue(str, byte[].class)).isInstanceOf(DecodeException.class); } }