Skip to content

Commit

Permalink
fix(Context.Bind): should escape special char in path
Browse files Browse the repository at this point in the history
Other famous frameworks, such as expressjs or rails, will unescape the path params. We should follow the industry convention too.
  • Loading branch information
ysmood committed Jul 12, 2023
1 parent 1ee8e22 commit 1cd4cae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bind.go
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -35,7 +36,12 @@ func (b *DefaultBinder) BindPathParams(c Context, i interface{}) error {
values := c.ParamValues()
params := map[string][]string{}
for i, name := range names {
params[name] = []string{values[i]}
escaped, err := url.QueryUnescape(values[i])
if err != nil {
return err
}

params[name] = []string{escaped}
}
if err := b.bindData(i, params, "param"); err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
Expand Down
13 changes: 13 additions & 0 deletions bind_test.go
Expand Up @@ -468,6 +468,19 @@ func TestBindParam(t *testing.T) {
assert.Equal(t, "Jon Snow", u.Name)
}

// Bind param with escaped characters
{
c := e.NewContext(req, rec)
c.SetPath("/users/:name")
c.SetParamNames("name")
c.SetParamValues("John%2FSnow")

err := c.Bind(u)
if assert.NoError(t, err) {
assert.Equal(t, "John/Snow", u.Name)
}
}

// Second test for the absence of a param
c2 := e.NewContext(req, rec)
c2.SetPath("/users/:id")
Expand Down

0 comments on commit 1cd4cae

Please sign in to comment.