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
70 changes: 70 additions & 0 deletions docs/rules/0203/field-behavior-required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
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 in a message used in a request has a
`google.api.field_behavior` annotation with valid values, as mandated by
[AIP-203][].

## 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 one of these categories.

## 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
94 changes: 0 additions & 94 deletions docs/rules/0203/optional-consistency.md

This file was deleted.

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
2 changes: 1 addition & 1 deletion rules/aip0203/aip0203.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ var standardFields = stringset.New("etag")
func AddRules(r lint.RuleRegistry) error {
return r.Register(
203,
fieldBehaviorRequired,
inputOnly,
immutable,
optional,
optionalBehaviorConflict,
optionalBehaviorConsistency,
outputOnly,
required,
requiredAndOptional,
Expand Down
75 changes: 75 additions & 0 deletions rules/aip0203/field_behavior_required.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 minimumRequiredFieldBehavior = stringset.New(
"OPTIONAL", "REQUIRED", "OUTPUT_ONLY", "IMMUTABLE",
)

var fieldBehaviorRequired = &lint.MethodRule{
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
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
// we only check requests, as OutputTypes are always
// OUTPUT_ONLY
it := m.GetInputType()
return checkFields(it)
},
}

func checkFields(m *desc.MessageDescriptor) []lint.Problem {
problems := []lint.Problem{}
for _, f := range m.GetFields() {
asMessage := f.GetMessageType()
if asMessage != nil {
problems = append(problems, checkFields(asMessage)...)
}
p := checkFieldBehavior(f)
if p != nil {
problems = append(problems, *p)
}
}
return problems
}

func checkFieldBehavior(f *desc.FieldDescriptor) *lint.Problem {
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
fieldBehavior := utils.GetFieldBehavior(f)
if len(fieldBehavior) == 0 {
return &lint.Problem{
Message: fmt.Sprintf("google.api.field_behavior annotation must be set, and have one of %v",
minimumRequiredFieldBehavior,
),
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
Descriptor: f,
}
}
// check for at least one valid annotation
if !minimumRequiredFieldBehavior.Intersects(fieldBehavior) {
return &lint.Problem{
Message: fmt.Sprintf(
"google.api.field_behavior must have at least one of the following behaviors set: %v",
minimumRequiredFieldBehavior,
),
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
Descriptor: f,
}
}
return nil
}
Loading