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

[AIP-136] Add URI suffix check for custom methods. #253

Merged
merged 2 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions rules/aip0136/aip0136.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func AddRules(r lint.RuleRegistry) {
r.Register(
httpMethod,
noPrepositions,
uriSuffix,
verbNoun,
)
}
Expand Down
68 changes: 68 additions & 0 deletions rules/aip0136/uri_suffix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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"
"strings"

"github.com/googleapis/api-linter/lint"
"github.com/googleapis/api-linter/rules/internal/utils"
"github.com/jhump/protoreflect/desc"
"github.com/stoewer/go-strcase"
)

var uriSuffix = &lint.MethodRule{
Name: lint.NewRuleName("core", "0136", "uri-suffix"),
URI: "https://aip.dev/136#guidance",
OnlyIf: isCustomMethod,
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
for _, httpRule := range utils.GetHTTPRules(m) {
// URIs should end in a `:` character followed by the name of the method.
// However, if the noun is the URI's resource, then we only use `:verb`,
// not `:verbNoun`.
mingzhi marked this conversation as resolved.
Show resolved Hide resolved
//
// This is somewhat tricky to test for perfectly, and may need to evolve
// over time, but the following rules should be mostly correct:
// 1. If the URI contains `{name=`, expect `:verb`.
// 2. Otherwise, expect `:verbNoun`.
//
// We blindly assume that the verb is always one word (the "noun" may be
// any number of words; they often include adjectives).
//
// N.B. The LowerCamel(Snake(name)) is because strcase does not translate
// from upper camel to lower camel correctly.
want := ":" + strcase.LowerCamelCase(strcase.SnakeCase(m.GetName()))
mingzhi marked this conversation as resolved.
Show resolved Hide resolved
if strings.Contains(httpRule.URI, "{name=") {
want = ":" + strings.Split(strcase.SnakeCase(m.GetName()), "_")[0]
}

// Do we have the suffix we expect?
if !strings.HasSuffix(httpRule.URI, want) {
// FIXME: We intentionally only return one Problem here.
// When we can attach issues to the particular annotation, update this
// to return multiples.
return []lint.Problem{{
Message: fmt.Sprintf(
"Custom method should have a URI suffix matching the method name, such as %q.",
want,
),
Descriptor: m,
}}
}
}
return nil
},
}
60 changes: 60 additions & 0 deletions rules/aip0136/uri_suffix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 TestURISuffix(t *testing.T) {
tests := []struct {
testName string
MethodName string
URI string
problems testutils.Problems
}{
{"ValidVerb", "ArchiveBook", "/v1/{name=publishers/*/books}:archive", testutils.Problems{}},
{"InvalidVerb", "ArchiveBook", "/v1/{name=publishers/*/books}:archiveBook", testutils.Problems{{Message: ":archive"}}},
{"ValidVerbNounNoVars", "TranslateText", "/v3:translateText", testutils.Problems{}},
{"ValidVerbNounNoName", "TranslateText", "/v3/{location=projects/*/locations/*}:translateText", testutils.Problems{}},
{"InvalidVerbNoun", "TranslateText", "/v3:translate", testutils.Problems{{Message: ":translateText"}}},
{"ValidOneWord", "Translate", "/v3:translate", testutils.Problems{}},
{"ValidStdMethod", "GetBook", "/v1/{name=publishers/*/books/*}", testutils.Problems{}},
}
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
import "google/api/annotations.proto";
service Library {
rpc {{.MethodName}}({{.MethodName}}Request) returns ({{.MethodName}}Response) {
option (google.api.http) = {
post: "{{.URI}}"
body: "*"
};
}
}
message {{.MethodName}}Request {}
message {{.MethodName}}Response {}
`, test)
method := file.GetServices()[0].GetMethods()[0]
problems := uriSuffix.Lint(file)
if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
t.Errorf(diff)
}
})
}
}