Skip to content

Commit 3126482

Browse files
authored
protobuf: Add unit test for empty map value deserialization (#2695)
Motivation: It is valid to not encode an empty map value and token, and we don't have a unit test to verify this behavior.
1 parent c9b1fc5 commit 3126482

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

servicetalk-data-protobuf/src/test/java/io/servicetalk/data/protobuf/ProtobufSerializerFactoryTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package io.servicetalk.data.protobuf;
1717

1818
import io.servicetalk.buffer.api.Buffer;
19+
import io.servicetalk.buffer.api.BufferAllocator;
1920
import io.servicetalk.data.protobuf.test.TestProtos.DummyMessage;
21+
import io.servicetalk.data.protobuf.test.TestProtos.MapMessage;
2022
import io.servicetalk.serializer.api.SerializerDeserializer;
2123
import io.servicetalk.serializer.api.StreamingSerializerDeserializer;
2224

25+
import com.google.protobuf.InvalidProtocolBufferException;
2326
import com.google.protobuf.Parser;
2427
import org.junit.jupiter.api.Test;
2528
import org.junit.jupiter.params.ParameterizedTest;
@@ -69,6 +72,20 @@ void serializeDeserializeClass() {
6972
serializeDeserialize(testMessage, PROTOBUF.serializerDeserializer(DummyMessage.class));
7073
}
7174

75+
@Test
76+
void serializeEmptyMap() throws InvalidProtocolBufferException {
77+
// These bytes were serialized from scala protobuf and don't include the trailing empty map value.
78+
final byte[] emptyMessageMapEmptyValueBytes = {0xa, 0x5, 0xa, 0x3, 0x66, 0x6f, 0x6f};
79+
final BufferAllocator allocator = DEFAULT_ALLOCATOR;
80+
final Buffer buffer = allocator.wrap(emptyMessageMapEmptyValueBytes);
81+
final MapMessage mapMessage = MapMessage.parseFrom(emptyMessageMapEmptyValueBytes);
82+
SerializerDeserializer<MapMessage> serializer = PROTOBUF.serializerDeserializer(MapMessage.class);
83+
assertThat(serializer.deserialize(buffer, allocator), equalTo(mapMessage));
84+
// Serializing in java currently writes "0x12, 0x0" for "map value tag, empty string" so we can't compare number
85+
// of bytes directly, but we can ensure it deserializes back to an equal object.
86+
assertThat(serializer.deserialize(serializer.serialize(mapMessage, allocator), allocator), equalTo(mapMessage));
87+
}
88+
7289
private static void serializeDeserialize(final DummyMessage testMessage,
7390
final SerializerDeserializer<DummyMessage> serializer) {
7491
final byte[] testMessageBytes = testMessage.toByteArray();

servicetalk-data-protobuf/src/test/proto/test_message.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ option java_outer_classname = "TestProtos";
2525
message DummyMessage {
2626
string message = 1;
2727
}
28+
29+
message MapMessage {
30+
map<string, string> attributes = 1;
31+
}

0 commit comments

Comments
 (0)