Repository: googleapis/api-linter
Pull request #367 (2019-11-20 04:06:37)

[AIP-191] Require java_outer_classname annotation.
Uses #365 as a base because of mutual dependence on `hasPackage`.


Patch:
diff --git a/docs/rules/0191/java-outer-classname.md b/docs/rules/0191/java-outer-classname.md
new file mode 100644
index 0000000..4828978
--- /dev/null
+++ b/docs/rules/0191/java-outer-classname.md
@@ -0,0 +1,74 @@
+---
+rule:
+  aip: 191
+  name: [core, '0191', java-outer-classname]
+  summary: All proto files must set `option java_outer_classname`.
+permalink: /191/java-outer-classname
+redirect_from:
+  - /0191/java-outer-classname
+---
+
+# Java package annotation
+
+This rule enforces that every proto file for a public API surface sets
+`option java_outer_classname`, as mandated in [AIP-191][].
+
+## Details
+
+This rule looks at each proto file, and complains if the `java_outer_classname`
+file annotation is not present, or set to empty string. It recommends a
+sensible default based on the filename, but does **not** complain if the value
+is set to something else.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+syntax = "proto3";
+
+package google.example.v1;
+
+option java_package = "com.google.example.v1";
+option java_multiple_files = true;
+// Needs `option java_outer_classname = "LibraryProto";` or similar.
+
+message Book {}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+syntax = "proto3";
+
+package google.example.v1;
+
+option java_package = "com.google.example.v1";
+option java_multiple_files = true;
+option java_outer_classname = "LibraryProto";
+
+message Book {}
+```
+
+## Disabling
+
+If you need to violate this rule, use a comment at the top of the file.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0191::java-outer-classname=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+syntax = "proto3";
+
+package google.example.v1;
+
+option java_package = "com.google.example.v1";
+option java_multiple_files = true;
+
+message Book {}
+```
+
+[aip-191]: https://aip.dev/191
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0191/aip0191.go b/rules/aip0191/aip0191.go
index c0a0685..9c74eb9 100644
--- a/rules/aip0191/aip0191.go
+++ b/rules/aip0191/aip0191.go
@@ -28,6 +28,7 @@ func AddRules(r lint.RuleRegistry) error {
 		191,
 		filename,
 		javaMultipleFiles,
+		javaOuterClassname,
 		javaPackage,
 		syntax,
 	)
diff --git a/rules/aip0191/java_outer_classname.go b/rules/aip0191/java_outer_classname.go
new file mode 100644
index 0000000..9021856
--- /dev/null
+++ b/rules/aip0191/java_outer_classname.go
@@ -0,0 +1,44 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 		https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0191
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+	"github.com/stoewer/go-strcase"
+)
+
+var javaOuterClassname = &lint.FileRule{
+	Name:   lint.NewRuleName(191, "java-outer-classname"),
+	OnlyIf: hasPackage,
+	LintFile: func(f *desc.FileDescriptor) []lint.Problem {
+		if f.GetFileOptions().GetJavaOuterClassname() == "" {
+			want := strcase.UpperCamelCase(strings.ReplaceAll(f.GetName(), ".", "_"))
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"Proto files should set `option java_outer_classname = %q`.",
+					want,
+				),
+				Descriptor: f,
+				Location:   locations.FilePackage(f),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0191/java_outer_classname_test.go b/rules/aip0191/java_outer_classname_test.go
new file mode 100644
index 0000000..4df8529
--- /dev/null
+++ b/rules/aip0191/java_outer_classname_test.go
@@ -0,0 +1,41 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// 		https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0191
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestJavaOuterClassname(t *testing.T) {
+	for _, test := range []struct {
+		name       string
+		statements []string
+		problems   testutils.Problems
+	}{
+		{"Valid", []string{"package foo.v1;", `option java_outer_classname = "TestProto";`}, testutils.Problems{}},
+		{"Invalid", []string{"package foo.v1;", ""}, testutils.Problems{{Message: `java_outer_classname = "TestProto"`}}},
+		{"Ignored", []string{"", ""}, testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3String(t, strings.Join(test.statements, "\n"))
+			if diff := test.problems.SetDescriptor(f).Diff(javaOuterClassname.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}