From e5a51b78d35702bd9763e42d989dc46e66dbd8c5 Mon Sep 17 00:00:00 2001 From: J-guanghua <490011961@qq.com> Date: Tue, 27 Jun 2023 10:49:42 +0800 Subject: [PATCH] fix:go-http plugin path encoding error #2873 --- cmd/protoc-gen-go-http/http.go | 2 +- cmd/protoc-gen-go-http/http_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/protoc-gen-go-http/http.go b/cmd/protoc-gen-go-http/http.go index f52a20772..b7f1a74e6 100644 --- a/cmd/protoc-gen-go-http/http.go +++ b/cmd/protoc-gen-go-http/http.go @@ -240,7 +240,7 @@ func buildPathVars(path string) (res map[string]*string) { } func replacePath(name string, value string, path string) string { - pattern := regexp.MustCompile(fmt.Sprintf(`(?i){([\s]*%s[\s]*)=?([^{}]*)}`, name)) + pattern := regexp.MustCompile(fmt.Sprintf(`(?i){([\s]*%s\b[\s]*)=?([^{}]*)}`, name)) idx := pattern.FindStringIndex(path) if len(idx) > 0 { path = fmt.Sprintf("%s{%s:%s}%s", diff --git a/cmd/protoc-gen-go-http/http_test.go b/cmd/protoc-gen-go-http/http_test.go index 0070b2208..024f4c037 100644 --- a/cmd/protoc-gen-go-http/http_test.go +++ b/cmd/protoc-gen-go-http/http_test.go @@ -85,3 +85,16 @@ func TestIterationMiddle(t *testing.T) { t.Fatal(`replacePath("message.name", "messages/*", path) should be "/test/{message.name:messages/.*}/books"`) } } +func TestReplaceBoundary(t *testing.T) { + + path := "/test/{message.namespace=*}/name/{message.name=*}" + vars := buildPathVars(path) + for v, s := range vars { + if s != nil { + path = replacePath(v, *s, path) + } + } + if !reflect.DeepEqual("/test/{message.namespace:.*}/name/{message.name:.*}", path) { + t.Fatal(`"/test/{message.namespace=*}/name/{message.name=*}" should be "/test/{message.namespace:.*}/name/{message.name:.*}"`) + } +} \ No newline at end of file