2

My understanding with proto3 is that for scalars, there is no way to distinguish whether the message sender set a field to the default value or didn't set the field at all because default values aren't sent over the wire. That is, the hasField call for that field will return false regardless if the default value was set or not. And hasField will only return true if the field was set to something other than the default value.

I've read some stuff about using object as wrappers to get around this situation, but am still trying to understand how it'd work e.g: https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wrappers.proto

My question is: if all the fields of an object happen to be set to their default values, will that object still be sent over the wire? Or will hasFields for that object return False?

gunit
  • 3,700
  • 4
  • 31
  • 42

1 Answers1

2

A zero byte serialization is perfectly valid in protobuf. If nothing needs to be serialized because all fields are the default: you'll get zero bytes.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Does this mean hasField for that object will return false? For example if you have an object with 10 fields, and all 10 fields happen to be the default value, then that entire object will not be sent over the wire? Therefore calling hasField on the OBJECT will return false? Whereas if just 1 of those fields is not the default value, then that object will be sent, and calling hasField on the object will return true? – gunit Dec 13 '18 at 21:07
  • @gunit do you mean a sub-message? A sub message can exist but be zero length - just a header and a zero length prefix – Marc Gravell Dec 13 '18 at 21:29