github.com/googleapis/api-linter@v1.65.2/rules/aip0136/http_uri_suffix.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0136
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/googleapis/api-linter/locations"
    22  
    23  	"github.com/googleapis/api-linter/lint"
    24  	"github.com/googleapis/api-linter/rules/internal/utils"
    25  	"github.com/jhump/protoreflect/desc"
    26  	"github.com/stoewer/go-strcase"
    27  )
    28  
    29  var uriSuffix = &lint.MethodRule{
    30  	Name: lint.NewRuleName(136, "http-uri-suffix"),
    31  	OnlyIf: func(m *desc.MethodDescriptor) bool {
    32  		return isCustomMethod(m) && httpNameVariable.LintMethod(m) == nil && httpParentVariable.LintMethod(m) == nil
    33  	},
    34  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    35  		for _, httpRule := range utils.GetHTTPRules(m) {
    36  			var want string
    37  
    38  			// URIs should end in a `:` character followed by the name of the method.
    39  			// However, if the noun is the URI's resource, then we only use `:verb`,
    40  			// not `:verbNoun`.
    41  			//
    42  			// This is somewhat tricky to test for perfectly, and may need to evolve
    43  			// over time, but the following rules should be mostly correct:
    44  			//   1. If the URI contains `{name=` or `{parent=`, expect `:verb`.
    45  			//   2. Address known edge cases from other AIPs.
    46  			//   3. For collections, expect `nouns:verb`.
    47  			//   4. Otherwise, expect `:verbNoun`.
    48  			//
    49  			// We blindly assume that the verb is always one word (the "noun" may be
    50  			// any number of words; they often include adjectives).
    51  			// There are some known exceptions, particularly around "Batch".
    52  			// ----------------------------------------------------------------------
    53  			// If the URI contains `{name=` or `{parent=`, expect `:verb`.
    54  			if strings.Contains(httpRule.URI, ":batch") {
    55  				rpcSlice := strings.Split(strcase.SnakeCase(m.GetName()), "_")
    56  				want = ":" + strcase.LowerCamelCase(rpcSlice[0]+"_"+rpcSlice[1])
    57  			} else {
    58  				for key := range httpRule.GetVariables() {
    59  					if key == "name" || key == "parent" || strings.HasSuffix(key, ".name") {
    60  						rpcSlice := strings.Split(strcase.SnakeCase(m.GetName()), "_")
    61  						want = ":" + rpcSlice[0]
    62  						break
    63  					}
    64  				}
    65  			}
    66  
    67  			// AIP-162 introduces some special cases around revisions, where
    68  			// `ListFooRevisions` gets a suffix of `:listRevisions` (and the same for
    69  			// `Delete` and `Tag`).
    70  			n := m.GetName()
    71  			if strings.HasPrefix(n, "List") && strings.HasSuffix(m.GetName(), "Revisions") {
    72  				want = ":listRevisions"
    73  			}
    74  			if strings.HasSuffix(m.GetName(), "Revision") {
    75  				if strings.HasPrefix(m.GetName(), "Tag") {
    76  					want = ":tagRevision"
    77  				}
    78  				if strings.HasPrefix(m.GetName(), "Delete") {
    79  					want = ":deleteRevision"
    80  				}
    81  			}
    82  
    83  			// If the final component of the URI (before the verb) matches the final
    84  			// component of the RPC name, then assume this matches `nouns:verb`.
    85  			//
    86  			// Note that we do not do any pluralization here -- both the RPC and
    87  			// the URI must be plural already. If the RPC is singular, step 4 should
    88  			// apply.
    89  			plainURI := httpRule.GetPlainURI()
    90  			segs := strings.Split(strings.Split(plainURI, ":")[0], "/")
    91  			segment := segs[len(segs)-1]
    92  			if len(want) == 0 && strings.HasSuffix(strings.ToLower(n), strings.ToLower(segment)) {
    93  				want = segment + ":" + strings.Split(strcase.SnakeCase(n), "_")[0]
    94  			}
    95  
    96  			// Nothing else applied; expect `:verbNoun`.
    97  			if len(want) == 0 {
    98  				want = ":" + strcase.LowerCamelCase(strcase.SnakeCase(m.GetName()))
    99  			}
   100  
   101  			// Do we have the suffix we expect?
   102  			if !strings.HasSuffix(httpRule.URI, want) {
   103  				// FIXME: We intentionally only return one Problem here.
   104  				// When we can attach issues to the particular annotation, update this
   105  				// to return multiples.
   106  				return []lint.Problem{{
   107  					Message: fmt.Sprintf(
   108  						"Custom method should have a URI suffix matching the method name, such as %q.",
   109  						want,
   110  					),
   111  					Descriptor: m,
   112  					Location:   locations.MethodHTTPRule(m),
   113  				}}
   114  			}
   115  		}
   116  		return nil
   117  	},
   118  }