fix:go-http plugin path encoding error #2873

pull/2894/head
J-guanghua 1 year ago
parent fcd3b18e83
commit e5a51b78d3
  1. 2
      cmd/protoc-gen-go-http/http.go
  2. 13
      cmd/protoc-gen-go-http/http_test.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",

@ -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:.*}"`)
}
}
Loading…
Cancel
Save