Skip to content

Commit 7b4be99

Browse files
Shogo Nagasakaphelrine
authored andcommitted
Fix node's parameter names assignment problem.
1 parent 036e696 commit 7b4be99

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
7979
r.insert(method, path[:i], h, pkind, ppath, pnames)
8080
return
8181
}
82-
r.insert(method, path[:i], nil, pkind, ppath, pnames)
82+
r.insert(method, path[:i], nil, pkind, "", nil)
8383
} else if path[i] == '*' {
8484
r.insert(method, path[:i], nil, skind, "", nil)
8585
pnames = append(pnames, "*")

router_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,42 @@ func TestRouterParamAlias(t *testing.T) {
874874
testRouterAPI(t, api)
875875
}
876876

877+
// Issue #1052
878+
func TestRouterParamOrdering(t *testing.T) {
879+
api := []*Route{
880+
{GET, "/:a/:b/:c/:id", ""},
881+
{GET, "/:a/:id", ""},
882+
{GET, "/:a/:e/:id", ""},
883+
}
884+
testRouterAPI(t, api)
885+
api2 := []*Route{
886+
{GET, "/:a/:id", ""},
887+
{GET, "/:a/:e/:id", ""},
888+
{GET, "/:a/:b/:c/:id", ""},
889+
}
890+
testRouterAPI(t, api2)
891+
api3 := []*Route{
892+
{GET, "/:a/:b/:c/:id", ""},
893+
{GET, "/:a/:e/:id", ""},
894+
{GET, "/:a/:id", ""},
895+
}
896+
testRouterAPI(t, api3)
897+
}
898+
899+
// Issue #1139
900+
func TestRouterMixedParams(t *testing.T) {
901+
api := []*Route{
902+
{GET, "/teacher/:tid/room/suggestions", ""},
903+
{GET, "/teacher/:id", ""},
904+
}
905+
testRouterAPI(t, api)
906+
api2 := []*Route{
907+
{GET, "/teacher/:id", ""},
908+
{GET, "/teacher/:tid/room/suggestions", ""},
909+
}
910+
testRouterAPI(t, api2)
911+
}
912+
877913
func benchmarkRouterRoutes(b *testing.B, routes []*Route) {
878914
e := New()
879915
r := e.router
@@ -914,7 +950,7 @@ func BenchmarkRouterGooglePlusAPI(b *testing.B) {
914950

915951
func (n *node) printTree(pfx string, tail bool) {
916952
p := prefix(tail, pfx, "└── ", "├── ")
917-
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.kind, n.parent, n.methodHandler)
953+
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v, pnames=%v\n", p, n.prefix, n, n.kind, n.parent, n.methodHandler, n.pnames)
918954

919955
children := n.children
920956
l := len(children)

0 commit comments

Comments
 (0)