This application note explains the various presence tracking disciplines for protobuf fields. It also explains the behaviour of explicit presence tracking for singular proto3 fields with basic types.
Field presence is the notion of whether a protobuf field has a value. There are two different manifestations of presence for protobufs: no presence, where the generated message API stores field values (only), and explicit presence, where the API also stores whether or not a field has been set.
Historically, proto2 has mostly followed explicit presence, while proto3 exposes only no presence semantics. Singular proto3 fields of basic types (numeric, string, bytes, and enums) which are defined with the optional
label have explicit presence, like proto2 (this feature is enabled by default as release 3.15).
Presence disciplines define the semantics for translating between the API representation and the serialized representation. The no presence discipline relies upon the field value itself to make decisions at (de)serialization time, while the explicit presence discipline relies upon the explicit tracking state instead.
The wire format is a stream of tagged, self-delimiting values. By definition, the wire format represents a sequence of present values. In other words, every value found within a serialization represents a present field; furthermore, the serialization contains no information about not-present values.
The generated API for a proto message includes (de)serialization definitions which translate between API types and a stream of definitionally present (tag, value) pairs. This translation is designed to be forward- and backward-compatible across changes to the message definition; however, this compatibility introduces some (perhaps surprising) considerations when deserializing wire-formatted messages:
repeated
fields are typically appended to the field's API representation. (Note that serializing a packed repeated field produces only one, length-delimited value in the tag stream.)optional
field values follow the rule that “the last one wins.”oneof
fields expose the API-level invariant that only one field is set at a time. However, the wire format may include multiple (tag, value) pairs which notionally belong to the oneof
. Similar to optional
fields, the generated API follows the “last one wins” rule.Protobufs can be represented in human-readable, textual forms. Two notable formats are TextFormat (the output format produced by generated message DebugString
methods) and JSON.
These formats have correctness requirements of their own, and are generally stricter than tagged-value stream formats. However, TextFormat more closely mimics the semantics of the wire format, and does, in certain cases, provide similar semantics (for example, appending repeated name-value mappings to a repeated field). In particular, similar to the wire format, TextFormat only includes fields which are present.
JSON is a much stricter format, however, and cannot validly represent some semantics of the wire format or TextFormat.
null
value, which may be used to represent a defined but not-present field.oneof
fields unambiguously: if multiple cases are present, they are unordered.In theory, JSON can represent presence in a semantic-preserving fashion. In practice, however, presence correctness can vary depending upon implementation choices, especially if JSON was chosen as a means to interoperate with clients not using protobufs.
This table outlines whether presence is tracked for fields in proto2 APIs (both for generated APIs and using dynamic reflection):
Field type | Explicit Presence |
---|---|
Singular numeric (integer or floating point) | ✔️ |
Singular enum | ✔️ |
Singular string or bytes | ✔️ |
Singular message | ✔️ |
Repeated | |
Oneofs | ✔️ |
Maps |
Singular fields (of all types) track presence explicitly in the generated API. The generated message interface includes methods to query presence of fields. For example, the field foo
has a corresponding has_foo
method. (The specific name follows the same language-specific naming convention as the field accessors.) These methods are sometimes referred to as “hazzers” within the protobuf implementation.
Similar to singular fields, oneof
fields explicitly track which one of the members, if any, contains a value. For example, consider this example oneof
:
oneof foo { int32 a = 1; float b = 2; }
Depending on the target language, the generated API would generally include several methods:
has_foo
foo
has_a
, has_b
a
, b
Repeated fields and maps do not track presence: there is no distinction between an empty and a not-present repeated field.
This table outlines whether presence is tracked for fields in proto3 APIs (both for generated APIs and using dynamic reflection):
Field type | optional | Explicit Presence |
---|---|---|
Singular numeric (integer or floating point) | No | |
Singular enum | No | |
Singular string or bytes | No | |
Singular numeric (integer or floating point) | Yes | ✔️ |
Singular enum | Yes | ✔️ |
Singular string or bytes | Yes | ✔️ |
Singular message | Yes | ✔️ |
Singular message | No | ✔️ |
Repeated | N/A | |
Oneofs | N/A | ✔️ |
Maps | N/A |
Similar to proto2 APIs, proto3 does not track presence explicitly for repeated fields. Without the optional
label, proto3 APIs do not track presence for basic types (numeric, string, bytes, and enums), either. Oneof fields affirmatively expose presence, although the same set of hazzer methods may not generated as in proto2 APIs.
Under the no presence discipline, the default value is synonymous with “not present” for purposes of serialization. To notionally “clear” a field (so it won't be serialized), an API user would set it to the default value.
The default value for enum-typed fields under no presence is the corresponding 0-valued enumerator. Under proto3 syntax rules, all enum types are required to have an enumerator value which maps to 0. By convention, this is an UNKNOWN
or similarly-named enumerator. If the zero value is notionally outside the domain of valid values for the application, this behavior can be thought of as tantamount to explicit presence.
The no presence serialization discipline results in visible differences from the explicit presence tracking discipline, when the default value is set. For a singular field with numeric, enum, or string type:
has_foo
method indicates whether or not the field foo
has been set (and not cleared).clear_foo
method must be used to clear (i.e., un-set) the value.Under the no presence rules, it is effectively impossible for a target field to merge-from its default value (using the protobuf's API merging functions). This is because default values are skipped, similar to the no presence serialization discipline. Merging only updates the target (merged-to) message using the non-skipped values from the update (merged-from) message.
The difference in merging behavior has further implications for protocols which rely on partial “patch” updates. If field presence is not tracked, then an update patch alone cannot represent an update to the default value, because only non-default values are merged-from.
Updating to set a default value in this case requires some external mechanism, such as FieldMask
. However, if presence is tracked, then all explicitly-set values -- even default values -- will be merged into the target.
Changing a field between explicit presence and no presence is a binary-compatible change for serialized values in wire format. However, the serialized representation of the message may differ, depending on which version of the message definition was used for serialization. Specifically, when a “sender” explicitly sets a field to its default value:
This change may or may not be safe, depending on the application's semantics. For example, consider two clients with different versions of a message definition.
Client A uses this definition of the message, which follows the explicit presence serialization discipline for field foo
:
syntax = "proto3"; message Msg { optional int32 foo = 1; }
Client B uses a definition of the same message, except that it follows the no presence discipline:
syntax = "proto3"; message Msg { int32 foo = 1; }
Now, consider a scenario where client A observes foo
's presence as the clients repeatedly exchange the “same” message by deserializing and reserializing:
// Client A: Msg m_a; m_a.set_foo(1); // non-default value assert(m_a.has_foo()); // OK Send(m_a.SerializeAsString()); // to client B // Client B: Msg m_b; m_b.ParseFromString(Receive()); // from client A assert(m_b.foo() == 1); // OK Send(m_b.SerializeAsString()); // to client A // Client A: m_a.ParseFromString(Receive()); // from client B assert(m_a.foo() == 1); // OK assert(m_a.has_foo()); // OK m_a.set_foo(0); // default value Send(m_a.SerializeAsString()); // to client B // Client B: Msg m_b; m_b.ParseFromString(Receive()); // from client A assert(m_b.foo() == 0); // OK Send(m_b.SerializeAsString()); // to client A // Client A: m_a.ParseFromString(Receive()); // from client B assert(m_a.foo() == 0); // OK assert(m_a.has_foo()); // FAIL
If client A depends on explicit presence for foo
, then a “round trip” through client B will be lossy from the perspective of client A. In the example, this is not a safe change: client A requires (by assert
) that the field is present; even without any modifications through the API, that requirement fails in a value- and peer-dependent case.
These are the general steps to use field tracking support for proto3:
optional
field to a .proto
file.protoc
(at least v3.15, or v3.12 using --experimental_allow_proto3_optional
flag)..proto
file changesThis is an example of a proto3 message with fields which follow both no presence and explicit presence semantics:
syntax = "proto3"; package example; message MyMessage { // No presence: int32 not_tracked = 1; // Explicit presence: optional int32 tracked = 2; }
protoc
invocationPresence tracking for proto3 messages is enabled by default since v3.15.0 release, formerly up until v3.12.0 the --experimental_allow_proto3_optional
flag was required when using presence tracking with protoc.
The generated code for proto3 fields with explicit presence (the optional
label) will be the same as it would be in a proto2 file.
This is the definition used in the “no presence” examples below:
syntax = "proto3"; package example; message Msg { int32 foo = 1; }
This is the definition used in the “explicit presence” examples below:
syntax = "proto3"; package example; message Msg { optional int32 foo = 1; }
In the examples, a function GetProto
constructs and returns a message of type Msg
with unspecified contents.
No presence:
Msg m = GetProto(); if (m.foo() != 0) { // "Clear" the field: m.set_foo(0); } else { // Default value: field may not have been present. m.set_foo(1); }
Explicit presence:
Msg m = GetProto(); if (m.has_foo()) { // Clear the field: m.clear_foo(); } else { // Field is not present, so set it. m.set_foo(1); }
No presence:
var m = GetProto(); if (m.Foo != 0) { // "Clear" the field: m.Foo = 0; } else { // Default value: field may not have been present. m.Foo = 1; }
Explicit presence:
var m = GetProto(); if (m.HasFoo) { // Clear the field: m.ClearFoo(); } else { // Field is not present, so set it. m.Foo = 1; }
No presence:
m := GetProto() if m.Foo != 0 { // "Clear" the field: m.Foo = 0 } else { // Default value: field may not have been present. m.Foo = 1 }
Explicit presence:
m := GetProto() if m.Foo != nil { // Clear the field: m.Foo = nil } else { // Field is not present, so set it. m.Foo = proto.Int32(1) }
These examples use a Builder
to demonstrate clearing. Simply checking presence and getting values from a Builder
follows the same API as the message type.
No presence:
Msg.Builder m = GetProto().toBuilder(); if (m.getFoo() != 0) { // "Clear" the field: m.setFoo(0); } else { // Default value: field may not have been present. m.setFoo(1); }
Explicit presence:
Msg.Builder m = GetProto().toBuilder(); if (m.hasFoo()) { // Clear the field: m.clearFoo() } else { // Field is not present, so set it. m.setFoo(1); }
No presence:
m = example.Msg() if m.foo != 0: # "Clear" the field: m.foo = 0 else: # Default value: field may not have been present. m.foo = 1
Explicit presence:
m = example.Msg() if m.HasField('foo'): # Clear the field: m.ClearField('foo') else: # Field is not present, so set it. m.foo = 1
No presence:
m = Msg.new if m.foo != 0 # "Clear" the field: m.foo = 0 else # Default value: field may not have been present. m.foo = 1 end
Explicit presence:
m = Msg.new if m.has_foo? # Clear the field: m.clear_foo else # Field is not present, so set it. m.foo = 1 end
No presence:
var m = new Msg(); if (m.getFoo() != 0) { // "Clear" the field: m.setFoo(0); } else { // Default value: field may not have been present. m.setFoo(1); }
Explicit presence:
var m = new Msg(); if (m.hasFoo()) { // Clear the field: m.clearFoo() } else { // Field is not present, so set it. m.setFoo(1); }
No presence:
Msg *m = [[Msg alloc] init]; if (m.foo != 0) { // "Clear" the field: m.foo = 0; } else { // Default value: field may not have been present. m.foo = 1; }
Explicit presence:
Msg *m = [[Msg alloc] init]; if (m.hasFoo()) { // Clear the field: [m clearFoo]; } else { // Field is not present, so set it. [m setFoo:1]; }