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 long-running operations. #603

Merged
merged 2 commits 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
69 changes: 69 additions & 0 deletions docs/rules/0151/response-unary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
rule:
aip: 151
name: [core, '0151', response-unary]
summary: Long-running operations must not use streaming.
permalink: /151/response-unary
redirect_from:
- /0151/response-unary
---

# Paginated methods: Unary responses

This rule enforces that all long-running operation methods use unary responses,
as mandated in [AIP-151][].

## Details

This rule looks at any message returning a `google.longrunning.Operation`, and
complains if the method uses gRPC server streaming (the `stream` keyword).

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
// Streaming is prohibited on long-running operations.
rpc ReadBook(ReadBookRequest) returns (stream google.longrunning.Operation) {
option (google.api.http) = {
post: "/v1/{parent=publishers/*/books/*}:read"
body: "*"
};
}
```

**Correct** code for this rule:

```proto
// Correct.
rpc ReadBook(ReadBookRequest) returns (google.longrunning.Operation) {
option (google.api.http) = {
post: "/v1/{parent=publishers/*/books/*}:read"
body: "*"
};
}
```

## 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::0151::response-unary
// aip.dev/not-precedent: We need to do this because reasons. --)
rpc ReadBook(ReadBookRequest) returns (stream google.longrunning.Operation) {
option (google.api.http) = {
post: "/v1/{parent=publishers/*/books/*}:read"
body: "*"
};
}
```

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

[aip-151]: https://aip.dev/151
[aip.dev/not-precedent]: https://aip.dev/not-precedent
1 change: 1 addition & 0 deletions rules/aip0151/aip0151.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func AddRules(r lint.RuleRegistry) error {
lroMetadataReachable,
lroResponse,
lroResponseReachable,
responseUnary,
)
}

Expand Down
36 changes: 36 additions & 0 deletions rules/aip0151/response_unary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 aip0151

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(151, "response-unary"),
OnlyIf: isLRO,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
if m.IsServerStreaming() {
return []lint.Problem{{
Message: "LRO responses must be unary, not streaming.",
Descriptor: m,
Location: locations.MethodResponseType(m),
}}
}
return nil
},
}
49 changes: 49 additions & 0 deletions rules/aip0151/response_unary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 aip0151

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
problems testutils.Problems
}{
{"Vaild", "", "google.longrunning.Operation", nil},
{"Invalid", "stream ", "google.longrunning.Operation", testutils.Problems{{Message: "unary, not stream"}}},
{"Irrelevant", "stream ", "ReadBookResponse", nil},
} {
t.Run(test.name, func(t *testing.T) {
f := testutils.ParseProto3Tmpl(t, `
import "google/longrunning/operations.proto";
service Library {
rpc ReadBook(ReadBookRequest) returns ({{.Stream}}{{.Response}});
}
message ReadBookRequest {}
message ReadBookResponse {}
`, test)
m := f.GetServices()[0].GetMethods()[0]
if diff := test.problems.SetDescriptor(m).Diff(responseUnary.Lint(f)); diff != "" {
t.Errorf(diff)
}
})
}
}