Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AIP-203): require field_behavior #1149

Merged
merged 10 commits into from
May 25, 2023
69 changes: 69 additions & 0 deletions docs/rules/0203/field-behavior-required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
rule:
aip: 203
name: [core, '0203', field-behavior-required]
summary: Field behavior is required, and must have one of OUTPUT_ONLY,
REQUIRED, or OPTIONAL.
permalink: /203/field-behavior-required
redirect_from:
- /0203/field-behavior-required
---

# Field Behavior Required

This rule enforces that each field has a `google.api.field_behavior` annotation
with valid values, as mandated by[AIP-203][]
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved

## Details

This rule looks at all fields and ensures they have a
`google.api.field_behavior` annotation. It also verifies that they have at least
one of the options `OUTPUT_ONLY`, `REQUIRED`, or `OPTIONAL`: all fields must
fall into of these categories.
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
message Book {
string name = 1;

// No field behavior
optional string title = 2;
}
```

**Correct** code for this rule:

```proto
// Correct.
message Book {
string name = 1;

string title = 2 [(google.api.field_behavior) = REQUIRED];
}
```

## Disabling

If you need to violate this rule, use a leading comment above the field.
Remember to also include an [aip.dev/not-precedent][] comment explaining why.

```proto
message Book {
string name = 1;

// Required. The title of the book.
// (-- api-linter: core::0203::field-behavior-required=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
optional string title = 2;
}
```

If you need to violate this rule for an entire file, place the comment at the
top of the file.

[aip-203]: https://aip.dev/203
[aip.dev/not-precedent]: https://aip.dev/not-precedent
2 changes: 1 addition & 1 deletion docs/rules/0203/required-and-optional.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ message Book {
**Correct** code for this rule:

```proto
// Incorrect.
// Correct.
message Book {
string name = 1;

Expand Down
1 change: 1 addition & 0 deletions rules/aip0203/aip0203.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var standardFields = stringset.New("etag")
func AddRules(r lint.RuleRegistry) error {
return r.Register(
203,
fieldBehaviorRequired,
inputOnly,
immutable,
optional,
Expand Down
51 changes: 51 additions & 0 deletions rules/aip0203/field_behavior_required.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aip0203

import (
"fmt"

"bitbucket.org/creachadair/stringset"
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

var requiredOneOfFieldBehavior = stringset.New(
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
"OPTIONAL", "REQUIRED", "OUTPUT_ONLY",
shwoodard marked this conversation as resolved.
Show resolved Hide resolved
)

var fieldBehaviorRequired = &lint.FieldRule{
Name: lint.NewRuleName(203, "field-behavior-required"),
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
LintField: func(f *desc.FieldDescriptor) []lint.Problem {
fieldBehavior := utils.GetFieldBehavior(f)
if len(fieldBehavior) == 0 {
return []lint.Problem{{
Message: "google.api.field_behavior annotation must be set",
Descriptor: f,
}}
}
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
// check for at least one valid annotation
if !requiredOneOfFieldBehavior.Intersects(fieldBehavior) {
return []lint.Problem{{
Message: fmt.Sprintf(
"google.api.field_behavior must have at least one of the following behaviors set: %v",
requiredOneOfFieldBehavior,
),
Descriptor: f,
}}
}
return nil
},
}
76 changes: 76 additions & 0 deletions rules/aip0203/field_behavior_required_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aip0203

import (
"testing"

"github.com/googleapis/api-linter/rules/internal/testutils"
)

const ()

func TestFieldBehaviorRequired(t *testing.T) {
for _, test := range []struct {
name string
Annotations string
problems testutils.Problems
}{
{
"ValidRequired",
"[(google.api.field_behavior) = REQUIRED]",
nil,
},
{
"ValidOptional",
"[(google.api.field_behavior) = OPTIONAL]",
nil,
},
{
"ValidOutputOnly",
"[(google.api.field_behavior) = OUTPUT_ONLY]",
nil,
},
{
"ValidOptionalImmutable",
`[(google.api.field_behavior) = OUTPUT_ONLY,
(google.api.field_behavior) = OPTIONAL]`,
nil,
},
{
"InvalidImmutable",
"[(google.api.field_behavior) = IMMUTABLE]",
testutils.Problems{{Message: "must have at least one"}},
},
{
"InvalidEmpty",
"",
testutils.Problems{{Message: "annotation must be set"}},
},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
import "google/api/field_behavior.proto";

message Book {
int32 page_count = 1 {{.Annotations}};
}
`, test)
field := f.GetMessageTypes()[0].GetFields()[0]
if diff := test.problems.SetDescriptor(field).Diff(fieldBehaviorRequired.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
}
}