Skip to content

Commit

Permalink
Add a specific codec for byte arrays.
Browse files Browse the repository at this point in the history
Initially, it relied on the JSON codec (encoding the byte arrays using base 64). This was breaking the compatibility with other libraries.

Fix quarkusio#28029.

(cherry picked from commit 4a2546e)
  • Loading branch information
cescoffier authored and gsmet committed Sep 20, 2022
1 parent dd9da5c commit 811c495
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 811c495

Please sign in to comment.