From cde962a6c1615c966d186cae33c501d7cffddc5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=AD=90?= <906604588@qq.com> Date: Fri, 18 Feb 2022 08:59:27 +0800 Subject: [PATCH] fix(cmd/http): warn only when a get or delete request declares a body (#1789) --- cmd/protoc-gen-go-http/http.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/protoc-gen-go-http/http.go b/cmd/protoc-gen-go-http/http.go index 3ecd41903..0d9458d82 100644 --- a/cmd/protoc-gen-go-http/http.go +++ b/cmd/protoc-gen-go-http/http.go @@ -110,6 +110,7 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati body string responseBody string ) + switch pattern := rule.Pattern.(type) { case *annotations.HttpRule_Get: path = pattern.Get @@ -137,8 +138,12 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati if body != "" { _, _ = fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: %s %s body should not be declared.\n", method, path) } - md.HasBody = false - } else if body == "*" { + } else { + if body == "" { + _, _ = fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: %s %s does not declare a body.\n", method, path) + } + } + if body == "*" { md.HasBody = true md.Body = "" } else if body != "" { @@ -146,7 +151,6 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati md.Body = "." + camelCaseVars(body) } else { md.HasBody = false - _, _ = fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: %s %s does not declare a body.\n", method, path) } if responseBody == "*" { md.ResponseBody = "" @@ -199,6 +203,10 @@ func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path } func buildPathVars(path string) (res map[string]*string) { + if strings.HasSuffix(path, "/") { + fmt.Fprintf(os.Stderr, "\u001B[31mERROR\u001B[m: Path %s should not end with \"/\" \n", path) + os.Exit(2) + } res = make(map[string]*string) pattern := regexp.MustCompile(`(?i){([a-z\.0-9_\s]*)=?([^{}]*)}`) matches := pattern.FindAllStringSubmatch(path, -1)