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

(-)a/Koha/REST/Plugin/Query.pm (-9 / +33 lines)
Lines 90-108 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a Link Here
90
            my $attributes = $args->{attributes};
90
            my $attributes = $args->{attributes};
91
            my $result_set = $args->{result_set};
91
            my $result_set = $args->{result_set};
92
92
93
            if ( defined $args->{params}->{_order_by} ) {
93
            my @order_by_styles = (
94
                my $order_by = $args->{params}->{_order_by};
94
                '_order_by',
95
                $order_by = [ split(/,/, $order_by) ] if ( !reftype($order_by) && index(',',$order_by) == -1);
95
                '_order_by[]'
96
                if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
96
            );
97
                    my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) }
97
            my @order_by_params;
98
                                @{ $order_by };
98
99
                    $attributes->{order_by} = \@order_by;
99
            foreach my $order_by_style ( @order_by_styles ) {
100
                if ( defined $args->{params}->{$order_by_style} and ref($args->{params}->{$order_by_style}) eq 'ARRAY' )  {
101
                    push( @order_by_params, @{$args->{params}->{$order_by_style} });
100
                }
102
                }
101
                else {
103
                else {
102
                    $attributes->{order_by} = _build_order_atom({ string => $order_by, result_set => $result_set });
104
                    push @order_by_params, $args->{params}->{$order_by_style}
105
                        if defined $args->{params}->{$order_by_style};
103
                }
106
                }
104
            }
107
            }
105
108
109
            my @THE_order_by;
110
111
            foreach my $order_by_param ( @order_by_params ) {
112
                my $order_by;
113
                $order_by = [ split(/,/, $order_by_param) ]
114
                    if ( !reftype($order_by_param) && index(',',$order_by_param) == -1);
115
116
                if ($order_by) {
117
                    if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
118
                        my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) } @{ $order_by };
119
                        push( @THE_order_by, @order_by);
120
                    }
121
                    else {
122
                        push @THE_order_by, _build_order_atom({ string => $order_by, result_set => $result_set });
123
                    }
124
                }
125
            }
126
127
            $attributes->{order_by} = \@THE_order_by
128
                if scalar @THE_order_by > 0;
129
106
            return $attributes;
130
            return $attributes;
107
        }
131
        }
108
    );
132
    );
Lines 253-259 Merges parameters from $q_params into $filtered_params. Link Here
253
277
254
sub _reserved_words {
278
sub _reserved_words {
255
279
256
    my @reserved_words = qw( _match _order_by _page _per_page q query x-koha-query);
280
    my @reserved_words = qw( _match _order_by _order_by[] _page _per_page q query x-koha-query);
257
    return \@reserved_words;
281
    return \@reserved_words;
258
}
282
}
259
283
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-11 / +15 lines)
Lines 196-202 subtest 'objects.search helper' => sub { Link Here
196
196
197
subtest 'objects.search helper, sorting on mapped column' => sub {
197
subtest 'objects.search helper, sorting on mapped column' => sub {
198
198
199
    plan tests => 35;
199
    plan tests => 42;
200
200
201
    $schema->storage->txn_begin;
201
    $schema->storage->txn_begin;
202
202
Lines 209-215 subtest 'objects.search helper, sorting on mapped column' => sub { Link Here
209
    $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'C', city_country => 'Belarus' } });
209
    $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'C', city_country => 'Belarus' } });
210
210
211
    my $t = Test::Mojo->new;
211
    my $t = Test::Mojo->new;
212
    diag("CSV-param");
212
    # CSV-param
213
    $t->get_ok('/cities?_order_by=%2Bname,-country')
213
    $t->get_ok('/cities?_order_by=%2Bname,-country')
214
      ->status_is(200)
214
      ->status_is(200)
215
      ->json_has('/0')
215
      ->json_has('/0')
Lines 222-228 subtest 'objects.search helper, sorting on mapped column' => sub { Link Here
222
      ->json_is('/3/country' => 'Argentina')
222
      ->json_is('/3/country' => 'Argentina')
223
      ->json_hasnt('/4');
223
      ->json_hasnt('/4');
224
224
225
    diag("Multi-param: traditional");
225
    # Multi-param: traditional
226
    $t->get_ok('/cities?_order_by=%2Bname&_order_by=-country')
226
    $t->get_ok('/cities?_order_by=%2Bname&_order_by=-country')
227
      ->status_is(200)
227
      ->status_is(200)
228
      ->json_has('/0')
228
      ->json_has('/0')
Lines 235-249 subtest 'objects.search helper, sorting on mapped column' => sub { Link Here
235
      ->json_is('/3/country' => 'Argentina')
235
      ->json_is('/3/country' => 'Argentina')
236
      ->json_hasnt('/4');
236
      ->json_hasnt('/4');
237
237
238
    diag("Pipe-param: Passes validation (treated as a 'single value array or one string), subsequently explodes");
238
    # Multi-param: PHP Style, Passes validation as above, subsequntly explodes
239
    $t->get_ok('/cities?_order_by=%2Bname|-country')
240
      ->status_is(500);
241
242
    diag("Multi-param: PHP Style, Passes validation as above, subsequntly explodes");
243
    $t->get_ok('/cities?_order_by[]=%2Bname&_order_by[]=-country')
239
    $t->get_ok('/cities?_order_by[]=%2Bname&_order_by[]=-country')
244
      ->status_is(500);
240
      ->status_is(200)
241
      ->json_has('/0')
242
      ->json_has('/1')
243
      ->json_is('/0/name' => 'A')
244
      ->json_is('/1/name' => 'B')
245
      ->json_is('/2/name' => 'C')
246
      ->json_is('/2/country' => 'Belarus')
247
      ->json_is('/3/name' => 'C')
248
      ->json_is('/3/country' => 'Argentina')
249
      ->json_hasnt('/4');
245
250
246
    diag("Single-param");
251
    # Single-param
247
    $t->get_ok('/cities?_order_by=-name')
252
    $t->get_ok('/cities?_order_by=-name')
248
      ->status_is(200)
253
      ->status_is(200)
249
      ->json_has('/0')
254
      ->json_has('/0')
250
- 

Return to bug 27680