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 35-47 get '/empty' => sub { Link Here
35
35
36
get '/query' => sub {
36
get '/query' => sub {
37
    my $c     = shift;
37
    my $c     = shift;
38
    my $input = {
38
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($c->req->params->to_hash);
39
        _page     => 2,
40
        _per_page => 3,
41
        firstname => 'Manuel',
42
        surname   => 'Cohen Arazi'
43
    };
44
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
45
    $c->render(
39
    $c->render(
46
        json => {
40
        json => {
47
            filtered_params => $filtered_params,
41
            filtered_params => $filtered_params,
Lines 51-71 get '/query' => sub { Link Here
51
    );
45
    );
52
};
46
};
53
47
54
get '/query_full' => sub {
48
get '/query_full/:id/:subid' => sub {
55
    my $c     = shift;
49
    my $c     = shift;
56
    my $input = {
50
    my $params = $c->req->params->to_hash;
57
        _match    => 'exact',
51
    $params->{id} = $c->stash->{id};
58
        _order_by => 'blah',
52
    $params->{subid} = $c->stash->{subid};
59
        _page     => 2,
53
    my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($params);
60
        _per_page => 3,
61
        firstname => 'Manuel',
62
        surname   => 'Cohen Arazi'
63
    };
64
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
65
    $c->render(
54
    $c->render(
66
        json => {
55
        json => {
67
            filtered_params => $filtered_params,
56
            filtered_params => $filtered_params,
68
            reserved_params => $reserved_params
57
            reserved_params => $reserved_params,
58
            path_params => $path_params
69
        },
59
        },
70
        status => 200
60
        status => 200
71
    );
61
    );
Lines 202-217 use Test::Mojo; Link Here
202
192
203
subtest 'extract_reserved_params() tests' => sub {
193
subtest 'extract_reserved_params() tests' => sub {
204
194
205
    plan tests => 8;
195
    plan tests => 9;
206
196
207
    my $t = Test::Mojo->new;
197
    my $t = Test::Mojo->new;
208
198
209
    $t->get_ok('/query')->status_is(200)
199
    $t->get_ok('/query?_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
210
      ->json_is( '/filtered_params' =>
200
      ->json_is( '/filtered_params' =>
211
          { firstname => 'Manuel', surname => 'Cohen Arazi' } )
201
          { firstname => 'Manuel', surname => 'Cohen Arazi' } )
212
      ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
202
      ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
213
203
214
    $t->get_ok('/query_full')->status_is(200)
204
    $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)
215
      ->json_is(
205
      ->json_is(
216
        '/filtered_params' => {
206
        '/filtered_params' => {
217
            firstname => 'Manuel',
207
            firstname => 'Manuel',
Lines 223-228 subtest 'extract_reserved_params() tests' => sub { Link Here
223
            _per_page => 3,
213
            _per_page => 3,
224
            _match    => 'exact',
214
            _match    => 'exact',
225
            _order_by => 'blah'
215
            _order_by => 'blah'
216
        } )
217
      ->json_is(
218
        '/path_params' => {
219
            id => 'with',
220
            subid => 'path'
226
        } );
221
        } );
227
222
228
};
223
};
229
- 

Return to bug 24487