Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol buffer: does changing field name break the message?

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;
}
like image 622
tiboo Avatar asked Aug 01 '17 07:08

tiboo


People also ask

Can you rename in Protobuf?

Renaming a field - With Protobuf content, the field names are only used in generated code. The field number is used to identify fields on the network. Renaming a field isn't a protocol breaking change for Protobuf. However, if a server is using JSON content then renaming a field is a breaking change.

Can you remove field from Protobuf?

Removing fields is fine, although you might want to mark it reserved so that nobody reuses it in an incompatible way. New code with old data (with the field) will silently ignore it; old code with new data will just load without the field populated, since everything in proto3 is implicitly optional .

How does protocol buffer work?

Protocol buffers are a combination of the definition language (created in . proto files), the code that the proto compiler generates to interface with data, language-specific runtime libraries, and the serialization format for data that is written to a file (or sent across a network connection).

Why are Protobuf fields numbered?

Field numbers are an important part of Protobuf. They're used to identify fields in the binary encoded data, which means they can't change from version to version of your service. The advantage is that backward compatibility and forward compatibility are possible.


1 Answers

Changing a field name will not affect the 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 than simply defining a new field.

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

like image 190
Oleg Estekhin Avatar answered Oct 05 '22 13:10

Oleg Estekhin