From 11f75e753330999e98bfcaafb8369edc69352e97 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 5 Aug 2020 12:09:40 -0300 Subject: [PATCH] Bug 26143: Make the API handle per_page=-1 This patch introduces handling for per_page=-1 and actually adds a missing feature to the API: being able to request all resources on a route. To test this: 1. Visit the libraries admin page 2. On the table, choose to display 'All' rows => FAIL: it doesn't refresh, the browser console displays a 500 error code and so the logs 3. Apply the tests patches 4. Run: $ kshell k$ prove t/Koha/REST/Plugin/Pagination.t \ t/db_dependent/Koha/REST/Plugin/Objects.t => FAIL: Tests fail loudly 5. Apply this patch 6. Restart plack 7. Repeat 2 => SUCCESS: choosing to display all, works 8. Repeat 4 => SUCCESS: Tests pass now! 9. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart (cherry picked from commit e4cae99f7ec76e40a57e4066e088b9b01caed744) Signed-off-by: Lucas Gass (cherry picked from commit 72bcf69f425a92ce4adfb4919a412d7797a82df2) Signed-off-by: Aleisha Amohia --- Koha/REST/Plugin/Objects.pm | 22 +++++++++++++++------- Koha/REST/Plugin/Pagination.pm | 22 +++++++++++++--------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 9912f43291..1c33e8d452 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -69,13 +69,15 @@ sub register { $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize'); $reserved_params->{_page} //= 1; - # Merge pagination into query attributes - $c->dbic_merge_pagination( - { - filter => $attributes, - params => $reserved_params - } - ); + unless ( $reserved_params->{_per_page} == -1 ) { + # Merge pagination into query attributes + $c->dbic_merge_pagination( + { + filter => $attributes, + params => $reserved_params + } + ); + } # Call the to_model function by reference, if defined if ( defined $filtered_params ) { @@ -105,6 +107,12 @@ sub register { params => $args, }); } + else { + $c->add_pagination_headers({ + total => $objects->count, + params => $args, + }); + } my @objects_list = map { ( defined $to_api ) diff --git a/Koha/REST/Plugin/Pagination.pm b/Koha/REST/Plugin/Pagination.pm index 02560055f0..5ab7daa7ba 100644 --- a/Koha/REST/Plugin/Pagination.pm +++ b/Koha/REST/Plugin/Pagination.pm @@ -61,20 +61,24 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys my ( $c, $args ) = @_; my $total = $args->{total}; - my $req_page = $args->{params}->{_page}; + my $req_page = $args->{params}->{_page} // 1; my $per_page = $args->{params}->{_per_page} // C4::Context->preference('RESTdefaultPageSize') // 20; - # do we need to paginate? - return $c unless $req_page; - - my $pages = int $total / $per_page; - $pages++ - if $total % $per_page > 0; + my $pages; + if ( $per_page == -1 ) { + $req_page = 1; + $pages = 1; + } + else { + $pages = int $total / $per_page; + $pages++ + if $total % $per_page > 0; + } my @links; - if ( $pages > 1 and $req_page > 1 ) { # Previous exists? + if ( $per_page != -1 and $pages > 1 and $req_page > 1 ) { # Previous exists? push @links, _build_link( $c, @@ -86,7 +90,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys ); } - if ( $pages > 1 and $req_page < $pages ) { # Next exists? + if ( $per_page != -1 and $pages > 1 and $req_page < $pages ) { # Next exists? push @links, _build_link( $c, -- 2.39.5