Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 714dd3a

Browse files
committedJul 12, 2023
fix(Context.Bind): should unescape special char in path
Other famous frameworks, such as expressjs or rails, will unescape the path params. We should follow the industry convention too.
1 parent 1ee8e22 commit 714dd3a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
 

‎bind.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9+
"net/url"
910
"reflect"
1011
"strconv"
1112
"strings"
@@ -35,7 +36,12 @@ func (b *DefaultBinder) BindPathParams(c Context, i interface{}) error {
3536
values := c.ParamValues()
3637
params := map[string][]string{}
3738
for i, name := range names {
38-
params[name] = []string{values[i]}
39+
escaped, err := url.QueryUnescape(values[i])
40+
if err != nil {
41+
return err
42+
}
43+
44+
params[name] = []string{escaped}
3945
}
4046
if err := b.bindData(i, params, "param"); err != nil {
4147
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)

‎bind_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,19 @@ func TestBindParam(t *testing.T) {
468468
assert.Equal(t, "Jon Snow", u.Name)
469469
}
470470

471+
// Bind param with escaped characters
472+
{
473+
c := e.NewContext(req, rec)
474+
c.SetPath("/users/:name")
475+
c.SetParamNames("name")
476+
c.SetParamValues("John%2FSnow")
477+
478+
err := c.Bind(u)
479+
if assert.NoError(t, err) {
480+
assert.Equal(t, "John/Snow", u.Name)
481+
}
482+
}
483+
471484
// Second test for the absence of a param
472485
c2 := e.NewContext(req, rec)
473486
c2.SetPath("/users/:id")

0 commit comments

Comments
 (0)
Please sign in to comment.