File tree 2 files changed +38
-0
lines changed
commonMain/src/kotlinx/serialization/protobuf/internal
commonTest/src/kotlinx/serialization/protobuf
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,12 @@ internal class PackedArrayEncoder(
25
25
throw SerializationException (" Packing only supports primitive number types" )
26
26
}
27
27
28
+ override fun endEncode (descriptor : SerialDescriptor ) {
29
+ if (stream.size() > 0 ) {
30
+ super .endEncode(descriptor)
31
+ }
32
+ }
33
+
28
34
override fun encodeTaggedString (tag : ProtoDesc , value : String ) {
29
35
throw SerializationException (" Packing only supports primitive number types" )
30
36
}
Original file line number Diff line number Diff line change @@ -51,6 +51,12 @@ class PackedArraySerializerTest {
51
51
val s : List <String >
52
52
)
53
53
54
+ @Serializable
55
+ data class PackedIntCarrier (
56
+ @ProtoPacked
57
+ val i : List <Int >
58
+ )
59
+
54
60
/* *
55
61
* Test that when packing is specified the array is encoded as packed
56
62
*/
@@ -132,4 +138,30 @@ class PackedArraySerializerTest {
132
138
assertEquals(listData, decoded)
133
139
}
134
140
141
+ /* *
142
+ * Test that empty packed repeated field is not presented in a message.
143
+ */
144
+ @Test
145
+ fun testEncodeEmptyPackedList () {
146
+ val obj = PackedIntCarrier (emptyList())
147
+ val encoded = ProtoBuf .encodeToHexString(obj)
148
+ assertEquals(" " , encoded)
149
+ }
150
+
151
+ /* *
152
+ * Test that absence of packed field and explicit presence with a length 0 are decoded an empty list.
153
+ */
154
+ @Test
155
+ fun testDecodeEmptyPackedList () {
156
+ val explicitlyEmpty = " 0a00"
157
+
158
+ val obj = PackedIntCarrier (emptyList())
159
+
160
+ val decodedExplicitEmpty = ProtoBuf .decodeFromHexString<PackedIntCarrier >(explicitlyEmpty)
161
+ val decodedAbsentField = ProtoBuf .decodeFromHexString<PackedIntCarrier >(" " )
162
+
163
+ assertEquals(obj, decodedExplicitEmpty)
164
+ assertEquals(obj, decodedAbsentField)
165
+ }
166
+
135
167
}
You can’t perform that action at this time.
0 commit comments