Skip to content

Commit

Permalink
fix:go-http plugin path encoding error (#2917)
Browse files Browse the repository at this point in the history
* fix:go-http plugin path encoding error

* fix:(http_test.go) File is not gofmt -ed with -s (gofmt)

* fix:(http_test.go) File is not gofmt -ed with -s (gofmt)

---------

Co-authored-by: J-guanghua <490011961@qq.com>
  • Loading branch information
J-guanghua and 490011961 committed Aug 4, 2023
1 parent 25e12da commit 79e862d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions cmd/protoc-gen-go-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:.*}"`)
}
}

0 comments on commit 79e862d

Please sign in to comment.