View | Details | Raw Unified | Return to bug 24487
Collapse All | Expand All

(-)a/Koha/REST/Plugin/Objects.pm (-1 / +11 lines)
Lines 50-56 sub register { Link Here
50
            my $attributes = {};
50
            my $attributes = {};
51
51
52
            # Extract reserved params
52
            # Extract reserved params
53
            my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
53
            my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
54
            # Look for embeds
54
            # Look for embeds
55
            my $embed = $c->stash('koha.embed');
55
            my $embed = $c->stash('koha.embed');
56
56
Lines 79-84 sub register { Link Here
79
                $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
79
                $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
80
            }
80
            }
81
81
82
            if ( defined $path_params ) {
83
84
                # Apply the mapping function to the passed params
85
                $filtered_params //= {};
86
                $path_params = $result_set->attributes_from_api($path_params);
87
                foreach my $param (keys %{$path_params}) {
88
                    $filtered_params->{$param} = $path_params->{$param};
89
                }
90
            }
91
82
            # Perform search
92
            # Perform search
83
            my $objects = $result_set->search( $filtered_params, $attributes );
93
            my $objects = $result_set->search( $filtered_params, $attributes );
84
94
(-)a/Koha/REST/Plugin/Query.pm (-2 / +7 lines)
Lines 54-72 Generates the DBIC query from the query parameters. Link Here
54
54
55
            my $reserved_params;
55
            my $reserved_params;
56
            my $filtered_params;
56
            my $filtered_params;
57
            my $path_params;
57
58
58
            my $reserved_words = _reserved_words();
59
            my $reserved_words = _reserved_words();
60
            my @query_param_names = keys %{$c->req->params->to_hash};
59
61
60
            foreach my $param ( keys %{$params} ) {
62
            foreach my $param ( keys %{$params} ) {
61
                if ( grep { $param eq $_ } @{$reserved_words} ) {
63
                if ( grep { $param eq $_ } @{$reserved_words} ) {
62
                    $reserved_params->{$param} = $params->{$param};
64
                    $reserved_params->{$param} = $params->{$param};
63
                }
65
                }
64
                else {
66
                elsif ( grep { $param eq $_ } @query_param_names ) {
65
                    $filtered_params->{$param} = $params->{$param};
67
                    $filtered_params->{$param} = $params->{$param};
66
                }
68
                }
69
                else {
70
                    $path_params->{$param} = $params->{$param};
71
                }
67
            }
72
            }
68
73
69
            return ( $filtered_params, $reserved_params );
74
            return ( $filtered_params, $reserved_params, $path_params );
70
        }
75
        }
71
    );
76
    );
72
77
(-)a/t/Koha/REST/Plugin/Query.t (-22 / +16 lines)
Lines 34-46 get '/empty' => sub { Link Here
34
34
35
get '/query' => sub {
35
get '/query' => sub {
36
    my $c     = shift;
36
    my $c     = shift;
37
    my $input = {
37
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($c->req->params->to_hash);
38
        _page     => 2,
39
        _per_page => 3,
40
        firstname => 'Manuel',
41
        surname   => 'Cohen Arazi'
42
    };
43
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
44
    $c->render(
38
    $c->render(
45
        json => {
39
        json => {
46
            filtered_params => $filtered_params,
40
            filtered_params => $filtered_params,
Lines 50-70 get '/query' => sub { Link Here
50
    );
44
    );
51
};
45
};
52
46
53
get '/query_full' => sub {
47
get '/query_full/:id/:subid' => sub {
54
    my $c     = shift;
48
    my $c     = shift;
55
    my $input = {
49
    my $params = $c->req->params->to_hash;
56
        _match    => 'exact',
50
    $params->{id} = $c->stash->{id};
57
        _order_by => 'blah',
51
    $params->{subid} = $c->stash->{subid};
58
        _page     => 2,
52
    my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($params);
59
        _per_page => 3,
60
        firstname => 'Manuel',
61
        surname   => 'Cohen Arazi'
62
    };
63
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
64
    $c->render(
53
    $c->render(
65
        json => {
54
        json => {
66
            filtered_params => $filtered_params,
55
            filtered_params => $filtered_params,
67
            reserved_params => $reserved_params
56
            reserved_params => $reserved_params,
57
            path_params => $path_params
68
        },
58
        },
69
        status => 200
59
        status => 200
70
    );
60
    );
Lines 187-202 use Test::Mojo; Link Here
187
177
188
subtest 'extract_reserved_params() tests' => sub {
178
subtest 'extract_reserved_params() tests' => sub {
189
179
190
    plan tests => 8;
180
    plan tests => 9;
191
181
192
    my $t = Test::Mojo->new;
182
    my $t = Test::Mojo->new;
193
183
194
    $t->get_ok('/query')->status_is(200)
184
    $t->get_ok('/query?_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
195
      ->json_is( '/filtered_params' =>
185
      ->json_is( '/filtered_params' =>
196
          { firstname => 'Manuel', surname => 'Cohen Arazi' } )
186
          { firstname => 'Manuel', surname => 'Cohen Arazi' } )
197
      ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
187
      ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
198
188
199
    $t->get_ok('/query_full')->status_is(200)
189
    $t->get_ok('/query_full/with/path?_match=exact&_order_by=blah&_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
200
      ->json_is(
190
      ->json_is(
201
        '/filtered_params' => {
191
        '/filtered_params' => {
202
            firstname => 'Manuel',
192
            firstname => 'Manuel',
Lines 208-213 subtest 'extract_reserved_params() tests' => sub { Link Here
208
            _per_page => 3,
198
            _per_page => 3,
209
            _match    => 'exact',
199
            _match    => 'exact',
210
            _order_by => 'blah'
200
            _order_by => 'blah'
201
        } )
202
      ->json_is(
203
        '/path_params' => {
204
            id => 'with',
205
            subid => 'path'
211
        } );
206
        } );
212
207
213
};
208
};
214
- 

Return to bug 24487