Monday, July 11, 2022

[FIXED] What is the relation of protobuf message field id and field order?

Issue

I want to understand if the messages bellow are compatible from the perspective of protobuf and serialization/deserialization.

message HelloReply {
  string message = 1;
  string personalized_message = 2;
}
message HelloReply {
  string personalized_message = 2;
  string message = 1;
}

Does the order matter for compatibility in any situation?


Solution

The textual order is largely irrelevant, although it may impact some code generation tooling - but most languages don't care about declaration order, so even that: won't matter. The fields are still defined semantically equivalent - the numbers match the existing meaning (name) and type. It is the number that is the determining feature in identifying a field.

At the protocol level:

  • parsers must allow fields in any order
  • serializers should (but not must) write fields in ascending numerical field order


Answered By - Marc Gravell
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.