fix(cmd/protoc-gen-go-http): Fix when replacement rule is not ending (#1721)

pull/1724/head
Giovanny Gutiérrez 3 years ago committed by GitHub
parent c1ab0cce3c
commit b6b95089c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      cmd/protoc-gen-go-http/http.go
  2. 19
      cmd/protoc-gen-go-http/http_test.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
}

@ -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)
}

Loading…
Cancel
Save