Protocol buffer: does changing field name break th

2019-04-04 19:20发布

问题:

With protocol buffer, does changing field name of a message still let it compatible backward? I couldn't find any cite about that.

Eg: original message

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

Change to:

message Person {
  required string full_name = 1;
  required int32 id = 2;
  optional string email = 3;
}

回答1:

Changing a field name will not affected protobuf encoding or compatibility between applications that use proto definitions which differ only by field names.

The binary protobuf encoding is based on tag numbers, so that is what you need to preserve.

You can even change a field type to some extent (check the type table at https://developers.google.com/protocol-buffers/docs/encoding#structure) providing its wire type stays the same, but that requires additional considerations whether, for example, changing uint32 to uint64 is safe from the point of view of your application code and for some definition of 'better' is better that simply defining a new field.

Changing a field name will affect json representation, if you use that feature.