Skip to content

Commit 47cdf69

Browse files
authored
Fix #506: support UUID values with Protobuf (#507)
1 parent b08f5fc commit 47cdf69

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schemagen/ProtobufSchemaHelper.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.dataformat.protobuf.schemagen;
22

33
import java.nio.ByteBuffer;
4+
import java.util.UUID;
45

56
import com.fasterxml.jackson.databind.*;
67

@@ -55,6 +56,8 @@ public static boolean hasIndex(BeanProperty writer) {
5556

5657
public static boolean isBinaryType(JavaType type) {
5758
return type.hasRawClass(byte[].class)
59+
// 24-Jul-2024, tatu: [dataformats-binary#506] UUID as Binary
60+
|| type.hasRawClass(UUID.class)
5861
|| type.isTypeOrSubTypeOf(ByteBuffer.class);
5962
}
6063
}

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/schema/SchemaWithUUIDTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.fasterxml.jackson.dataformat.protobuf.schema;
22

33
import java.nio.ByteBuffer;
4+
import java.nio.charset.StandardCharsets;
45
import java.util.UUID;
56

7+
import org.junit.Assert;
8+
69
import com.fasterxml.jackson.annotation.JsonProperty;
710
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
811
import com.fasterxml.jackson.dataformat.protobuf.ProtobufTestBase;
@@ -43,6 +46,17 @@ public void testWithUUID() throws Exception
4346
{
4447
ProtobufSchema schema = MAPPER.generateSchemaFor(UUIDBean.class);
4548
assertNotNull(schema);
49+
50+
UUIDBean input = new UUIDBean();
51+
input.messageId = UUID.nameUUIDFromBytes("abc".getBytes(StandardCharsets.UTF_8));
52+
53+
byte[] proto = MAPPER.writer().with(schema)
54+
.writeValueAsBytes(input);
55+
UUIDBean result = MAPPER.readerFor(UUIDBean.class)
56+
.with(schema)
57+
.readValue(proto);
58+
assertNotNull(result.messageId);
59+
assertEquals(input.messageId, result.messageId);
4660
}
4761

4862
// [dataformats-binary#68]
@@ -56,5 +70,17 @@ public void testWithBinary() throws Exception
5670
{
5771
ProtobufSchema schema = MAPPER.generateSchemaFor(BinaryBean.class);
5872
assertNotNull(schema);
73+
74+
// But let's try round-tripping too
75+
BinaryBean input = new BinaryBean();
76+
input.data = new byte[] { 1, 2, -1 };
77+
78+
byte[] proto = MAPPER.writer().with(schema)
79+
.writeValueAsBytes(input);
80+
BinaryBean result = MAPPER.readerFor(BinaryBean.class)
81+
.with(schema)
82+
.readValue(proto);
83+
assertNotNull(result.data);
84+
Assert.assertArrayEquals(input.data, result.data);
5985
}
6086
}

release-notes/VERSION-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Active maintainers:
1818

1919
- No changes since 2.17
2020

21+
2.17.3 (not yet released)
22+
23+
#506: Cannot deserialize `UUID` values
24+
(reported by @uniquonil)
25+
2126
2.17.2 (05-Jul-2024)
2227

2328
#497: (ion) Failed copy(): `IonValueMapper` does not override copy()

0 commit comments

Comments
 (0)