66

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;
}
tiboo
  • 8,213
  • 6
  • 33
  • 43

1 Answers1

88

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.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
  • 1
    Do you know how to handle changing a message name when that message is stored in an `Any`? – Dmitry Minkovsky Feb 13 '18 at 18:33
  • It wouldn't affect the shape of the message over the wire (the location and length of the bytes and the fields they correspond to), but if the consumer updates the .proto schema without updating the source code, won't the code break, because the code accesses fields via field names? Or is there a way to not use the field names when accessing specific fields? (E.g. if the code used to extract the `name` field and write it to a database, how it would it now know to extract the `full_name` field instead without a source code change?) – gunit Sep 17 '18 at 17:50
  • The code will not compile, but that's ok. The point of protobuf is to make sure that old clients that didn't updated the proto file will still work. – ElyashivLavi Jun 30 '19 at 08:34
  • 1
    Also beware that while the binary format doesn't care about field names, if you are using protobuf for say a web API and also support JSON, then the renamed field would break the JSON unmarshalling. – Rónán Ó Braonáin Dec 14 '21 at 01:10