1

Proto3: Can I add "optional" to an existing primitive field? Is that backwards compatible?

In order to have a primitive field differentiate between 0 and unset, I was wondering if it is possible to make it "optional" or if I need to deprecate the existing field and create a new one. So:

message foo {
  int32 bar = 0;
}

Becomes either:

1)

message foo {
  optional int32 bar = 0;
}
message foo {
  int32 bar = 0 [deprecated = true];
  optional int32 new_bar = 1

(1) would be the easier change, if backwards compatible.

Paul Smith
  • 11
  • 1
  • In proto3, all fields are `optional`. You seem to confuse `optional` and `nullable`. – 273K May 31 '23 at 14:38
  • Does this answer your question? [How to define an optional field in protobuf 3](https://stackoverflow.com/questions/42622015/how-to-define-an-optional-field-in-protobuf-3) – 273K May 31 '23 at 14:40
  • @273K - From https://stackoverflow.com/a/76375904/2542822 'optional' was added in v3.15 and semantically is different from the implicit one. And that does not answer my question about migrating to an optional field from one that does not have it with respect to backwards compatibility. – Paul Smith May 31 '23 at 17:34

1 Answers1

0

The proto3 language guide has generic guidelines, but does not address changing a normal proto3 ("implicit field presence") field into an optional field.

Internally the encoding does not change between normal and optional fields. What changes is when the field gets encoded. Let's consider different cases of old proto (with normal field) and new proto (with optional field):

Old encoder proto, new decoder proto, field value non-zero: Decoder gets correct value, optional field always set.

Old encoder proto, new decoder proto, field value is zero: Decoder acts as if the optional field is unset.

New encoder proto, old decoder proto, field unset: Decoder acts as if field value is zero.

New encoder proto, old decoder proto, field set: Decoder gets correct value.

So, it is not perfectly compatible because zero value may get lost or appear for missing fields. But for many practical cases, it may be viable.

jpa
  • 10,351
  • 1
  • 28
  • 45