From 969062a660d177cf4097b0438a76a4d157ef7c14 Mon Sep 17 00:00:00 2001 From: yousseftelda <58967038+yousseftelda@users.noreply.github.com> Date: Sat, 22 Aug 2020 08:39:07 +0200 Subject: [PATCH] Add option to configure unbound HTTP method warnings (#1609) --- internal/descriptor/registry.go | 8 ++++++++ internal/descriptor/services.go | 6 +++++- protoc-gen-grpc-gateway/main.go | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/descriptor/registry.go b/internal/descriptor/registry.go index a4c09b610db..480d2860cd7 100644 --- a/internal/descriptor/registry.go +++ b/internal/descriptor/registry.go @@ -84,6 +84,9 @@ type Registry struct { simpleOperationIDs bool standalone bool + // warnOnUnboundMethods causes the registry to emit warning logs if an RPC method + // has no HttpRule annotation. + warnOnUnboundMethods bool } type repeatedFieldSeparator struct { @@ -524,6 +527,11 @@ func (r *Registry) GetSimpleOperationIDs() bool { return r.simpleOperationIDs } +// SetWarnOnUnboundMethods sets warnOnUnboundMethods +func (r *Registry) SetWarnOnUnboundMethods(warn bool) { + r.warnOnUnboundMethods = warn +} + // sanitizePackageName replaces unallowed character in package name // with allowed character. func sanitizePackageName(pkgName string) string { diff --git a/internal/descriptor/services.go b/internal/descriptor/services.go index d2f1e0aaf85..b27b0b952ea 100644 --- a/internal/descriptor/services.go +++ b/internal/descriptor/services.go @@ -36,7 +36,11 @@ func (r *Registry) loadServices(file *File) error { optsList = append(optsList, opts) } if len(optsList) == 0 { - glog.Warningf("No HttpRule found for method: %s.%s", svc.GetName(), md.GetName()) + logFn := glog.V(1).Infof + if r.warnOnUnboundMethods { + logFn = glog.Warningf + } + logFn("No HttpRule found for method: %s.%s", svc.GetName(), md.GetName()) } meth, err := r.newMethod(svc, md, optsList) if err != nil { diff --git a/protoc-gen-grpc-gateway/main.go b/protoc-gen-grpc-gateway/main.go index c7fd89233f7..492db31dd99 100644 --- a/protoc-gen-grpc-gateway/main.go +++ b/protoc-gen-grpc-gateway/main.go @@ -36,6 +36,7 @@ var ( allowPatchFeature = flag.Bool("allow_patch_feature", true, "determines whether to use PATCH feature involving update masks (using google.protobuf.FieldMask).") standalone = flag.Bool("standalone", false, "generates a standalone gateway package, which imports the target service package") versionFlag = flag.Bool("version", false, "print the current version") + warnOnUnboundMethods = flag.Bool("warn_on_unbound_methods", false, "emit a warning message if an RPC method has no HttpRule annotation") ) // Variables set by goreleaser at build time @@ -96,6 +97,7 @@ func main() { reg.SetImportPath(*importPath) reg.SetAllowDeleteBody(*allowDeleteBody) reg.SetAllowRepeatedFieldsInBody(*allowRepeatedFieldsInBody) + reg.SetWarnOnUnboundMethods(*warnOnUnboundMethods) if err := reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil { emitError(err) return