Protobuf3: What happens when all fields of an object happen to be set to default value

284 Views Asked by At

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?

1

There are 1 best solutions below

2
On

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.