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-136): response message name #1387

Merged
113 changes: 113 additions & 0 deletions docs/rules/0136/response-message-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
rule:
aip: 136
name: [core, '0136', response-message-name]
summary: Custom methods must have standardized response message names.
permalink: /136/response-message-name
redirect_from:
- /0136/response-message-name
---

# Custom methods: Response message

This rule enforces that all custom methods should take a response message
matching the RPC name, with a `Response` suffix, or the resource being operated
on [AIP-136][].
liveFreeOrCode marked this conversation as resolved.
Show resolved Hide resolved

## Details

This rule looks at any method that is not a standard method, and complains if
the name of the corresponding input message does not match the name of the RPC
with the suffix `Response` appended, or the resource itself. It will use the
`(google.api.resource_reference)` on the first field of the request message to
determine the resource name that should be used.

## Examples

### Response Suffix

**Incorrect** code for this rule:

```proto
// Incorrect.
// Should be `ArchiveBookResponse`.
rpc ArchiveBook(ArchiveBookRequest) returns (ArchiveBookResp) {
option (google.api.http) = {
post: "/v1/{name=publishers/*/books/*}:archive"
body: "*"
};
}
```

**Correct** code for this rule:

```proto
// Correct.
rpc ArchiveBook(ArchiveBookRequest) returns (ArchiveBookResponse) {
option (google.api.http) = {
post: "/v1/{name=publishers/*/books/*}:archive"
body: "*"
};

```

### Resource

**Incorrect** code for this rule:

```proto
// Incorrect.
// Should be `Book`.
rpc ArchiveBook(ArchiveBookRequest) returns (Author) {
option (google.api.http) = {
post: "/v1/{name=publishers/*/books/*}:archive"
body: "*"
};
}

message ArchiveBookRequest {
// The book to archive.
// Format: publishers/{publisher}/books/{book}
string name = 1 [(google.api.resource_reference).type = "library.googleapis.com/Book"];
}
```

**Correct** code for this rule:

```proto
// Correct.
rpc ArchiveBook(ArchiveBookRequest) returns (Book) {
option (google.api.http) = {
post: "/v1/{name=publishers/*/books/*}:archive"
body: "*"
};
}

message ArchiveBookRequest {
// The book to archive.
// Format: publishers/{publisher}/books/{book}
string name = 1 [(google.api.resource_reference).type = "library.googleapis.com/Book"];
}
```

## Disabling

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

```proto
// (-- api-linter: core::0136::response-message-name=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
rpc ArchiveBook(ArchiveBookRequest) returns (ArchiveBookResp) {
option (google.api.http) = {
post: "/v1/{name=publishers/*/books/*}:archive"
body: "*"
};
}
```

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

[aip-136]: https://aip.dev/136
[aip.dev/not-precedent]: https://aip.dev/not-precedent
1 change: 1 addition & 0 deletions rules/aip0136/aip0136.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func AddRules(r lint.RuleRegistry) error {
httpBody,
httpMethod,
noPrepositions,
responseMessageName,
standardMethodsOnly,
uriSuffix,
verbNoun,
Expand Down
64 changes: 64 additions & 0 deletions rules/aip0136/response_message_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 aip0136

import (
"fmt"

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

// Custom methods should return a response message matching the RPC name,
// with a Response suffix.
var responseMessageName = &lint.MethodRule{
Name: lint.NewRuleName(135, "response-message-name"),
OnlyIf: isCustomMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
matchingResponseName := m.GetName() + "Response"
suggestion := matchingResponseName

got := m.GetOutputType().GetName()
want := stringset.New(matchingResponseName)

// Get the resource name from the first request message field, if it
// is a resource reference
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
if len(m.GetInputType().GetFields()) > 0 {
rr := utils.GetResourceReference(m.GetInputType().GetFields()[0])
if _, resourceMsgNameSingular, ok := utils.SplitResourceTypeName(rr.GetType()); ok {
want.Add(resourceMsgNameSingular)
suggestion += " or " + resourceMsgNameSingular
}
}

if !want.Contains(got) {
msg := "Custom methods should return a response message matching the RPC name, with a Response suffix, or the resource, not %q."

problem := lint.Problem{
Message: fmt.Sprintf(msg, got),
Suggestion: suggestion,
Descriptor: m,
Location: locations.MethodResponseType(m),
}

return []lint.Problem{problem}
}

return nil
},
}
83 changes: 83 additions & 0 deletions rules/aip0136/response_message_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 aip0136

import (
"testing"

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

func TestResponseMessageName(t *testing.T) {
// Set up the testing permutations.
tests := []struct {
testName string
MethodName string
RespMessageName string
OperatingOnResource bool
problems testutils.Problems
}{
{"Valid Resource", "ArchiveBook", "Book", true, testutils.Problems{}},
{"Invalid Resource", "ArchiveBook", "Author", true, testutils.Problems{{Suggestion: "ArchiveBookResponse or Book"}}},
{"Valid Response Suffix Stateful", "ArchiveBook", "ArchiveBookResponse", true, testutils.Problems{}},
{"Valid Response Suffix Stateless", "ArchiveBook", "ArchiveBookResponse", false, testutils.Problems{}},
{"Invalid Response Suffix", "ArchiveBook", "ArchiveBookResp", true, testutils.Problems{{Suggestion: "ArchiveBookResponse or Book"}}},
{"Unable To Find Resource", "ArchiveBook", "ArchiveBookResp", false, testutils.Problems{{Suggestion: "ArchiveBookResponse"}}},
{"Irrelevant", "DeleteBook", "DeleteBookResp", true, testutils.Problems{}},
}

// Run each test individually.
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
package test;
import "google/api/resource.proto";
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.RespMessageName}});
}
message {{.MethodName}}Request {
{{ if (.OperatingOnResource) }}
// The book to archive.
// Format: publishers/{publisher}/books/{book}
string name = 1 [
(google.api.resource_reference) = {
type: "library.googleapis.com/Book"
}];
{{ end }}
}
message Book {
option (google.api.resource) = {
type: "library.googleapis.com/Book"
pattern: "publishers/{publisher}/books/{book}"
};
}
message Author {
option (google.api.resource) = {
type: "library.googleapis.com/Author"
pattern: "authors/{author}"
};
}
{{ if and (ne .RespMessageName "Book") (ne .RespMessageName "Author") }}
message {{.RespMessageName}} {}
{{ end }}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := responseMessageName.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Error(diff)
}
})
}
}