Skip to content

Proto file option to preserve original field order during encoding#1658

Open
theorm wants to merge 3 commits into
protobufjs:masterfrom
theorm:feature/preserve_field_order_option
Open

Proto file option to preserve original field order during encoding#1658
theorm wants to merge 3 commits into
protobufjs:masterfrom
theorm:feature/preserve_field_order_option

Conversation

@theorm

@theorm theorm commented Oct 4, 2021

Copy link
Copy Markdown

Protobuf.js encode method always sorts fields by their ID before writing a message to a byte array. However in some cases it may be necessary to tell protobuf.js to use the original order of the fields as it was defined in the .proto file. In my case protobuf.js had to be integrated with a legacy platform that uses original field order, and protobuf.js had to produce the same base64-encoded strings as the platform.

This pull request introduces a .proto file wide custom option pbjs_encoder_field_order which, if set to ORIGINAL, instructs the encoder to keep the original order of the fields.

@alexander-fenster

alexander-fenster commented Oct 7, 2021

Copy link
Copy Markdown
Contributor

I just wanted to note that the requirement to emit the fields in any particular order violates the protobuf spec that says:

Field numbers may be used in any order in a .proto file. The order chosen has no effect on how the messages are serialized.
When a message is serialized, there is no guaranteed order for how its known or unknown fields should be written. Serialization order is an implementation detail and the details of any particular implementation may change in the future. Therefore, protocol buffer parsers must be able to parse fields in any order.

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.

2 participants