googleapis / googleapis/google-cloud-java

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

Offen
#12,449 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
priority: p4 type: bug
Vorherrschende Sprache
Java
Sterne
2.1k
Forks
1.2k
Ø Merge
1 T. 23 Std.
Gemergte PRs (30 T.)
154

Beschreibung

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](https://github.com/protocolbuffers/protobuf/releases/tag/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())
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.