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

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

Open
shanshin opened this issue Jan 21, 2025 · 0 comments · May be fixed by #2907
Open

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

shanshin opened this issue Jan 21, 2025 · 0 comments · May be fixed by #2907
Assignees
Labels

Comments

@shanshin
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant