khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
62 stars 31 forks source link

route_param regex is broken by R::Routes::Endpoint::_build_regex if contains quantifiers... #119

Open lmasarati opened 2 years ago

lmasarati commented 2 years ago

Hello, I think there could be a problem with the line

$regex =~ s/[{}]//g;

in Raisin::Routes::Endpoint

sub _build_regex {
    my $self = shift;
    return $self->path if ref($self->path) eq 'Regexp';

    my $regex = $self->path;

    $regex =~ s/(.?)([:*?])(\w+)/$self->_rep_regex($1, $2, $3)/eg;
    $regex =~ s/[{}]//g;

    # Allows any extensions
    $regex .= "(?:\\\.[^.]+?)?";

    qr/^$regex$/;
}

If I define a route_param as

resource 'backup' => sub {
    params (
        requires('sessionId',
            type => Str,
            regex => qr/^[a-f0-9]{32}$/
        )
    );
    route_param 'sessionId' => sub {
...

then the aforementioned line translates the regex to

'regex' => qr/^\/backup\/(?<sessionId>(?i:[a-f0-9]32))(?:\.[^.]+?)?$/,

The workaround is to remove the regex from params definition, leaving as generic Str and check it later during the execution of sub...