googleapis / googleapis/google-cloud-java

[sdk-platform-java] Enforce REQUIRED annotations via API call signatures

Offen
#12,535 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
priority: p3 type: feature request type: question
Vorherrschende Sprache
Java
Sterne
2.1k
Forks
1.2k
Ø Merge
1 T. 23 Std.
Gemergte PRs (30 T.)
154

Beschreibung

Problem:

When a field is annotated as REQUIRED, the API allows constructing objects without specifying explicitly the value of the required field.

Example:
```
import "google/api/field_behavior.proto";

message Child { ... }

message Parent {
Child child = 1 [
(google.api.field_behavior) = REQUIRED
];
}
```

The `Parent` will generate a Builder that has a `setChild()` method, but there's no compile-time way to check that this setter was called. So it's possible to write code that constructs "bad" messages which will only be manifest at runtime.

**Describe the solution you'd like**

Positional arguments for required fields. In the example above, to get a `Parent` Builder (or to create a Parent in some other way) your **only option** would be to call a method that requires a `Child` as a parameter.

To preserve the existing API, this feature could be placed behind a flag / option for the proto generator.

**Describe alternatives you've considered**

Don't think there's an alternative besides changing how code is generated.

Thanks!

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

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