Skip to content

Commit a39a035

Browse files
authored
Merge pull request #4308 from gofiber/fix-mounted-routes-regex-engine-issue
🐛 bug: preserve mounted sub-app regex handler during mount prefixing
2 parents ee0c55a + 25fd939 commit a39a035

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

domain.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,9 @@ func (d *domainRouter) mount(prefix string, subApp *App) Router {
421421
wrapperApp := New(Config{
422422
CaseSensitive: subApp.config.CaseSensitive,
423423
StrictRouting: subApp.config.StrictRouting,
424+
RegexHandler: subApp.config.RegexHandler,
424425
})
426+
wrapperApp.customConstraints = subApp.customConstraints
425427

426428
// Clone routes from the sub-app with domain-wrapped handlers.
427429
// Lock the sub-app while reading to prevent data races with concurrent

mount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (app *App) processSubAppsRoutes() {
202202
subAppRouteClone := app.copyRoute(subAppRoute)
203203

204204
// Add the parent route's path as a prefix to the sub-app's route
205-
app.addPrefixToRoute(route.path, subAppRouteClone)
205+
app.addPrefixToRoute(route.path, subAppRouteClone, route.group.app.config.RegexHandler, route.group.app.customConstraints...)
206206

207207
// Add the cloned sub-app's route to the slice of sub-app routes
208208
subRoutes[j] = subAppRouteClone

mount_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,49 @@ import (
99
"io"
1010
"net/http"
1111
"net/http/httptest"
12+
"regexp"
1213
"testing"
1314

1415
"github.com/stretchr/testify/require"
1516
)
1617

18+
func Test_App_Mount_PreservesSubAppRegexHandler(t *testing.T) {
19+
t.Parallel()
20+
21+
parent := New(Config{
22+
CaseSensitive: true,
23+
RegexHandler: func(pattern string) *matchOnlyRegexCompiler {
24+
return &matchOnlyRegexCompiler{re: regexp.MustCompile("(?i)" + pattern)}
25+
},
26+
})
27+
28+
sub := New(Config{
29+
CaseSensitive: true,
30+
RegexHandler: regexp.MustCompile,
31+
})
32+
sub.Get("/resource/:id<regex(ALLOW)>", func(c Ctx) error {
33+
return c.SendStatus(StatusOK)
34+
})
35+
36+
parent.Use("/mounted", sub)
37+
38+
resp, err := sub.Test(httptest.NewRequest(MethodGet, "/resource/ALLOW", http.NoBody))
39+
require.NoError(t, err)
40+
require.Equal(t, StatusOK, resp.StatusCode)
41+
42+
resp, err = parent.Test(httptest.NewRequest(MethodGet, "/mounted/resource/ALLOW", http.NoBody))
43+
require.NoError(t, err)
44+
require.Equal(t, StatusOK, resp.StatusCode)
45+
46+
resp, err = sub.Test(httptest.NewRequest(MethodGet, "/resource/allow", http.NoBody))
47+
require.NoError(t, err)
48+
require.Equal(t, StatusNotFound, resp.StatusCode)
49+
50+
resp, err = parent.Test(httptest.NewRequest(MethodGet, "/mounted/resource/allow", http.NoBody))
51+
require.NoError(t, err)
52+
require.Equal(t, StatusNotFound, resp.StatusCode)
53+
}
54+
1755
// go test -run Test_App_Mount
1856
func Test_App_Mount(t *testing.T) {
1957
t.Parallel()

router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func (app *App) customRequestHandler(rctx *fasthttp.RequestCtx) {
474474
}
475475
}
476476

477-
func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {
477+
func (app *App) addPrefixToRoute(prefix string, route *Route, regexHandler any, customConstraints ...CustomConstraint) *Route {
478478
prefixedPath := getGroupPath(prefix, route.Path)
479479
prettyPath := prefixedPath
480480
// Case-sensitive routing, all to lowercase
@@ -488,7 +488,7 @@ func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {
488488

489489
route.Path = prefixedPath
490490
route.path = RemoveEscapeChar(prettyPath)
491-
route.routeParser = parseRoute(prettyPath, app.config.RegexHandler, app.customConstraints...)
491+
route.routeParser = parseRoute(prettyPath, regexHandler, customConstraints...)
492492
route.root = false
493493
route.star = false
494494
route.caseSensitive = app.config.CaseSensitive

0 commit comments

Comments
 (0)