1
0
mirror of https://github.com/labstack/echo.git synced 2025-11-06 08:59:21 +02:00

Fix routing conflict for dynamic routes and static route with common prefix (#1509) (#1512)

* Add test for issue #1509 for dynamic routes and multiple static routes with common prefix

* Fix #1509: routing conflict for dynamic routes and static route with common prefix

* Improve routing performance for static only route trees
This commit is contained in:
Roland Lammel
2020-02-24 17:26:49 +01:00
committed by GitHub
parent f4b5a90ad3
commit 5ddc3a68ba
2 changed files with 34 additions and 5 deletions

View File

@@ -347,7 +347,14 @@ func (r *Router) Find(method, path string, c Context) {
if l == pl {
// Continue search
search = search[l:]
} else {
// Finish routing if no remaining search and we are on an leaf node
if search == "" && (nn == nil || cn.parent == nil || cn.ppath != "") {
break
}
}
// Attempt to go back up the tree on no matching prefix or no remaining search
if l != pl || search == "" {
if nn == nil { // Issue #1348
return // Not found
}
@@ -360,10 +367,6 @@ func (r *Router) Find(method, path string, c Context) {
}
}
if search == "" {
break
}
// Static node
if child = cn.findChild(search[0], skind); child != nil {
// Save next

View File

@@ -567,6 +567,32 @@ func TestRouterParamWithSlash(t *testing.T) {
})
}
// Issue #1509
func TestRouterParamStaticConflict(t *testing.T) {
e := New()
r := e.router
handler := func(c Context) error {
c.Set("path", c.Path())
return nil
}
g := e.Group("/g")
g.GET("/skills", handler)
g.GET("/status", handler)
g.GET("/:name", handler)
c := e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/g/s", c)
c.handler(c)
assert.Equal(t, "s", c.Param("name"))
assert.Equal(t, "/g/:name", c.Get("path"))
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/g/status", c)
c.handler(c)
assert.Equal(t, "/g/status", c.Get("path"))
}
func TestRouterMatchAny(t *testing.T) {
e := New()
r := e.router