Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit error on HTTP rules without a matching selector and log warning on unbound methods #1178

Merged
merged 2 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,29 @@ func (r *Registry) AddExternalHTTPRule(qualifiedMethodName string, rule *annotat
r.externalHTTPRules[qualifiedMethodName] = append(r.externalHTTPRules[qualifiedMethodName], rule)
}

// UnboundExternalHTTPRules returns the list of External HTTPRules
// which does not have a matching method in the registry
func (r *Registry) UnboundExternalHTTPRules() []string {
allServiceMethods := make(map[string]struct{})
for _, f := range r.files {
for _, s := range f.GetService() {
svc := &Service{File: f, ServiceDescriptorProto: s}
for _, m := range s.GetMethod() {
method := &Method{Service: svc, MethodDescriptorProto: m}
allServiceMethods[method.FQMN()] = struct{}{}
}
}
}

var missingMethods []string
for httpRuleMethod := range r.externalHTTPRules {
if _, ok := allServiceMethods[httpRuleMethod]; !ok {
missingMethods = append(missingMethods, httpRuleMethod)
}
}
return missingMethods
}

// AddPkgMap adds a mapping from a .proto file to proto package name.
func (r *Registry) AddPkgMap(file, protoPkg string) {
r.pkgMap[file] = protoPkg
Expand Down
40 changes: 40 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,43 @@ func TestLoadGoPackageInputPath(t *testing.T) {
t.Errorf("file.GoPkg = %#v; want %#v", got, want)
}
}

func TestUnboundExternalHTTPRules(t *testing.T) {
reg := NewRegistry()
methodName := ".example.ExampleService.Echo"
reg.AddExternalHTTPRule(methodName, nil)
assertStringSlice(t, "unbound external HTTP rules", reg.UnboundExternalHTTPRules(), []string{methodName})
loadFile(t, reg, `
name: "path/to/example.proto",
package: "example"
message_type <
name: "StringMessage"
field <
name: "string"
number: 1
label: LABEL_OPTIONAL
type: TYPE_STRING
>
>
service <
name: "ExampleService"
method <
name: "Echo"
input_type: "StringMessage"
output_type: "StringMessage"
>
>
`)
assertStringSlice(t, "unbound external HTTP rules", reg.UnboundExternalHTTPRules(), []string{})
}

func assertStringSlice(t *testing.T, message string, got, want []string) {
if len(got) != len(want) {
t.Errorf("%s = %#v len(%d); want %#v len(%d)", message, got, len(got), want, len(want))
}
for i := range want {
if got[i] != want[i] {
t.Errorf("%s[%d] = %#v; want %#v", message, i, got[i], want[i])
}
}
}
2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/descriptor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *Registry) loadServices(file *File) error {
optsList = append(optsList, opts)
}
if len(optsList) == 0 {
glog.V(1).Infof("Found non-target method: %s.%s", svc.GetName(), md.GetName())
glog.Warningf("No HttpRule found for method: %s.%s", svc.GetName(), md.GetName())
}
meth, err := r.newMethod(svc, md, optsList)
if err != nil {
Expand Down
59 changes: 59 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,65 @@ func TestExtractServicesSimple(t *testing.T) {
testExtractServices(t, []*descriptor.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
}

func TestExtractServicesWithoutAnnotation(t *testing.T) {
src := `
name: "path/to/example.proto",
package: "example"
message_type <
name: "StringMessage"
field <
name: "string"
number: 1
label: LABEL_OPTIONAL
type: TYPE_STRING
>
>
service <
name: "ExampleService"
method <
name: "Echo"
input_type: "StringMessage"
output_type: "StringMessage"
>
>
`
var fd descriptor.FileDescriptorProto
if err := proto.UnmarshalText(src, &fd); err != nil {
t.Fatalf("proto.UnmarshalText(%s, &fd) failed with %v; want success", src, err)
}
msg := &Message{
DescriptorProto: fd.MessageType[0],
Fields: []*Field{
{
FieldDescriptorProto: fd.MessageType[0].Field[0],
},
},
}
file := &File{
FileDescriptorProto: &fd,
GoPkg: GoPackage{
Path: "path/to/example.pb",
Name: "example_pb",
},
Messages: []*Message{msg},
Services: []*Service{
{
ServiceDescriptorProto: fd.Service[0],
Methods: []*Method{
{
MethodDescriptorProto: fd.Service[0].Method[0],
RequestType: msg,
ResponseType: msg,
},
},
},
},
}

crossLinkFixture(file)
testExtractServices(t, []*descriptor.FileDescriptorProto{&fd}, "path/to/example.proto", file.Services)
}

func TestExtractServicesCrossPackage(t *testing.T) {
srcs := []string{
`
Expand Down
5 changes: 5 additions & 0 deletions protoc-gen-grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func main() {
emitError(err)
return
}
unboundHTTPRules := reg.UnboundExternalHTTPRules()
if len(unboundHTTPRules) != 0 {
emitError(fmt.Errorf("HTTP rules without a matching selector: %s", strings.Join(unboundHTTPRules, ", ")))
return
}

var targets []*descriptor.File
for _, target := range req.FileToGenerate {
Expand Down