Search code examples
c#protocol-buffersdeprecation-warning

Is it possible to add a "this is what you should use" message when deprecating a Protocol buffer field?


I have a web service and to-date we have been using integer ID's OR guid values for referencing things in the database.

message Request {
  sint64 Id = 1;
  optional string Guid = 2;
  optional bool GetSoftDeletedRecords = 3;
}

I want to update the ID value to be deprecated and force users to only use the GUID value. I know I can do

message Request {
  sint64 Id = 1 [deprecated = true];
  optional string Guid = 2;
  optional bool GetSoftDeletedRecords = 3;
}

but I'd like to add a simple message Please use "Guid" field instead that is presented to the user. I'm using c# so I'm pretty sure [deprecated = true] gets translated to an obsolete attribute, is there a way to ensure that obsolete attribute has a message value?


Solution

  • is there a way to ensure that obsolete attribute has a message value?

    No, there isn't. The code to generate the obsolete attribute is in csharp_field_base.cc, and it just looks like this:

      if (descriptor_->options().deprecated()) {
        printer->Print("[global::System.ObsoleteAttribute]\n");
      } else if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE &&
               descriptor_->message_type()->options().deprecated()) {
        printer->Print("[global::System.ObsoleteAttribute]\n");
      }
    

    However, if you put a comment on the field itself, that will end up in the summary. So I'd just write:

    message Request {
      // Use the Guid field instead.
      sint64 Id = 1 [deprecated = true];
      optional string Guid = 2;
      optional bool GetSoftDeletedRecords = 3;
    }
    

    (I'd personally rename the fields to be more proto-conventional, i.e. id, guid and get_soft_deleted_records - they'll be converted into idiomatic C# property names anyway.)