Question about proto message - breaking change behavior

16 views
Skip to first unread message

Aggarwal Sre

unread,
Oct 13, 2022, 2:07:12 PM10/13/22
to grpc.io
Hi All,

I recently observed some behavior w.r.t proto message - marshalling/unmarshalling. Wanted to know if there is a better way to determine the contract break specifically for code written in C#?

Client (Version 1) and server (Version 1) were using different versions of a message with some *breaking change*. My understanding *was* in case of a contract break we will see some sort of exception but turns out the field (field3) was just an empty object [Not null]. In our scenario it is possible to have an empty object for that field, so checking null or empty object won't help either.

Is there a guideline to determine a contract break at run time? Or do we need to somehow ensure in our code change review process or build process that ordinals are not changed?

Version 1
Message A {
string field1 = 1;
uint64 filed2 = 2;
map<string, int> field3 = 3;
}

Version 2
Message A {
string field1 = 1;
map<string, int> field3 = 2;
}

Regards,
Vivek 

Terry Wilson

unread,
Oct 19, 2022, 1:21:43 PM10/19/22
to grpc.io
I can't comment on the C# specifics, but when dealing with protobuf changes, one should never reuse the ordinal numbers of deleted fields. If you don't need a field anymore, you should reserve the field number. You can find some documentation on this over here.

A better approach here for your version 2 would be:

message A {
  reserved 2;
  string field1 = 1;
  map<string, int> field3 = 3;
}

This will avoid problems with coding/decoding the message, which I suspect you are experiencing here.

Hope that helps,
Terry
Reply all
Reply to author
Forward
0 new messages