Repository: googleapis/api-linter
Pull request #478 (2020-02-03 18:59:19)

[fix] Fix two-word noun false positives.
The `http-name-variable` and `http-parent-variable` rules each
have a false positive if the noun in question is two words.
This PR fixes that.


Patch:
diff --git a/rules/aip0136/http_name_variable.go b/rules/aip0136/http_name_variable.go
index b6bc6d9..d006956 100644
--- a/rules/aip0136/http_name_variable.go
+++ b/rules/aip0136/http_name_variable.go
@@ -44,7 +44,7 @@ var httpNameVariable = &lint.MethodRule{
 				// Determine the resource.
 				name = strings.TrimSuffix(name, "/*")
 				segs := strings.Split(name, "/")
-				resource := p.Singular(segs[len(segs)-1])
+				resource := strcase.SnakeCase(p.Singular(segs[len(segs)-1]))
 
 				// Does the RPC name end in the singular name of the resource?
 				// If not, complain.
diff --git a/rules/aip0136/http_parent_variable.go b/rules/aip0136/http_parent_variable.go
index d5b8633..993a7c9 100644
--- a/rules/aip0136/http_parent_variable.go
+++ b/rules/aip0136/http_parent_variable.go
@@ -38,7 +38,7 @@ var httpParentVariable = &lint.MethodRule{
 				// Determine the resource.
 				segs := strings.Split(strings.Split(http.GetPlainURI(), ":")[0], "/")
 				plural := segs[len(segs)-1]
-				singular := p.Singular(plural)
+				singular := strcase.SnakeCase(p.Singular(plural))
 
 				// Does the RPC name end in the singular or plural name of the resource?
 				// If not, complain.


Test patch:
diff --git a/rules/aip0136/http_name_variable_test.go b/rules/aip0136/http_name_variable_test.go
index 2661743..64afce0 100644
--- a/rules/aip0136/http_name_variable_test.go
+++ b/rules/aip0136/http_name_variable_test.go
@@ -32,6 +32,7 @@ func TestHTTPVariables(t *testing.T) {
 		{"ValidRevisions", "ListBookRevisions", "/v1/{name=publishers/*/books/*}:listRevisions", testutils.Problems{}},
 		{"Invalid", "WritePage", "/v1/{name=publishers/*/books/*}:writePage", testutils.Problems{{Message: "name variable"}}},
 		{"ValidBookVar", "WritePage", "/v1/{book=publishers/*/books/*}:writePage", testutils.Problems{}},
+		{"ValidTwoWordNoun", "WriteAudioBook", "/v1/{name=publishers/*/audioBooks/*}:write", testutils.Problems{}},
 	} {
 		t.Run(test.name, func(t *testing.T) {
 			f := testutils.ParseProto3Tmpl(t, `
diff --git a/rules/aip0136/http_parent_variable_test.go b/rules/aip0136/http_parent_variable_test.go
index ba2ccb6..265c012 100644
--- a/rules/aip0136/http_parent_variable_test.go
+++ b/rules/aip0136/http_parent_variable_test.go
@@ -29,6 +29,7 @@ func TestHTTPParentVariable(t *testing.T) {
 	}{
 		{"Valid", "WriteBook", "/v1/{parent=publishers/*}/books:write", testutils.Problems{}},
 		{"ValidPlural", "WriteBook", "/v1/{parent=publishers/*}/books:write", testutils.Problems{}},
+		{"ValidTwoWordNoun", "WriteAudioBook", "/v1/{parent=publishers/*}/audioBooks:write", testutils.Problems{}},
 		{"Invalid", "WritePage", "/v1/{parent=publishers/*/books/*}:writePage", testutils.Problems{{Message: "parent variable"}}},
 		{"ValidBookVar", "WritePage", "/v1/{book=publishers/*/books/*}:writePage", testutils.Problems{}},
 	} {
diff --git a/rules/aip0136/http_uri_suffix_test.go b/rules/aip0136/http_uri_suffix_test.go
index a4a5dde..40308fd 100644
--- a/rules/aip0136/http_uri_suffix_test.go
+++ b/rules/aip0136/http_uri_suffix_test.go
@@ -36,6 +36,7 @@ func TestURISuffix(t *testing.T) {
 		{"InvalidVerbNoun", "TranslateText", "/v3:translate", testutils.Problems{{Message: ":translateText"}}},
 		{"ValidOneWord", "Translate", "/v3:translate", testutils.Problems{}},
 		{"ValidStdMethod", "GetBook", "/v1/{name=publishers/*/books/*}", testutils.Problems{}},
+		{"ValidTwoWordNoun", "WriteAudioBook", "/v1/{name=publishers/*/audioBooks/*}:write", testutils.Problems{}},
 		{"ValidListRevisions", "ListBookRevisions", "/v1/{name=publishers/*/books/*}:listRevisions", testutils.Problems{}},
 		{"ValidTagRevision", "TagBookRevision", "/v1/{name=publishers/*/books/*}:tagRevision", testutils.Problems{}},
 		{"ValidDeleteRevision", "DeleteBookRevision", "/v1/{name=publishers/*/books/*}:deleteRevision", testutils.Problems{}},