Protocol Buffer: Changing field type from string to stringValue is backward compatible?

Viewed 869

Does changing the field type from string to stringValue will be backward compatible if field x was not used before? For example: original message

message Student {
  string x = 1;
  string name = 2;
}

Changes to:

message Student {
  google.protobuf.StringValue x = 1;
  string name = 2;
}
1 Answers

This is not a backwards-compatible change, so messages that previously set field x would not be guaranteed parse.

However, if you can guarantee that field x was never present in any message that will need to be deserialized, you'll be fine, since the field will just be absent from the serialized message. Note that this also applies to older code that tries to deserialize newer versions of the message.

In the end, it's often easier to just use a new field number:

message Student {
  google.protobuf.StringValue x = 3;
  string name = 2;

  reserved 1;
}

The reserved keyword makes sure that this field ID is never used again, even if it's deleted from the generated code.

Related