AIP-203
Field behavior documentation
When defining fields in protocol buffers, it is customary to explain to users certain aspects of the field's behavior (such as whether it is required or optional). Additionally, it can be useful for other tools to understand this behavior (for example, to optimize client library signatures).
Guidance
APIs use the google.api.field_behavior annotation to describe well-understood
field behavior, such as a field being required or immutable.
// The audio data to be recognized.
RecognitionAudio audio = 2 [(google.api.field_behavior) = REQUIRED];
- APIs must apply the
google.api.field_behaviorannotation on every field on a message or sub-message used in a request.- An exception to this is the AIP-154
etagfield on a resource message, which should not have anygoogle.api.field_behaviorassigned. - Furthermore,
oneoffields may omitgoogle.api.field_behavior, but should document any specific behaviors in the comments in this case.
- An exception to this is the AIP-154
- The annotation must include any google.api.FieldBehavior values that
accurately describe the behavior of the field.
FIELD_BEHAVIOR_UNSPECIFIEDmust not be used.
- APIs must at minimum use one of
REQUIRED,OPTIONAL, orOUTPUT_ONLY.
Warning: Although field_behavior does not impact proto-level behavior,
many clients (e.g. CLIs and SDKs) rely on them to generate code. Thoroughly
review and consider which values are relevant when adding a new field.
Fields with no annotation are interpreted as OPTIONAL for
backwards-compatility. Nontheless, this annotation must not be omitted.
Note: The vocabulary given in this document is for descriptive purposes only, and does not itself add any validation. The purpose is to consistently document this behavior for clients.
field behavior of nested messages
google.api.field_behavior annotations on a nested message are independent of
the annotations of the parent.
For example, a nested message can have a field behavior of REQUIRED while the
parent field can be OPTIONAL:
message Title {
View on GitHub