Skip to content

Commit 5bf6888

Browse files
asahasrabuddhevishr
authored andcommitted
Parameterized routes sometimes return 404 (#1480)
* url param bug * add comment * add tests * Bump echo version
1 parent 399da56 commit 5bf6888

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

echo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ const (
227227

228228
const (
229229
// Version of Echo
230-
Version = "4.1.13"
230+
Version = "4.1.14"
231231
website = "https://echo.labstack.com"
232232
// http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo
233233
banner = `

router.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ func (r *Router) Find(method, path string, c Context) {
414414
if cn = nn.findChildByKind(pkind); cn != nil && strings.IndexByte(ns, '/') == -1 {
415415
pvalues[len(cn.pnames)-1] = search
416416
break
417+
} else if cn != nil && strings.IndexByte(ns, '/') != 1 {
418+
// If slash is present, it means that this is a parameterized route.
419+
cn = cn.parent
420+
goto Param
417421
}
418422
for {
419423
np = nn.parent

router_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,16 @@ func TestRouterParam1466(t *testing.T) {
11411141
r.Add(http.MethodGet, "/skills/:name/users", func(c Context) error {
11421142
return nil
11431143
})
1144+
// Additional routes for Issue 1479
1145+
r.Add(http.MethodGet, "/users/:username/likes/projects/ids", func(c Context) error {
1146+
return nil
1147+
})
1148+
r.Add(http.MethodGet, "/users/:username/profile", func(c Context) error {
1149+
return nil
1150+
})
1151+
r.Add(http.MethodGet, "/users/:username/uploads/:type", func(c Context) error {
1152+
return nil
1153+
})
11441154

11451155
c := e.NewContext(nil, nil).(*context)
11461156

@@ -1152,6 +1162,26 @@ func TestRouterParam1466(t *testing.T) {
11521162

11531163
r.Find(http.MethodGet, "/users/signup", c)
11541164
assert.Equal(t, "", c.Param("username"))
1165+
// Additional assertions for #1479
1166+
r.Find(http.MethodGet, "/users/sharewithme/likes/projects/ids", c)
1167+
assert.Equal(t, "sharewithme", c.Param("username"))
1168+
1169+
r.Find(http.MethodGet, "/users/ajitem/likes/projects/ids", c)
1170+
assert.Equal(t, "ajitem", c.Param("username"))
1171+
1172+
r.Find(http.MethodGet, "/users/sharewithme/profile", c)
1173+
assert.Equal(t, "sharewithme", c.Param("username"))
1174+
1175+
r.Find(http.MethodGet, "/users/ajitem/profile", c)
1176+
assert.Equal(t, "ajitem", c.Param("username"))
1177+
1178+
r.Find(http.MethodGet, "/users/sharewithme/uploads/self", c)
1179+
assert.Equal(t, "sharewithme", c.Param("username"))
1180+
assert.Equal(t, "self", c.Param("type"))
1181+
1182+
r.Find(http.MethodGet, "/users/ajitem/uploads/self", c)
1183+
assert.Equal(t, "ajitem", c.Param("username"))
1184+
assert.Equal(t, "self", c.Param("type"))
11551185
}
11561186

11571187
func benchmarkRouterRoutes(b *testing.B, routes []*Route) {

0 commit comments

Comments
 (0)