Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tree): correctly expand the capacity of params #3502

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,45 @@ func TestRouteParamsByNameWithExtraSlash(t *testing.T) {
assert.Equal(t, "/is/super/great", wild)
}

// TestRouteParamsNotEmpty tests that context parameters will be set
// even if a route with params/wildcards is registered after the context
// initialisation (which happened in a previous requets).
func TestRouteParamsNotEmpty(t *testing.T) {
name := ""
lastName := ""
wild := ""
router := New()

w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")

assert.Equal(t, http.StatusNotFound, w.Code)

router.GET("/test/:name/:last_name/*wild", func(c *Context) {
name = c.Params.ByName("name")
lastName = c.Params.ByName("last_name")
var ok bool
wild, ok = c.Params.Get("wild")

assert.True(t, ok)
assert.Equal(t, name, c.Param("name"))
assert.Equal(t, lastName, c.Param("last_name"))

assert.Empty(t, c.Param("wtf"))
assert.Empty(t, c.Params.ByName("wtf"))

wtf, ok := c.Params.Get("wtf")
assert.Empty(t, wtf)
assert.False(t, ok)
})

w = PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "john", name)
assert.Equal(t, "smith", lastName)
assert.Equal(t, "/is/super/great", wild)
}

// TestHandleStaticFile - ensure the static file handles properly
func TestRouteStaticFile(t *testing.T) {
// SETUP file
Expand Down
16 changes: 15 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,14 @@ walk: // Outer loop for walking the tree
}

// Save param value
if params != nil && cap(*params) > 0 {
if params != nil {
// Preallocate capacity if necessary
if cap(*params) < int(globalParamsCount) {
newParams := make(Params, len(*params), globalParamsCount)
copy(newParams, *params)
*params = newParams
}

if value.params == nil {
value.params = params
}
Expand Down Expand Up @@ -544,6 +551,13 @@ walk: // Outer loop for walking the tree
case catchAll:
// Save param value
if params != nil {
// Preallocate capacity if necessary
if cap(*params) < int(globalParamsCount) {
newParams := make(Params, len(*params), globalParamsCount)
copy(newParams, *params)
*params = newParams
}

if value.params == nil {
value.params = params
}
Expand Down
34 changes: 31 additions & 3 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,9 @@ func TestTreeInvalidNodeType(t *testing.T) {

func TestTreeInvalidParamsType(t *testing.T) {
tree := &node{}
tree.wildChild = true
tree.children = append(tree.children, &node{})
tree.children[0].nType = 2
// add a child with wildcard
route := "/:path"
tree.addRoute(route, fakeHandler(route))

// set invalid Params type
params := make(Params, 0)
Expand All @@ -904,6 +904,34 @@ func TestTreeInvalidParamsType(t *testing.T) {
tree.getValue("/test", &params, getSkippedNodes(), false)
}

func TestTreeExpandParamsCapacity(t *testing.T) {
data := []struct {
path string
}{
{"/:path"},
{"/*path"},
}

for _, item := range data {
tree := &node{}
tree.addRoute(item.path, fakeHandler(item.path))
params := make(Params, 0)

value := tree.getValue("/test", &params, getSkippedNodes(), false)

if value.params == nil {
t.Errorf("Expected %s params to be set, but they weren't", item.path)
continue
}

if len(*value.params) != 1 {
t.Errorf("Wrong number of %s params: got %d, want %d",
item.path, len(*value.params), 1)
continue
}
}
}

func TestTreeWildcardConflictEx(t *testing.T) {
conflicts := [...]struct {
route string
Expand Down
Loading