Skip to content

Commit 353ee02

Browse files
authored
feat: disallow regexp constraint in optional wildcards (#77)
1 parent 835516a commit 353ee02

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ Pattern /src/file=*{path}
199199
/src/file= matches
200200
````
201201

202-
Named wildcard can include regular expression using the syntax `+{name:regexp}` or `*{name:regexp}`. Regular expressions cannot
203-
contain capturing groups, but can use non-capturing groups `(?:pattern)` instead. Regexp support is opt-in via `fox.AllowRegexpParam(true)` option.
202+
Named wildcards can include a regular expression constraint using the syntax `+{name:regexp}`. Regular expressions cannot
203+
contain capturing groups, but can use non-capturing groups `(?:pattern)` instead. Optional wildcards (`*{param}`) do not
204+
support regular expressions. Regexp support is opt-in via `fox.AllowRegexpParam(true)` option.
204205

205206
````
206207
Pattern /src/+{filepath:[A-Za-z/]+\.json}

fox.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,12 @@ func (fox *Router) parseRoute(url string) (parsedRoute, error) {
12121212
}
12131213

12141214
if url[i] == ':' {
1215+
// Optional wildcards (*{param}) do not support regular expressions because they match
1216+
// empty strings, making it impossible to disambiguate routes with different regexps that
1217+
// both match the same empty-string path.
1218+
if url[startCatchAll] == '*' {
1219+
return parsedRoute{}, fmt.Errorf("%w: %w in optional wildcard", ErrInvalidRoute, ErrRegexpNotAllowed)
1220+
}
12151221
previous = state
12161222
state = stateRegex
12171223
i++

fox_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,11 @@ func TestParseRoute(t *testing.T) {
32823282
path: "/foo/{:}",
32833283
wantErr: ErrInvalidRoute,
32843284
},
3285+
{
3286+
name: "unsupported regexp in optional wildcard",
3287+
path: "/foo/*{any:[A-z]+}",
3288+
wantErr: ErrInvalidRoute,
3289+
},
32853290
{
32863291
name: "unbalanced braces in param regexp",
32873292
path: "/foo/{bar:[A-z]+",
@@ -4825,9 +4830,7 @@ func TestTree_Has(t *testing.T) {
48254830
"/users/uid_{id:[A-z]+}/ch",
48264831
"/john/doe/",
48274832
"/foo/*{name}",
4828-
"/foo/*{name:[A-z]+}",
48294833
"/foo/uid_*{id}",
4830-
"/foo/uid_*{id:[A-z]+}",
48314834
}
48324835

48334836
f, _ := NewRouter(AllowRegexpParam(true))

0 commit comments

Comments
 (0)