khrt / Raisin

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

Nested route_param loses params from parent route_param #77

Closed mschout closed 5 years ago

mschout commented 5 years ago

We ran into this problem today. Seems that using route_param inside another route_param loses the params for the parent route_param.

Here is a test case demonstrating the problem:

subtest 'params nested route_param' => sub {
    resource api => sub {
        params requires => { name => 'id', type => undef };
        route_param id => sub {
            params(
                requires => { name => 'start', type => undef },
                optional => { name => 'count', type => undef },
            );
            get sub { param };

            params(
                requires => { name => 'sub_id', type => undef },
            );
            route_param sub_id => sub {
                get sub { param };
            };
        }
    };

    my $app = Raisin::API->app;
    my $e = $app->routes->routes->[1];

    my %params = map { $_->name => $_ } @{ $e->params };

    ok $params{id}, 'id';
    is $params{id}->named, 1, 'named';
    is $params{id}->required, 1, 'required';

    ok $params{sub_id}, 'sub_id';
    is $params{sub_id}->named, 1, 'named';
    is $params{sub_id}->required, 1, 'required';
};

What actually ends up in %params is only the sub_id param. The id param is lost.

khrt commented 5 years ago

Thank you for providing a test scenario. Will take a look!

khrt commented 5 years ago

This must be fixed, but would be nice if you can verify that.

mschout commented 5 years ago

Thanks @khrt I will git it a spin today and let you know.

mschout commented 5 years ago

Working perfectly with v0.87, thanks!