Skip to content

[Protobuf] Encode empty packed repeated filed as field absence #2906

@shanshin

Description

@shanshin

What is your use-case and why do you need this feature?
In current implementation packed fields are always presents in the encoded output even collection is empty.

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

...
    @Test
    fun testEncodeEmptyPackedList() {
        val obj = PackedIntCarrier(emptyList())
        val encoded = ProtoBuf.encodeToHexString(obj)
        println(encoded)
    }

this will prints 0a00 - field 1 with empty length-delimited value

Describe the solution you'd like
The protobuf documentation does not explicitly specify how empty fields should be encoded.

Based on the principle that we should try to create messages of the smallest size, and that the Java message class created using the standard com.google.protobuf:protoc compiler also does not write an empty value - we should also stop encoding them.

In this case the output of the previous example should be empty line.

Impact on decoding

The decoding behavior will not change in any way, the input messages "" and "0a00" will give the same result - an empty list in l property.

Impact on nullability

Currently, the null for value for collections cannot be encoded in any way, so this change will not affect the nullability in any way.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions