1

I am making changes to my python code that is using protobuf 3

I read that

  • string, bytes, and message fields, optional is compatible with repeated
  • changing a field between a map<K, V> and the corresponding repeated message field is binary compatible

Given required and optional is now removed from protobuf 3, I don't need to define optional for it anymore, I am wondering can I change and remove the repeated field type to a single object type and stay backward compatible? Not entirely sure if the field is automatically assume it is optional or not. If not what are my options here?

I made the code changes, and everything seems to be passing in unit tests, and I am unable to find much documentation on this, thanks for the help.

message Class {
    bool is_restricted = 1;
    repeated Student students = 2;
}

To

message Class {
    bool is_restricted = 1;
    Student student = 2;
}

Does this works and backward compatible?

Ohhh
  • 415
  • 2
  • 5
  • 24
  • 2
    By saying that this is compatible, do you mean that if the historical messages (written somewhere), that used to have either 0 or 1 item on the repeated field list can be interpreted as a new message with a non-repeated list, then I think yes. However if there is a message with 2+ it will fail to parse a new Message. Also if there is an existing client that receives the new message and attempts to parse it as an old one with repeated field it will work as well. Does it answer your question? – Piotr May 07 '21 at 23:14
  • Yes, that answers my question, thanks. I do wonder what is the reason behind it? Does protobuf 3 automatically assume field is optional? I assume this wouldn't work if I convert it down to protobuf 2 right – Ohhh May 08 '21 at 03:38
  • 1
    Protobuf 3 only has optional and repeated fields (optional is implicit, you don't write optional anymore). I think that the binary format is more like key/value pairs. Take a look into the text format as an example: https://stackoverflow.com/questions/18873924/what-does-the-protobuf-text-format-look-like So, the optional and repeated with one element is represented exactly the same. And actually required is the same as well. The only difference for required is that if key=value is missing for a given required key, an error would be thrown during parsing. – Piotr May 08 '21 at 03:46
  • So if you serialize a message with optional field (that is present) it can be parsed back as a repeated field, and as a required from proto2 as well (proto2 and proto3 are binary-compatible). Yet this would not be a good practice at all to parse optional fields as required, since if it's missing parsing would fail. – Piotr May 08 '21 at 03:49
  • Got it, thanks for the help on this. – Ohhh May 08 '21 at 04:25

0 Answers0