Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skipped encoding of empty packed collections in protobuf #2907

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ internal class PackedArrayEncoder(
throw SerializationException("Packing only supports primitive number types")
}

override fun endEncode(descriptor: SerialDescriptor) {
if (stream.size() > 0) {
super.endEncode(descriptor)
}
}

override fun encodeTaggedString(tag: ProtoDesc, value: String) {
throw SerializationException("Packing only supports primitive number types")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class PackedArraySerializerTest {
val s: List<String>
)

@Serializable
data class PackedIntCarrier(
@ProtoPacked
val i: List<Int>
)

/**
* Test that when packing is specified the array is encoded as packed
*/
Expand Down Expand Up @@ -132,4 +138,30 @@ class PackedArraySerializerTest {
assertEquals(listData, decoded)
}

/**
* Test that empty packed repeated field is not presented in a message.
*/
@Test
fun testEncodeEmptyPackedList() {
val obj = PackedIntCarrier(emptyList())
val encoded = ProtoBuf.encodeToHexString(obj)
assertEquals("", encoded)
}

/**
* Test that absence of packed field and explicit presence with a length 0 are decoded an empty list.
*/
@Test
fun testDecodeEmptyPackedList() {
val explicitlyEmpty = "0a00"

val obj = PackedIntCarrier(emptyList())

val decodedExplicitEmpty = ProtoBuf.decodeFromHexString<PackedIntCarrier>(explicitlyEmpty)
val decodedAbsentField = ProtoBuf.decodeFromHexString<PackedIntCarrier>("")

assertEquals(obj, decodedExplicitEmpty)
assertEquals(obj, decodedAbsentField)
}

}