Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pkg/route/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,30 @@ func checkPathValid(path string) {
}
}

func checkDuplicateParams(path string) {
seen := make(map[string]bool)

for i := 0; i < len(path); i++ {
if path[i] == ':' {
start := i + 1
end := start
for end < len(path) && path[end] != '/' {
end++
}
name := path[start:end]
if seen[name] {
panic(fmt.Sprintf("duplicate param name %q in path %q", name, path))
}
seen[name] = true
i = end - 1
}
}
}

// addRoute adds a node with the given handle to the path.
func (r *router) addRoute(path string, h app.HandlersChain) {
checkPathValid(path)

checkDuplicateParams(path)
var (
pnames []string // Param names
ppath = path // Pristine path
Expand Down
11 changes: 11 additions & 0 deletions pkg/route/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,3 +735,14 @@ func TestTreeParamNotOptimize(t *testing.T) {
{"/1", false, "/:paramb", param.Params{param.Param{Key: "paramb", Value: "1"}}},
})
}

func TestDuplicateParamNamePanic(t *testing.T) {
tree := &router{method: "GET", root: &node{}}

recv := catchPanic(func() {
tree.addRoute("/user/:id/order/:id", fakeHandler("/user/:id/order/:id"))
})
if recv == nil {
t.Error("expected panic for duplicate param names, but got none")
}
}