c++protobuf-c

Should the Protobufs from two different repositories be aligned


Problem Description

We have two codes on different repositories. One is in Java and the Other is in C++. We share a common protobuf. The problem is that on our side which is the C++ side we have less members that the one on the JAVA side. As you can see, on our work is assigned id 4, whereas on Java side it is assigned id 5. Both members have the same name which is work.

Question

If the protobufs are not aligned what problems can we have? is it ok for the protobufs not be aligned?


message CPPContext {
  optional string date              = 1;
  optional string time              = 2;
  optional string hour              = 3;
  optional string work              = 4;
}

message JAVAContext {
  optional string date              = 1;
  optional string time              = 2;
  optional string hour              = 3;
  optional string currency          = 4;
  optional string work              = 5;
}

Solution

  • Protobuf serialize and deserialize messages based on field numbers not field names.

    For example if CPPContext message gets deserialized on the other side as JAVAContext then your work field will be treated as currency field on the other side.

    It is better to use same proto files on both communicating sides. Or at least (backward-)compatible proto files. For example it is fine to add new optional fields with new field ids in proto files on one side first (they will be ignored on the other side) but it is not fine to change id of a field or remove required field.