Skip to content

Commit

Permalink
fix: Do not require undelete method if delete is not present. (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sneeringer authored Feb 10, 2021
1 parent dafa6e4 commit d2d2f98
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rules/aip0164/declarative_friendly_required.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var declarativeFriendlyRequired = &lint.MessageRule{
LintMessage: func(m *desc.MessageDescriptor) []lint.Problem {
resource := m.GetName()
want := fmt.Sprintf("Undelete%s", resource)
if utils.FindMethod(m.GetFile(), want) == nil {
delete := fmt.Sprintf("Delete%s", resource)
if utils.FindMethod(m.GetFile(), want) == nil && utils.FindMethod(m.GetFile(), delete) != nil {
return []lint.Problem{{
Message: fmt.Sprintf("Declarative-friendly %s should have an Undelete method.", resource),
Descriptor: m,
Expand Down
23 changes: 23 additions & 0 deletions rules/aip0164/declarative_friendly_required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ func TestDeclarativeFriendlyRequired(t *testing.T) {
t.Run(test.testName, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
import "google/api/resource.proto";
import "google/protobuf/empty.proto";
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns (Book);
rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty);
}
message Book {
option (google.api.resource) = {
{{.Style}}
};
}
message {{.MethodName}}Request {}
message DeleteBookRequest {}
`, test)
message := file.GetMessageTypes()[0]
problems := declarativeFriendlyRequired.Lint(file)
Expand All @@ -37,4 +40,24 @@ func TestDeclarativeFriendlyRequired(t *testing.T) {
}
})
}

// Also test that undelete is not required if delete is not present.
t.Run("ValidNoDelete", func(t *testing.T) {
file := testutils.ParseProto3String(t, `
import "google/api/resource.proto";
service Library {
rpc GetBook(GetBookRequest) returns (Book);
}
message Book {
option (google.api.resource) = {
style: DECLARATIVE_FRIENDLY
};
}
message GetBookRequest {}
`)
problems := declarativeFriendlyRequired.Lint(file)
if len(problems) > 0 {
t.Errorf("Got %v, expected no problems.", problems)
}
})
}

0 comments on commit d2d2f98

Please sign in to comment.