Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 bug: fix regex constraints that contain comma #2256

Merged
merged 1 commit into from Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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