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: Prohibit streaming on paging methods. #601

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions docs/rules/0158/response-unary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
rule:
aip: 158
name: [core, '0158', response-unary]
summary: Paginated responses must not use streaming.
permalink: /158/response-unary
redirect_from:
- /0158/response-unary
---

# Paginated methods: Unary responses

This rule enforces that all paginated methods (`List` and `Search` methods, or
methods with pagination fields) use unary responses, as mandated in
[AIP-158][].

## Details

This rule looks at any message matching `List*Response` or `Search*Response`,
or any response message that has `next_page_token` field, and complains if the
method uses gRPC server streaming (the `stream` keyword).

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
// Streaming is prohibited on paginated responses.
rpc ListBooks(ListBooksRequest) returns (stream ListBooksResponse) {
option (google.api.http) = {
get: "/v1/{parent=publishers/*}/books"
};
}
```

**Correct** code for this rule:

```proto
// Correct.
rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {
option (google.api.http) = {
get: "/v1/{parent=publishers/*}/books"
};
}
```

## Disabling

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

```proto
// (-- api-linter: core::0158::response-unary
// aip.dev/not-precedent: We need to do this because reasons. --)
rpc ListBooks(ListBooksRequest) returns (stream ListBooksResponse) {
option (google.api.http) = {
get: "/v1/{parent=publishers/*}/books"
};
}
```

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

[aip-158]: https://aip.dev/158
[aip.dev/not-precedent]: https://aip.dev/not-precedent
5 changes: 5 additions & 0 deletions rules/aip0158/aip0158.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func AddRules(r lint.RuleRegistry) error {
responsePaginationNextPageToken,
responseRepeatedFirstField,
responsePluralFirstField,
responseUnary,
)
}

Expand All @@ -46,3 +47,7 @@ func isPaginatedRequestMessage(m *desc.MessageDescriptor) bool {
func isPaginatedResponseMessage(m *desc.MessageDescriptor) bool {
return paginatedRes.MatchString(m.GetName()) || m.FindFieldByName("next_page_token") != nil
}

func isPaginatedMethod(m *desc.MethodDescriptor) bool {
return isPaginatedRequestMessage(m.GetInputType()) && isPaginatedResponseMessage(m.GetOutputType())
}
36 changes: 36 additions & 0 deletions rules/aip0158/response_unary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 aip0158

import (
"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/locations"
"github.com/jhump/protoreflect/desc"
)

var responseUnary = &lint.MethodRule{
Name: lint.NewRuleName(158, "response-unary"),
OnlyIf: isPaginatedMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
if m.IsServerStreaming() {
return []lint.Problem{{
Message: "Paginated responses must be unary, not streaming.",
Descriptor: m,
Location: locations.MethodResponseType(m),
}}
}
return nil
},
}
54 changes: 54 additions & 0 deletions rules/aip0158/response_unary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 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 aip0158

import (
"testing"

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

func TestResponseUnary(t *testing.T) {
for _, test := range []struct {
name string
Stream string
Response string
NextPageToken string
problems testutils.Problems
}{
{"Vaild", "", "ListFoosResponse", "next_page_token", nil},
{"Invalid", "stream ", "ListFoosResponse", "next_page_token", testutils.Problems{{Message: "unary, not stream"}}},
{"Irrelevant", "stream ", "FrobFoosResponse", "not_paginated_response", nil},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
service Foos {
rpc ListFoos(ListFoosRequest) returns ({{.Stream}}{{.Response}});
}
message ListFoosRequest {
int32 page_size = 1;
string page_token = 2;
}
message {{.Response}} {
string {{.NextPageToken}} = 1;
}
`, test)
m := f.GetServices()[0].GetMethods()[0]
if diff := test.problems.SetDescriptor(m).Diff(responseUnary.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
}
}