Skip to content
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
2 changes: 0 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ type RequestContext interface {
QueryParam(name string) string
// Header retrieves the value of the request header for the given key.
Header(key string) string
// Pattern returns the registered route pattern or an empty string if the handler is called in a scope other than [RouteHandler].
Pattern() string
}

// Context represents the context of the current HTTP request. It provides methods to access request data and
Expand Down
2 changes: 2 additions & 0 deletions fox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ func Sub(router *Router) HandlerFunc {
// gracefully as a defensive measure: if the parent registers /api and the sub-router registers /,
// we treat it similarly to /api*{any} (optional wildcard), matching /api with the pattern /api/.
*subCtx.subPatterns = append(*subCtx.subPatterns, strings.TrimSuffix(c.pattern, "/"))
*subCtx.params = append(*subCtx.params, *c.params...)
*subCtx.paramsKeys = append((*subCtx.paramsKeys)[:0], keys...)
router.serveSubRouter(subCtx, "/")
return
}
Expand Down
20 changes: 20 additions & 0 deletions fox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,26 @@ func TestRouter_ServeHTTP_HandleSubRouter(t *testing.T) {
assert.Equal(t, http.StatusNotFound, w.Code)
})

t.Run("sub-router with parent param and static suffix propagates params", func(t *testing.T) {
var paramSeen, patternSeen string
sub := MustRouter()
sub.MustAdd(MethodGet, "/", func(c *Context) {
paramSeen = c.Param("ver")
patternSeen = c.Pattern()
})

fx := MustRouter()
require.NoError(t, onlyError(fx.Add(MethodGet, "/api/{ver}/admin", Sub(sub))))

req := httptest.NewRequest(http.MethodGet, "/api/v1/admin", nil)
w := httptest.NewRecorder()
fx.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "v1", paramSeen)
assert.Equal(t, "/api/{ver}/admin/", patternSeen)
})

t.Run("sub-router registered in non route handler", func(t *testing.T) {
sub := MustRouter()
sub.MustAdd(MethodGet, "/users", patternHandler)
Expand Down
12 changes: 4 additions & 8 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ func (t *iTree) lookupByPath(method, path string, c *Context, lazy bool) (int, *
}

func (t *iTree) allocateContext() *Context {
patterns := make([]string, 0)
params := make([]string, 0, t.maxParams)
keys := make([]string, 0, t.maxParams)
stacks := make(skipStack, 0, t.maxDepth)
return &Context{
params: &params,
skipStack: &stacks,
paramsKeys: &keys,
subPatterns: &patterns,
params: new(make([]string, 0, t.maxParams)),
skipStack: new(make(skipStack, 0, t.maxDepth)),
paramsKeys: new(make([]string, 0, t.maxParams)),
subPatterns: new(make([]string, 0)),
// This is a read only value, no reset. It's always the
// owner of the pool.
tree: t,
Expand Down
Loading