Monday, March 23, 2009

Protocol Buffers pitfalls

There are few rough edges to protobuf and Java.
One I noticed today was that if you have an enum field and do not define a default value, protobuf auto generated code sets a default for you (the first enum value). I expected it to have null but this mistake costed me with a nasty bug.
Conclusion: you must set a default for protobuf enums as in:

  enum Player {
UNKNOWN = 0;
MP3 = 1;
VIDEO = 2;
}
optional Player player = 10 [default = UNKNOWN];
The other one I saw is with repeated string option like:
repeated string person = 9;

If you set no value if the list then on serialization it blows away with an NPE when it tries to check the encoding of the string.
Conclusion: Avoid. If you must then embed it in another object which you can then use as a list item.

3 comments:

Ismael Juma March 25, 2009 at 12:48 PM  

Hi Eishay,

Did you file a bug report for the String case? A NPE seems like a bug to me.

Also, do you get notifications for the issue tracker of thrift-protobuf-compare[1]?

I just filed an issue, but there's a very old one still open that has actually been fixed.

Best,
Ismael

[1] http://code.google.com/p/thrift-protobuf-compare/issues

Eishay Smith March 25, 2009 at 1:09 PM  

Did not file a bug, will do.

I did not have an alert for issues, fixed that. The old issue there got fixed and I'll look into the one you sent and update the benchmarks.

Thanks for the issue, Eishay

Ismael Juma March 25, 2009 at 1:11 PM  

Thank you Eishay.

Creative Commons License This work by Eishay Smith is licensed under a Creative Commons Attribution 3.0 Unported License.