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

(-)a/Koha/REST/Plugin/Objects.pm (-1 / +11 lines)
Lines 54-60 sub register { Link Here
54
            my $attributes = {};
54
            my $attributes = {};
55
55
56
            # Extract reserved params
56
            # Extract reserved params
57
            my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
57
            my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
58
58
59
            # Merge sorting into query attributes
59
            # Merge sorting into query attributes
60
            $c->dbic_merge_sorting(
60
            $c->dbic_merge_sorting(
Lines 82-87 sub register { Link Here
82
                $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
82
                $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
83
            }
83
            }
84
84
85
            if ( defined $path_params ) {
86
87
                # Apply the mapping function to the passed params
88
                $filtered_params //= {};
89
                $path_params = $to_model->($path_params);
90
                foreach my $param (keys %{$path_params}) {
91
                    $filtered_params->{$param} = $path_params->{$param};
92
                }
93
            }
94
85
            # Perform search
95
            # Perform search
86
            my $objects = $objects_set->search( $filtered_params, $attributes );
96
            my $objects = $objects_set->search( $filtered_params, $attributes );
87
97
(-)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 32-44 get '/empty' => sub { Link Here
32
32
33
get '/query' => sub {
33
get '/query' => sub {
34
    my $c     = shift;
34
    my $c     = shift;
35
    my $input = {
35
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($c->req->params->to_hash);
36
        _page     => 2,
37
        _per_page => 3,
38
        firstname => 'Manuel',
39
        surname   => 'Cohen Arazi'
40
    };
41
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
42
    $c->render(
36
    $c->render(
43
        json => {
37
        json => {
44
            filtered_params => $filtered_params,
38
            filtered_params => $filtered_params,
Lines 48-68 get '/query' => sub { Link Here
48
    );
42
    );
49
};
43
};
50
44
51
get '/query_full' => sub {
45
get '/query_full/:id/:subid' => sub {
52
    my $c     = shift;
46
    my $c     = shift;
53
    my $input = {
47
    my $params = $c->req->params->to_hash;
54
        _match    => 'exact',
48
    $params->{id} = $c->stash->{id};
55
        _order_by => 'blah',
49
    $params->{subid} = $c->stash->{subid};
56
        _page     => 2,
50
    my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($params);
57
        _per_page => 3,
58
        firstname => 'Manuel',
59
        surname   => 'Cohen Arazi'
60
    };
61
    my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
62
    $c->render(
51
    $c->render(
63
        json => {
52
        json => {
64
            filtered_params => $filtered_params,
53
            filtered_params => $filtered_params,
65
            reserved_params => $reserved_params
54
            reserved_params => $reserved_params,
55
            path_params => $path_params
66
        },
56
        },
67
        status => 200
57
        status => 200
68
    );
58
    );
Lines 184-199 use Test::Mojo; Link Here
184
174
185
subtest 'extract_reserved_params() tests' => sub {
175
subtest 'extract_reserved_params() tests' => sub {
186
176
187
    plan tests => 8;
177
    plan tests => 9;
188
178
189
    my $t = Test::Mojo->new;
179
    my $t = Test::Mojo->new;
190
180
191
    $t->get_ok('/query')->status_is(200)
181
    $t->get_ok('/query?_page=2&_per_page=3&firstname=Manuel&surname=Cohen%20Arazi')->status_is(200)
192
      ->json_is( '/filtered_params' =>
182
      ->json_is( '/filtered_params' =>
193
          { firstname => 'Manuel', surname => 'Cohen Arazi' } )
183
          { firstname => 'Manuel', surname => 'Cohen Arazi' } )
194
      ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
184
      ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
195
185
196
    $t->get_ok('/query_full')->status_is(200)
186
    $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)
197
      ->json_is(
187
      ->json_is(
198
        '/filtered_params' => {
188
        '/filtered_params' => {
199
            firstname => 'Manuel',
189
            firstname => 'Manuel',
Lines 205-210 subtest 'extract_reserved_params() tests' => sub { Link Here
205
            _per_page => 3,
195
            _per_page => 3,
206
            _match    => 'exact',
196
            _match    => 'exact',
207
            _order_by => 'blah'
197
            _order_by => 'blah'
198
        } )
199
      ->json_is(
200
        '/path_params' => {
201
            id => 'with',
202
            subid => 'path'
208
        } );
203
        } );
209
204
210
};
205
};
211
- 

Return to bug 24487