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

     1  // Copyright 2022 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 aip0127
    16  
    17  import (
    18  	"fmt"
    19  	"regexp"
    20  
    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  )
    26  
    27  var (
    28  	literal          = `(\w+)`
    29  	identifier       = `(\w+)`
    30  	verb             = fmt.Sprintf("(:%s)", literal)
    31  	fieldPath        = fmt.Sprintf(`(%s(\.%s)*)`, identifier, identifier)
    32  	variableSegment  = fmt.Sprintf(`(\*\*|\*|%s)`, literal)
    33  	variableTemplate = fmt.Sprintf("(%s(/%s)*)", variableSegment, variableSegment)
    34  	variable         = fmt.Sprintf(`(\{%s(=%s)?\})`, fieldPath, variableTemplate)
    35  	segment          = fmt.Sprintf(`(\*\*|\*|%s|%s)`, literal, variable)
    36  	segments         = fmt.Sprintf("(%s(/%s)*)", segment, segment)
    37  	template         = fmt.Sprintf("^/%s(%s)?$", segments, verb)
    38  	templateRegex    = regexp.MustCompile(template)
    39  )
    40  
    41  // HTTP URL pattern should follow the syntax rules described here:
    42  // https://github.com/googleapis/googleapis/blob/16db2fb7fab4668bdfa09966513e03581d8f5e35/google/api/http.proto#L224.
    43  var httpTemplateSyntax = &lint.MethodRule{
    44  	Name:   lint.NewRuleName(127, "http-template-syntax"),
    45  	OnlyIf: utils.HasHTTPRules,
    46  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    47  		problems := []lint.Problem{}
    48  		for _, httpRule := range utils.GetHTTPRules(m) {
    49  			// Replace the API Versioning template if it matches exactly so as
    50  			// to not emit false positives.
    51  			uri := utils.VersionedSegment.ReplaceAllString(httpRule.URI, "v")
    52  			if !templateRegex.MatchString(uri) {
    53  				message := fmt.Sprintf("The HTTP pattern %q does not follow proper HTTP path template syntax", httpRule.URI)
    54  				problems = append(problems, lint.Problem{
    55  					Message:    message,
    56  					Descriptor: m,
    57  					Location:   locations.MethodHTTPRule(m),
    58  				})
    59  			}
    60  		}
    61  		return problems
    62  	},
    63  }