Skip to content

Commit 7ac1501

Browse files
committed
chore: guard against invalid UUIDs
Some integration tests failed w/ a BufferUnderflow exception when run against v1.32.16, which we can handle more gracefully by returning null from decodeUuid(). An invalid UUID is a server-side issue and we should not try to decode it in the client.
1 parent ec2de67 commit 7ac1501

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/data/DeleteManyRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static Rpc<DeleteManyRequest, WeaviateProtoBatchDelete.BatchDeleteRequest
4646
var objects = reply.getObjectsList()
4747
.stream()
4848
.map(obj -> new DeleteManyResponse.DeletedObject(
49-
ByteStringUtil.decodeUuid(obj.getUuid()).toString(),
49+
ByteStringUtil.decodeUuid(obj.getUuid()),
5050
obj.getSuccessful(),
5151
obj.getError()))
5252
.toList();

src/main/java/io/weaviate/client6/v1/internal/grpc/ByteStringUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ private ByteStringUtil() {
1616
private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
1717

1818
/** Decode ByteString to UUID. */
19-
public static UUID decodeUuid(ByteString bs) {
19+
public static String decodeUuid(ByteString bs) {
20+
if (bs.size() != Long.BYTES * 2) {
21+
return null;
22+
}
2023
var buf = ByteBuffer.wrap(bs.toByteArray());
2124
var most = buf.getLong();
2225
var least = buf.getLong();
23-
return new UUID(most, least);
26+
return new UUID(most, least).toString();
2427
}
2528

2629
/** Encode float[] to ByteString. */

src/test/java/io/weaviate/client6/v1/internal/grpc/ByteStringUtilTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,17 @@ public void test_decodeVector_2d() {
4949
public void test_decodeUuid() {
5050
byte[] bytes = { 38, 19, -74, 24, -114, -19, 73, 43, -112, -60, 47, 96, 83, -89, -35, -23 };
5151
String want = "2613b618-8eed-492b-90c4-2f6053a7dde9";
52-
String got = ByteStringUtil.decodeUuid(ByteString.copyFrom(bytes)).toString();
52+
String got = ByteStringUtil.decodeUuid(ByteString.copyFrom(bytes));
5353
Assertions.assertThat(got).isEqualTo(want);
5454
}
5555

56+
@Test
57+
public void test_decodeUuid_bufferUnderflow() {
58+
byte[] bytes = { 38, 19 }; // A valid UUID is exactly 16 bytes
59+
String got = ByteStringUtil.decodeUuid(ByteString.copyFrom(bytes));
60+
Assertions.assertThat(got).isNull();
61+
}
62+
5663
@Test
5764
public void test_decodeVector_1d_empty() {
5865
byte[] bytes = new byte[0];

0 commit comments

Comments
 (0)