Skip to content

Encode packed fields as packed in the schema adapter - #3749

Open
oldergod wants to merge 3 commits into
masterfrom
bquenaudon/schema-adapter-packed
Open

oldergod wants to merge 3 commits into
masterfrom
bquenaudon/schema-adapter-packed

Conversation

@oldergod

@oldergod oldergod commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

SchemaProtoAdapterFactory mapped EncodeMode.PACKED to WireField.Label.REPEATED.
So Schema.protoAdapter wrote proto2 [packed = true] fields and proto3 repeated scalars unpacked.
For example, proto3 repeated int32 with 1, 2, 3 gave 080108020803 instead of 0a03010203.

This change maps PACKED to WireField.Label.PACKED, so encode and encodedSize match protoc.
Decoding still accepts both forms, and [packed = false] still writes unpacked.
The schema enum adapter now sizes values as unsigned 32-bit varints, as it writes them and as generated enum adapters do.
Before, a negative enum value got a wrong packed length prefix.

SchemaEncoder uses this adapter for option values, so custom options with packed fields now encode packed, as protoc does.
A new SchemaEncoderTest case checks these bytes.

New tests compare the adapter output with protobuf-java DynamicMessage bytes for every packable type in proto2 and proto3.

Release note: Schema.protoAdapter now writes packed fields packed.
The file descriptor bytes that SchemaEncoder and the gRPC reflection service in wire-reflector return also change when a custom option has a packed field.

The dynamic adapter mapped packed fields to the repeated label.
It wrote proto2 [packed = true] fields and proto3 repeated scalars unpacked.
Map them to the packed label so encode and encodedSize match protoc.
Decoding still accepts both forms.
The schema enum adapter sized values as signed 32-bit varints but wrote them as unsigned ones.
Negative values took 10 bytes in the size and 5 bytes on the wire.
Packed fields use that size as their length prefix, so the bytes were malformed.
Size values as unsigned 32-bit varints, as generated enum adapters do.
@oldergod
oldergod marked this pull request as ready for review November 5, 2026 12:42
SchemaEncoder encodes option values with the schema adapter.
This test checks the bytes of a packed custom option.
@oldergod
oldergod force-pushed the bquenaudon/schema-adapter-packed branch from 83eb52d to 27099c0 Compare November 9, 2026 17:35

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.

1 participant