Auto-population of a field should not occur if a field has explicit presence and is set to the default value
The current implementation for autopopulation of a field assumes that if the field is an empty string or not set (and all other requirements are fulfilled), that the field should be autopopulated.
Per https://google.aip.dev/client-libraries/4235:
The field must be automatically populated if and only if one of the following conditions holds:
The field supports explicit presence, and has not been set by the user
Digging in a little - a field in proto3 supports explicit presence if it is explicitly annotated as optional (https://protobuf.dev/programming-guides/field_presence/#presence-in-proto3-apis).
Proto2 supports explicit presence by default, but GAPIC libraries require proto3 by default, so I don't think we need to check for proto2 vs. proto3.
Additionally, proto3 supports explicit presence as of v3.15.0. Since we're already using a much newer version of proto3, I don't think we need to check for any older versions of proto3.
Note that being annotated as optional is not a requirement for autopopulation - the requirement is simply that the field is not marked as required.
Therefore, there exists an edge case where:
- a string field
foois explicitly annotated asoptional - a user sets
footo the value of an empty string
and the expected outcome is that foo is not autopopulated.
A potential path forward would be to:
- Check for the explicit
optionalannotation of a field and add it as an attribute toField - If that attribute is true, update the generated code to:
if(!request.hasRequestId())
Lowering priority to p3 as there isn't currently a customer need for this functionality.