Skip to content

Commit

Permalink
fix(AIP-127): account for api_version template (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz authored Dec 15, 2022
1 parent e55a765 commit 70085dc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/rules/0127/http-template-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rule:
HTTP template variable patterns should match the patterns defined by their resources.
permalink: /127/http-template-pattern
redirect_from:
- /127/http-template-pattern
- /0127/http-template-pattern
---

# HTTP Pattern Variables
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/0127/http-template-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rule:
HTTP patterns should follow the HTTP path template syntax.
permalink: /127/http-template-syntax
redirect_from:
- /127/http-template-syntax
- /0127/http-template-syntax
---

# HTTP Pattern Variables
Expand Down
5 changes: 4 additions & 1 deletion rules/aip0127/http_template_syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ var httpTemplateSyntax = &lint.MethodRule{
LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
problems := []lint.Problem{}
for _, httpRule := range utils.GetHTTPRules(m) {
if !templateRegex.MatchString(httpRule.URI) {
// Replace the API Versioning template if it matches exactly so as
// to not emit false positives.
uri := utils.VersionedSegment.ReplaceAllString(httpRule.URI, "v")
if !templateRegex.MatchString(uri) {
message := fmt.Sprintf("The HTTP pattern %q does not follow proper HTTP path template syntax", httpRule.URI)
problems = append(problems, lint.Problem{
Message: message,
Expand Down
1 change: 1 addition & 0 deletions rules/aip0127/http_template_syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestHttpTemplateSyntax(t *testing.T) {

// Valid cases
{"SingleLiteral", "/v1", true},
{"VersionedTemplate", "/{$api_version}/books", true},
{"TwoLiterals", "/v1/books", true},
{"ThreeLiterals", "/v1/books/shelves", true},
{"SingleLiteralWithVerb", "/v1:verb", true},
Expand Down
10 changes: 5 additions & 5 deletions rules/internal/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (h *HTTPRule) GetVariables() map[string]string {
vars := map[string]string{}

// Replace the version template variable with "v".
uri := templateSegment.ReplaceAllString(h.URI, "v")
uri := VersionedSegment.ReplaceAllString(h.URI, "v")
for _, match := range plainVar.FindAllStringSubmatch(uri, -1) {
vars[match[1]] = "*"
}
Expand All @@ -123,13 +123,13 @@ func (h *HTTPRule) GetVariables() map[string]string {
func (h *HTTPRule) GetPlainURI() string {
return plainVar.ReplaceAllString(
varSegment.ReplaceAllString(
templateSegment.ReplaceAllString(h.URI, "v"),
VersionedSegment.ReplaceAllString(h.URI, "v"),
"$2"),
"*")
}

var (
plainVar = regexp.MustCompile(`\{([^}=]+)\}`)
varSegment = regexp.MustCompile(`\{([^}=]+)=([^}]+)\}`)
templateSegment = regexp.MustCompile(`\{\$api_version\}`)
plainVar = regexp.MustCompile(`\{([^}=]+)\}`)
varSegment = regexp.MustCompile(`\{([^}=]+)=([^}]+)\}`)
VersionedSegment = regexp.MustCompile(`\{\$api_version\}`)
)

0 comments on commit 70085dc

Please sign in to comment.