Skip to content

Commit 8cabd1e

Browse files
clippitvishr
authored andcommitted
don't make router parse duplicated param when backtracing happens #1368 (#1369)
1 parent 8cfaf50 commit 8cabd1e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

router.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ func (r *Router) Find(method, path string, c Context) {
400400
if nn != nil {
401401
cn = nn
402402
nn = cn.parent // Next (Issue #954)
403+
if nn != nil {
404+
nk = nn.kind
405+
}
403406
search = ns
404407
if nk == pkind {
405408
goto Param

router_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,47 @@ func TestRouterStaticDynamicConflict(t *testing.T) {
851851
assert.Equal(t, 3, c.Get("c"))
852852
}
853853

854+
// Issue #1348
855+
func TestRouterParamBacktraceNotFound(t *testing.T) {
856+
e := New()
857+
r := e.router
858+
859+
// Add
860+
r.Add(http.MethodGet, "/:param1", func(c Context) error {
861+
return nil
862+
})
863+
r.Add(http.MethodGet, "/:param1/foo", func(c Context) error {
864+
return nil
865+
})
866+
r.Add(http.MethodGet, "/:param1/bar", func(c Context) error {
867+
return nil
868+
})
869+
r.Add(http.MethodGet, "/:param1/bar/:param2", func(c Context) error {
870+
return nil
871+
})
872+
873+
c := e.NewContext(nil, nil).(*context)
874+
875+
//Find
876+
r.Find(http.MethodGet, "/a", c)
877+
assert.Equal(t, "a", c.Param("param1"))
878+
879+
r.Find(http.MethodGet, "/a/foo", c)
880+
assert.Equal(t, "a", c.Param("param1"))
881+
882+
r.Find(http.MethodGet, "/a/bar", c)
883+
assert.Equal(t, "a", c.Param("param1"))
884+
885+
r.Find(http.MethodGet, "/a/bar/b", c)
886+
assert.Equal(t, "a", c.Param("param1"))
887+
assert.Equal(t, "b", c.Param("param2"))
888+
889+
c = e.NewContext(nil, nil).(*context)
890+
r.Find(http.MethodGet, "/a/bbbbb", c)
891+
he := c.handler(c).(*HTTPError)
892+
assert.Equal(t, http.StatusNotFound, he.Code)
893+
}
894+
854895
func testRouterAPI(t *testing.T, api []*Route) {
855896
e := New()
856897
r := e.router

0 commit comments

Comments
 (0)