Skip to content

Commit

Permalink
Allow path field in update (#86)
Browse files Browse the repository at this point in the history
* Allow path field in updates

* add path required rule

* fix

* pr fix
  • Loading branch information
rambleraptor authored Sep 16, 2024
1 parent 70e81ee commit fcad800
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
59 changes: 59 additions & 0 deletions docs/rules/0134/request-path-required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
rule:
aep: 134
name: [core, '0134', request-path-required]
summary: Update RPCs must have a `path` field in the request.
permalink: /134/request-path-required
redirect_from:
- /0134/request-path-required
---

# Update methods: Name field

This rule enforces that all `Update` standard methods have a `string path`
field in the request message, as mandated in [AEP-134][].

## Details

This rule looks at any message matching `Update*Request` and complains if
the `path` field is missing.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
message UpdateBookRequest {
// Field path should be `path`.
string book = 1;
}
```

**Correct** code for this rule:

```proto
// Correct.
message UpdateBookRequest {
string path = 1;
}
```

## Disabling

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

```proto
// (-- api-linter: core::0134::request-path-required=disabled
// aep.dev/not-precedent: We need to do this because reasons. --)
message UpdateBookRequest {
string book = 1;
}
```

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

[aep-134]: https://aep.dev/134
[aep.dev/not-precedent]: https://aep.dev/not-precedent
1 change: 1 addition & 0 deletions rules/aep0134/aep0134.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func AddRules(r lint.RuleRegistry) error {
requestMaskField,
requestMaskRequired,
requestMessageName,
requestPathRequired,
requestRequiredFields,
requestResourceField,
requestResourceRequired,
Expand Down
37 changes: 37 additions & 0 deletions rules/aep0134/request_path_required.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2019 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 aep0134

import (
"fmt"

"github.com/aep-dev/api-linter/lint"
"github.com/aep-dev/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
)

var requestPathRequired = &lint.MessageRule{
Name: lint.NewRuleName(134, "request-path-required"),
OnlyIf: utils.IsListRequestMessage,
LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
if m.FindFieldByName("path") == nil {
return []lint.Problem{{
Message: fmt.Sprintf("Method %q has no `path` field", m.GetName()),
Descriptor: m,
}}
}
return nil
},
}
53 changes: 53 additions & 0 deletions rules/aep0134/request_path_required_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019 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 aep0134

import (
"testing"

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

func TestRequestNameRequired(t *testing.T) {
// Set up the testing permutations.
tests := []struct {
name string
MessageName string
FieldName string
problems testutils.Problems
}{
{"Valid", "ListBookRequest", "path", nil},
{"InvalidName", "ListBookRequest", "id", testutils.Problems{{Message: "path"}}},
{"Irrelevant", "RemoveBookRequest", "id", nil},
}

// Run each test individually.
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
message {{.MessageName}} {
string {{.FieldName}} = 1;
}
`, test)

// Run the lint rule, and establish that it returns the correct problems.
problems := requestPathRequired.Lint(f)
message := f.GetMessageTypes()[0]
if diff := test.problems.SetDescriptor(message).Diff(problems); diff != "" {
t.Errorf("Problems did not match: %v", diff)
}
})
}
}
1 change: 1 addition & 0 deletions rules/aep0134/request_unknown_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var unknownFields = &lint.MessageRule{
allowedFields := stringset.New(
fieldNameFromResource(resource), // AEP-134
"allow_missing", // AEP-134
"path", // AEP-134
"request_id", // AEP-155
"update_mask", // AEP-134
"validate_only", // AEP-163
Expand Down
5 changes: 5 additions & 0 deletions rules/aep0134/request_unknown_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func TestUnknownFields(t *testing.T) {
builder.FieldTypeBool(),
testutils.Problems{},
},
{
"PathOnly", "UpdateBigBookRequest", "path",
builder.FieldTypeString(),
testutils.Problems{},
},
{
"Invalid", "UpdateBigBookRequest", "application_id",
builder.FieldTypeString(),
Expand Down

0 comments on commit fcad800

Please sign in to comment.