gapic-generator-java icon indicating copy to clipboard operation
gapic-generator-java copied to clipboard

Auto-population of a field should not occur if a field has explicit presence and is set to the default value

Open alicejli opened this issue 1 year ago • 2 comments

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:

  1. a string field foo is explicitly annotated as optional
  2. a user sets foo to the value of an empty string

and the expected outcome is that foo is not autopopulated.

A potential path forward would be to:

  1. Check for the explicit optional annotation of a field and add it as an attribute to Field
  2. If that attribute is true, update the generated code to:
if(!request.hasRequestId())

alicejli avatar Mar 13 '24 15:03 alicejli

Lowering priority to p3 as there isn't currently a customer need for this functionality.

alicejli avatar Mar 19 '24 13:03 alicejli