Skip to content

Add an option to encode fields in tag order - #3751

Open
oldergod wants to merge 7 commits into
masterfrom
bquenaudon/encode-in-tag-order
Open

oldergod wants to merge 7 commits into
masterfrom
bquenaudon/encode-in-tag-order

Conversation

@oldergod

@oldergod oldergod commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

This adds encodeInTagOrder to the kotlin {} and java {} targets.
It is false by default.

With the option on, the generated encode functions write fields in ascending tag order.
The bytes are then the same as protoc.
This includes oneof fields and extension fields.
In Kotlin, a boxed or sealed oneof splits around plain fields that have tags between its members.

With the option off, the generated code does not change.

The option applies only to the types that the target generates.
A nested type that another module or jar generates keeps its own field order.

You cannot use compact and encodeInTagOrder together on a java {} target, because compact code encodes by reflection.
The Gradle plugin fails before code generation when both are set.

Closes #3746.

Release note:

  • New: encodeInTagOrder option on the kotlin {} and java {} targets. When it is true, the generated encode functions write fields in ascending tag order, so the bytes are the same as protoc. It is false by default, and generated code does not change when it is false (Add an option to encode fields in tag order #3751)
  • Binary change: the constructors and copy functions of KotlinTarget and JavaTarget, the constructors of KotlinSchemaHandler and JavaSchemaHandler, and KotlinGenerator.invoke have a new last parameter encodeInTagOrder with the default value false. Kotlin source that calls them compiles without change. Java source that calls them must add the new argument. Code that is compiled against an earlier Wire version and calls them directly must be compiled again (Add an option to encode fields in tag order #3751)

@oldergod
oldergod marked this pull request as ready for review November 5, 2026 12:55
The new encodeInTagOrder option is for the Kotlin and Java targets.
With it on, generated encode functions write fields in ascending tag order, so the bytes are the same as protoc.
The default is false, and it does not change generated code.
The Java generator fails when compact and encodeInTagOrder are both set.

Closes #3746.
The last run of a split boxed oneof now writes every key that no earlier run writes.
Before, a custom key with a tag in no run was not written, but encodedSize counted it.
Runs also grow in place, so code generation time is linear in the oneof size.
The encode function no longer scans all segments for each split run.
@oldergod
oldergod force-pushed the bquenaudon/encode-in-tag-order branch from c0c4a5b to e43ea8c Compare November 5, 2026 13:22

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kotlin encode() writes fields in declaration order (oneof members last), so the bytes differ from protobuf-java

1 participant