Skip to content

Commit

Permalink
Fix GetLocationFromRoute bug for optional params (#1922)
Browse files Browse the repository at this point in the history
* Add Global Layout for view render

* Add test case for Views Layout

* Update ctx_test.go

* Add App Name function to pass custom app name

* Remove json tag for function

* Change func to string

* Add test for AppName

* Add RedirectToRoute and RedirectBack with fallback if referer in header not found

* replace errors.New with fmt.Errorf

* simplified code

* Add tests for different formats

* Add method to get route location and add benchmarks

* Add ToString function

* Fix error

* rearrange case for fmt.Stringer

* Fix bug for error return

* Lock latest route for app.Name(namee string)

* decreasing timeout for client test with timeout

* remove println and adjust condition to > 0

* Change name to get route url

* Change name to get route url

* Update ctx.go

Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>

* Fix bug on getting url for optional and greedy params

* Fix greedy pattern

* This PR will fix #1921 (comment).

The optional and greedy params were not fetched correctly

* This PR will fix #1921 (comment).

The optional and greedy params were not fetched correctly

* This PR will fix #1921 (comment).

The optional and greedy params were not fetched correctly

Co-authored-by: RW <rene@gofiber.io>
Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
  • Loading branch information
3 people committed May 31, 2022
1 parent 68e922d commit 3bb4b7e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ctx.go
Expand Up @@ -1144,23 +1144,23 @@ func (c *Ctx) Bind(vars Map) error {
func (c *Ctx) getLocationFromRoute(route Route, params Map) (string, error) {
buf := bytebufferpool.Get()
for _, segment := range route.routeParser.segs {
if segment.IsParam {
for key, val := range params {
if key == segment.ParamName || segment.IsGreedy {
_, err := buf.WriteString(utils.ToString(val))
if err != nil {
return "", err
}
for key, val := range params {
if segment.IsParam && (key == segment.ParamName || (segment.IsGreedy && len(key) == 1 && isInCharset(key[0], greedyParameters))) {
_, err := buf.WriteString(utils.ToString(val))
if err != nil {
return "", err
}
}
} else {
}
if !segment.IsParam {
_, err := buf.WriteString(segment.Const)
if err != nil {
return "", err
}
}
}
location := buf.String()
// release buffer
bytebufferpool.Put(buf)
return location, nil
}
Expand Down
35 changes: 35 additions & 0 deletions ctx_test.go
Expand Up @@ -2753,6 +2753,41 @@ func Test_Ctx_Get_Location_From_Route_name(t *testing.T) {
utils.AssertEqual(t, "/user/fiber", location)
}

// go test -run Test_Ctx_Get_Location_From_Route_name_Optional_greedy
func Test_Ctx_Get_Location_From_Route_name_Optional_greedy(t *testing.T) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
app.Get("/:phone/*/send/*", func(c *Ctx) error {
return c.SendString("Phone: " + c.Params("phone") + "\nFirst Param: " + c.Params("*1") + "\nSecond Param: " + c.Params("*2"))
}).Name("SendSms")

location, err := c.GetRouteURL("SendSms", Map{
"phone": "23456789",
"*1": "sms",
"*2": "test-msg",
})
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "/23456789/sms/send/test-msg", location)
}

// go test -run Test_Ctx_Get_Location_From_Route_name_Optional_greedy_one_param
func Test_Ctx_Get_Location_From_Route_name_Optional_greedy_one_param(t *testing.T) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
app.Get("/:phone/*/send", func(c *Ctx) error {
return c.SendString("Phone: " + c.Params("phone") + "\nFirst Param: " + c.Params("*1"))
}).Name("SendSms")

location, err := c.GetRouteURL("SendSms", Map{
"phone": "23456789",
"*": "sms",
})
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "/23456789/sms/send", location)
}

type errorTemplateEngine struct{}

func (t errorTemplateEngine) Render(w io.Writer, name string, bind interface{}, layout ...string) error {
Expand Down
2 changes: 2 additions & 0 deletions path.go
Expand Up @@ -53,6 +53,8 @@ const (
var (
// slash has a special role, unlike the other parameters it must not be interpreted as a parameter
routeDelimiter = []byte{slashDelimiter, '-', '.'}
// list of greedy parameters
greedyParameters = []byte{wildcardParam, plusParam}
// list of chars for the parameter recognising
parameterStartChars = []byte{wildcardParam, plusParam, paramStarterChar}
// list of chars of delimiters and the starting parameter name char
Expand Down

0 comments on commit 3bb4b7e

Please sign in to comment.