Safeness of changing proto field number

Viewed 1201

For example if I changed from:

message Request {
  int foo = 1;
}

to

message Request {
  int bar = 1;
  int foo = 2;
}

Is it safe to change foo from 1 to 2? Docs say not to: These numbers are used to identify your fields in the message binary format, and should not be changed once your message type is in use., but I'd like to know why.

1 Answers

If you have a serialized version of the message generated with the first version, you will no be able to deserialize with the second version of the message. If you use protobuf to generate a model to store in a DB or to be published in a broker like Apache Kafka, you need to follow the convention. If you use proto buffer to generate model and service for online usage, you should do not break anything (if you will regenerate all the models)

See also the reserved keyword in order to do not reuse an old numer. Further reading also here

Related