Skip to content

Commit

Permalink
🐛 bug: fix regex constraints that contain comma (#2256)
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Dec 5, 2022
1 parent 6b9601f commit 077a5dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions path.go
Expand Up @@ -271,12 +271,12 @@ func (routeParser *routeParser) analyseParameterPart(pattern string) (string, *r
// Assign constraint
if start != -1 && end != -1 {
constraint := &Constraint{
ID: getParamConstraintType(c[:start]),
Data: splitNonEscaped(c[start+1:end], string(parameterConstraintDataSeparatorChars)),
ID: getParamConstraintType(c[:start]),
}

// remove escapes from data
if constraint.ID != regexConstraint {
constraint.Data = splitNonEscaped(c[start+1:end], string(parameterConstraintDataSeparatorChars))
if len(constraint.Data) == 1 {
constraint.Data[0] = RemoveEscapeChar(constraint.Data[0])
} else if len(constraint.Data) == 2 {
Expand All @@ -287,6 +287,7 @@ func (routeParser *routeParser) analyseParameterPart(pattern string) (string, *r

// Precompile regex if has regex constraint
if constraint.ID == regexConstraint {
constraint.Data = []string{c[start+1 : end]}
constraint.RegexCompiler = regexp.MustCompile(constraint.Data[0])
}

Expand Down
7 changes: 7 additions & 0 deletions path_test.go
Expand Up @@ -6,6 +6,7 @@ package fiber

import (
"fmt"
"strings"
"testing"

"github.com/gofiber/fiber/v2/utils"
Expand Down Expand Up @@ -526,6 +527,12 @@ func Test_Path_matchParams(t *testing.T) {
{url: "/api/v1/peach", params: []string{"peach"}, match: true},
{url: "/api/v1/p34ch", params: nil, match: false},
})
testCase("/api/v1/:param<regex(^[a-z0-9]([a-z0-9-]{1,61}[a-z0-9])?$)>", []testparams{
{url: "/api/v1/12", params: nil, match: false},
{url: "/api/v1/xy", params: nil, match: false},
{url: "/api/v1/test", params: []string{"test"}, match: true},
{url: "/api/v1/" + strings.Repeat("a", 64), params: nil, match: false},
})
testCase("/api/v1/:param<regex(\\d{4}-\\d{2}-\\d{2})}>", []testparams{
{url: "/api/v1/ent", params: nil, match: false},
{url: "/api/v1/15", params: nil, match: false},
Expand Down

0 comments on commit 077a5dc

Please sign in to comment.