Skip to content

Commit

Permalink
Merge pull request #28036 from cescoffier/fix-binary-support-in-redis
Browse files Browse the repository at this point in the history
Add a specific codec for byte arrays
  • Loading branch information
gsmet authored Sep 19, 2022
2 parents 1deb916 + 4a2546e commit b6f6718
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static <T> Codec<T> getDefaultCodecFor(Class<T> clazz) {
if (clazz.equals(String.class)) {
return (Codec<T>) StringCodec.INSTANCE;
}
if (clazz.equals(byte[].class)) {
return (Codec<T>) ByteArrayCodec.INSTANCE;
}
// JSON by default
return new JsonCodec<>(clazz);
}
Expand Down Expand Up @@ -114,4 +117,23 @@ public Integer decode(byte[] item) {
}
}

public static class ByteArrayCodec implements Codec<byte[]> {

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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<String, String> cmd = ds.value(String.class);
String str = cmd.get(key);
assertThatThrownBy(() -> Json.decodeValue(str, byte[].class)).isInstanceOf(DecodeException.class);
}
}

0 comments on commit b6f6718

Please sign in to comment.