diff --git a/cmd/protoc-gen-go-http/http.go b/cmd/protoc-gen-go-http/http.go index 4e985451c..3ecd41903 100644 --- a/cmd/protoc-gen-go-http/http.go +++ b/cmd/protoc-gen-go-http/http.go @@ -214,13 +214,15 @@ 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[\s]*)=?([^{}]*)}`, name)) idx := pattern.FindStringIndex(path) if len(idx) > 0 { - path = fmt.Sprintf("%s{%s:%s}", + path = fmt.Sprintf("%s{%s:%s}%s", path[:idx[0]], // The start of the match name, - strings.ReplaceAll(value, "*", ".*")) + strings.ReplaceAll(value, "*", ".*"), + path[idx[1]:], + ) } return path } diff --git a/cmd/protoc-gen-go-http/http_test.go b/cmd/protoc-gen-go-http/http_test.go index f28a7f410..d4f861807 100644 --- a/cmd/protoc-gen-go-http/http_test.go +++ b/cmd/protoc-gen-go-http/http_test.go @@ -29,11 +29,11 @@ func TestTwoParametersReplacement(t *testing.T) { } func TestNoReplacePath(t *testing.T) { - path := "/test/{message.id}" - assert.Equal(t, path, replacePath("message.id", "", path)) - - path = "/test/{message.id=test}" + path := "/test/{message.id=test}" assert.Equal(t, "/test/{message.id:test}", replacePath("message.id", "test", path)) + + path = "/test/{message.id=test/*}" + assert.Equal(t, "/test/{message.id:test/.*}", replacePath("message.id", "test/*", path)) } func TestReplacePath(t *testing.T) { @@ -52,3 +52,14 @@ func TestIteration(t *testing.T) { } assert.Equal(t, "/test/{message.id}/{message.name:messages/.*}", path) } + +func TestIterationMiddle(t *testing.T) { + path := "/test/{message.name=messages/*}/books" + vars := buildPathVars(path) + for v, s := range vars { + if s != nil { + path = replacePath(v, *s, path) + } + } + assert.Equal(t, "/test/{message.name:messages/.*}/books", path) +}