Skip to content

Commit

Permalink
πŸ› Fix wrong route matching for not greedy parameter (#1017)
Browse files Browse the repository at this point in the history
* πŸ› Fix wrong route matching for not greedy parameter
#1016

Co-authored-by: Fenny <[email protected]>
  • Loading branch information
ReneWerner87 and Fenny authored Nov 13, 2020
1 parent df78ede commit 6ffc89f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ func findParamLen(s string, segment *routeSegment) int {
return constPosition
}
} else if constPosition := strings.Index(s, segment.ComparePart); constPosition != -1 {
// if the compare part was found, but contains a slash although this part is not greedy, then it must not match
// example: /api/:param/fixedEnd -> path: /api/123/456/fixedEnd = no match , /api/123/fixedEnd = match
if !segment.IsGreedy && strings.IndexByte(s[:constPosition], slashDelimiter) != -1 {
return 0
}
return constPosition
}

Expand Down
8 changes: 8 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func Test_Path_matchParams(t *testing.T) {
{url: "/api/v1/", params: nil, match: false},
{url: "/api/v1/something", params: nil, match: false},
})
testCase("/api/:param/fixedEnd", []testparams{
{url: "/api/abc/fixedEnd", params: []string{"abc"}, match: true},
{url: "/api/abc/def/fixedEnd", params: nil, match: false},
})
testCase("/shop/product/::filter/color::color/size::size", []testparams{
{url: "/shop/product/:test/color:blue/size:xs", params: []string{"test", "blue", "xs"}, match: true},
{url: "/shop/product/test/color:blue/size:xs", params: nil, match: false},
Expand Down Expand Up @@ -430,6 +434,10 @@ func Benchmark_Path_matchParams(t *testing.B) {

}
}
benchCase("/api/:param/fixedEnd", []testparams{
{url: "/api/abc/fixedEnd", params: []string{"abc"}, match: true},
{url: "/api/abc/def/fixedEnd", params: nil, match: false},
})
benchCase("/api/v1/:param/*", []testparams{
{url: "/api/v1/entity", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/", params: []string{"entity", ""}, match: true},
Expand Down

0 comments on commit 6ffc89f

Please sign in to comment.