github.com/googleapis/api-linter@v1.65.2/rules/aip0136/http_body.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 "strings" 19 20 "bitbucket.org/creachadair/stringset" 21 "github.com/googleapis/api-linter/lint" 22 "github.com/googleapis/api-linter/locations" 23 "github.com/googleapis/api-linter/rules/internal/utils" 24 "github.com/jhump/protoreflect/desc" 25 "github.com/stoewer/go-strcase" 26 ) 27 28 var httpBody = &lint.MethodRule{ 29 Name: lint.NewRuleName(136, "http-body"), 30 OnlyIf: isCustomMethod, 31 LintMethod: func(m *desc.MethodDescriptor) []lint.Problem { 32 for _, httpRule := range utils.GetHTTPRules(m) { 33 noBody := stringset.New("GET", "DELETE") 34 if !noBody.Contains(httpRule.Method) { 35 // Determine the name of the resource. 36 // This entails some guessing; we assume that the verb is a single 37 // word and that the resource is everything else. 38 resource := strings.Join(strings.Split(strcase.SnakeCase(m.GetName()), "_")[1:], "_") 39 if !stringset.New(resource, "*").Contains(httpRule.Body) { 40 // FIXME: We intentionally only return one Problem here. 41 // When we can attach problems to the particular annotation, update this 42 // to return multiples. 43 return []lint.Problem{{ 44 Message: "Custom POST methods should set `body: \"*\"`.", 45 Descriptor: m, 46 Location: locations.MethodHTTPRule(m), 47 }} 48 } 49 } else if noBody.Contains(httpRule.Method) && httpRule.Body != "" { 50 // FIXME: We intentionally only return one Problem here. 51 // When we can attach problems to the particular annotation, update this 52 // to return multiples. 53 return []lint.Problem{{ 54 Message: "Custom GET (or DELETE) methods should not set a body clause.", 55 Descriptor: m, 56 Location: locations.MethodHTTPRule(m), 57 }} 58 } 59 } 60 return nil 61 }, 62 }