diff --git a/path.go b/path.go index 927a97aa..6ea02951 100644 --- a/path.go +++ b/path.go @@ -437,10 +437,12 @@ func (routeParser *routeParser) getMatch(detectionPath, path string, params *[ma // take over the params positions params[paramsIterator] = path[:i] - // check constraint - for _, c := range segment.Constraints { - if matched := c.CheckConstraint(params[paramsIterator]); !matched { - return false + if !(segment.IsOptional && i == 0) { + // check constraint + for _, c := range segment.Constraints { + if matched := c.CheckConstraint(params[paramsIterator]); !matched { + return false + } } } diff --git a/path_test.go b/path_test.go index 5e89e1a8..a702eaa2 100644 --- a/path_test.go +++ b/path_test.go @@ -597,6 +597,12 @@ func Test_Path_matchParams(t *testing.T) { {url: "/api/v1/2005-1101/paach", params: nil, match: false}, {url: "/api/v1/2005-11-01/peach", params: []string{"2005-11-01", "peach"}, match: true}, }) + testCase("/api/v1/:param?", []testparams{ + {url: "/api/v1/entity", params: nil, match: false}, + {url: "/api/v1/8728382", params: []string{"8728382"}, match: true}, + {url: "/api/v1/true", params: nil, match: false}, + {url: "/api/v1/", params: []string{""}, match: true}, + }) } func Test_Utils_GetTrimmedParam(t *testing.T) { @@ -851,4 +857,10 @@ func Benchmark_Path_matchParams(t *testing.B) { {url: "/api/v1/2005-1101/paach", params: nil, match: false}, {url: "/api/v1/2005-11-01/peach", params: []string{"2005-11-01", "peach"}, match: true}, }) + benchCase("/api/v1/:param?", []testparams{ + {url: "/api/v1/entity", params: nil, match: false}, + {url: "/api/v1/8728382", params: []string{"8728382"}, match: true}, + {url: "/api/v1/true", params: nil, match: false}, + {url: "/api/v1/", params: []string{""}, match: true}, + }) }