11

I have a proto A that depends on proto B. Then I deprecated the field protoB:

import "protoB.proto";

message ProtoA {
  string assignmentStatus = 1;
  protoB proto_b = 2 [deprecated = true];
}

I'd think in this case I should be able to remove the import statement right? But when I did that, the compiler complains about the dependency not being imported.

What's the deal here?

HeyThere
  • 503
  • 1
  • 5
  • 19

1 Answers1

4

Marking something as deprecated just ... marks it as deprecated; for example, in C# the proto_b member would be declared but marked as [Obsolete]. Since it still exists, it needs to know what to describe it as. The data is still accessible in your application, for example.

If you want to remove it: remove it:

message ProtoA {
  string assignmentStatus = 1;
  // field 2 *was* protoB proto_b = 2, now removed
}

(leaving a comment is important to avoid people accidentally reusing the field number, which could cause problems with pre-existing data).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 22
    After removing the field you should mark it as `reserved`. This to maintain backwards compatibility while preventing new additions using the same field id. More info [here](https://developers.google.com/protocol-buffers/docs/proto3#reserved). – Bart Feb 18 '20 at 13:10
  • thank you! didn't know we can remove a field, i thought the index number is needed to maintain the compatibility. – HeyThere Feb 23 '20 at 04:43
  • 1
    @heythere nope; just don't *reuse* it to mean something else, is the main point; you also shouldn't add or remove "required" fields, but proto3 doesn't have the concept of "required" – Marc Gravell Feb 23 '20 at 09:00
  • @Bart, your comment should really be _the_ answer. Care to convert it? – Noel Yap Feb 28 '23 at 01:06
  • 1
    It is a bad practice to just remove the field. It is breaking the forward compatibility since tomorrow, the component may use the same filed index for some other information. The best approach here is to mark it as `reserved`. – Nikola Mar 28 '23 at 16:06