Skip to content

Commit cf5483f

Browse files
authored
Java: Fix byte GlideString conversion to String bug (#2271)
* Java: Fix byte GlideString conversion to String bug Signed-off-by: Guian Gumpac <guian.gumpac@improving.com>
1 parent ba7af10 commit cf5483f

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
* Core: Change FUNCTION STATS command to return multi node response for standalone mode ([#2117](https://github.com/valkey-io/valkey-glide/pull/2117))
125125

126126
#### Fixes
127+
* Java: Fix GlideString conversion from byte to String ([#2271](https://github.com/valkey-io/valkey-glide/pull/2271))
127128
* Java: Add overloads for XADD to allow duplicate entry keys ([#1970](https://github.com/valkey-io/valkey-glide/pull/1970))
128129
* Node: Fix ZADD bug where command could not be called with only the `changed` optional parameter ([#1995](https://github.com/valkey-io/valkey-glide/pull/1995))
129130
* Java: `XRange`/`XRevRange` should return `null` instead of `GlideException` when given a negative count ([#1920](https://github.com/valkey-io/valkey-glide/pull/1920))

java/client/src/main/java/glide/utils/ArrayTransformUtils.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ public static String[] convertMapToKeyValueStringArray(Map<String, ?> args) {
3232
}
3333

3434
/**
35-
* Converts a map of GlideString keys and values of any type in to an array of GlideStrings with
36-
* alternating keys and values.
35+
* Converts a map of GlideString keys and values to an array of GlideStrings.
3736
*
38-
* @param args Map of GlideString keys to values of any type to convert.
37+
* @param args Map of GlideString keys to values of GlideString.
3938
* @return Array of strings [key1, gs(value1.toString()), key2, gs(value2.toString()), ...].
4039
*/
41-
public static GlideString[] convertMapToKeyValueGlideStringArray(Map<GlideString, ?> args) {
40+
public static GlideString[] convertMapToKeyValueGlideStringArray(
41+
Map<GlideString, GlideString> args) {
4242
return args.entrySet().stream()
43-
.flatMap(entry -> Stream.of(entry.getKey(), GlideString.gs(entry.getValue().toString())))
43+
.flatMap(entry -> Stream.of(entry.getKey(), entry.getValue()))
4444
.toArray(GlideString[]::new);
4545
}
4646

@@ -64,10 +64,10 @@ public static String[] convertNestedArrayToKeyValueStringArray(String[][] args)
6464
}
6565

6666
/**
67-
* Converts a nested array of GlideString keys and values of any type in to an array of
68-
* GlideStrings with alternating keys and values.
67+
* Converts a nested array of GlideString keys and values in to an array of GlideStrings with
68+
* alternating keys and values.
6969
*
70-
* @param args Nested array of GlideString keys to values of any type to convert.
70+
* @param args Nested array of GlideString keys and values to convert.
7171
* @return Array of strings [key1, gs(value1.toString()), key2, gs(value2.toString()), ...].
7272
*/
7373
public static GlideString[] convertNestedArrayToKeyValueGlideStringArray(GlideString[][] args) {
@@ -78,7 +78,7 @@ public static GlideString[] convertNestedArrayToKeyValueGlideStringArray(GlideSt
7878
}
7979
}
8080
return Arrays.stream(args)
81-
.flatMap(entry -> Stream.of(entry[0], GlideString.gs(entry[1].toString())))
81+
.flatMap(entry -> Stream.of(entry[0], entry[1]))
8282
.toArray(GlideString[]::new);
8383
}
8484

@@ -99,10 +99,10 @@ public static String[] convertMapToValueKeyStringArray(Map<String, ?> args) {
9999
* Converts a map of GlideString keys and values of any type into an array of GlideStrings with
100100
* alternating values and keys.
101101
*
102-
* @param args Map of GlideString keys to values of any type to convert.
102+
* @param args Map of GlideString keys to values of Double type to convert.
103103
* @return Array of GlideStrings [gs(value1.toString()), key1, gs(value2.toString()), key2, ...].
104104
*/
105-
public static GlideString[] convertMapToValueKeyStringArrayBinary(Map<GlideString, ?> args) {
105+
public static GlideString[] convertMapToValueKeyStringArrayBinary(Map<GlideString, Double> args) {
106106
return args.entrySet().stream()
107107
.flatMap(entry -> Stream.of(gs(entry.getValue().toString()), entry.getKey()))
108108
.toArray(GlideString[]::new);

java/integTest/src/test/java/glide/SharedCommandTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,40 @@ public void hset_hget_existing_fields_non_existing_fields(BaseClient client) {
984984
assertNull(client.hget(key, "non_existing_field").get());
985985
}
986986

987+
@SneakyThrows
988+
@ParameterizedTest(autoCloseArguments = false)
989+
@MethodSource("getClients")
990+
public void non_UTF8_GlideString_test(BaseClient client) {
991+
byte[] nonUTF8Bytes = new byte[] {(byte) 0xEE};
992+
GlideString key = gs(nonUTF8Bytes);
993+
GlideString hashKey = gs(UUID.randomUUID().toString());
994+
GlideString hashNonUTF8Key = gs(new byte[] {(byte) 0xFF});
995+
GlideString value = gs(nonUTF8Bytes);
996+
String stringField = "field";
997+
Map<GlideString, GlideString> fieldValueMap = Map.of(gs(stringField), value);
998+
999+
// Testing keys and values using byte[] that cannot be converted to UTF-8 Strings.
1000+
assertEquals(OK, client.set(key, value).get());
1001+
assertEquals(value, client.get(key).get());
1002+
1003+
// Testing set values using byte[] that cannot be converted to UTF-8 Strings.
1004+
assertEquals(1, client.hset(hashKey, fieldValueMap).get());
1005+
assertDeepEquals(new GlideString[] {gs(stringField)}, client.hkeys(hashKey).get());
1006+
assertThrows(
1007+
ExecutionException.class, () -> client.hget(hashKey.toString(), stringField).get());
1008+
1009+
// Testing keys for a set using byte[] that cannot be converted to UTF-8 Strings returns bytes.
1010+
assertEquals(1, client.hset(hashNonUTF8Key, fieldValueMap).get());
1011+
assertDeepEquals(new GlideString[] {gs(stringField)}, client.hkeys(hashNonUTF8Key).get());
1012+
// No error is thrown as GlideString will be returned when arguments are GlideStrings.
1013+
assertEquals(value, client.hget(hashNonUTF8Key, gs(stringField)).get());
1014+
1015+
// Converting non UTF-8 bytes result to String returns a message.
1016+
assertEquals(
1017+
"Value not convertible to string: byte[] 13",
1018+
client.hget(hashNonUTF8Key, gs(stringField)).get().toString());
1019+
}
1020+
9871021
@SneakyThrows
9881022
@ParameterizedTest(autoCloseArguments = false)
9891023
@MethodSource("getClients")

0 commit comments

Comments
 (0)