[Bps-public-commit] rt-extension-rest2 branch, rest2_api_search_enhancements, updated. 1.07-9-gb417411

Aaron Trevena ast at bestpractical.com
Mon Mar 30 09:47:26 EDT 2020


The branch, rest2_api_search_enhancements has been updated
       via  b417411b74fe9202ef2cc0b1dc659b7bbc676435 (commit)
      from  0498caa050032d9d7da85176ced49f9cddfa7269 (commit)

Summary of changes:
 lib/RT/Extension/REST2.pm                     |  8 +++-
 lib/RT/Extension/REST2/Resource/Collection.pm | 12 ++++-
 xt/search-simple.t                            | 64 +++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 4 deletions(-)
 create mode 100644 xt/search-simple.t

- Log -----------------------------------------------------------------
commit b417411b74fe9202ef2cc0b1dc659b7bbc676435
Author: Aaron Trevena <ast at bestpractical.com>
Date:   Mon Mar 30 14:34:36 2020 +0100

    Allow non-JSON searches to return disabled objects with 'find_disabled_rows=1' query parameter
    
    Add new test, keeps search API arguments consistent across formats

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index c2b5088..4c03dea 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -566,8 +566,6 @@ values).  An example:
         ]
     '
 
-By default, only enabled objects are returned. For results to include also disabled objectn you should specify C<find_disabled_rows=1> as query parameter.
-
 The JSON payload must be an array of hashes with the keys C<field> and C<value>
 and optionally C<operator>.
 
@@ -656,6 +654,12 @@ or previous page, then the URL for that page is returned in the next_page
 and prev_page variables respectively. It is up to you to store the required
 JSON to pass with the following page request.
 
+=head2 Disabled items
+
+By default, only enabled objects are returned. For results to include
+also disabled objectn you should specify C<find_disabled_rows=1> as a
+query parameter.
+
 =head2 Fields
 
 When fetching search results you can include additional fields by adding
diff --git a/lib/RT/Extension/REST2/Resource/Collection.pm b/lib/RT/Extension/REST2/Resource/Collection.pm
index db8b11f..734b391 100644
--- a/lib/RT/Extension/REST2/Resource/Collection.pm
+++ b/lib/RT/Extension/REST2/Resource/Collection.pm
@@ -45,12 +45,20 @@ sub setup_paging {
     $self->collection->GotoPage($page - 1);
 }
 
-sub limit_collection { 1 }
+sub limit_collection {
+    my $self        = shift;
+    my $collection  = $self->collection;
+    $collection->{'find_disabled_rows'} = 1
+        if $self->request->param('find_disabled_rows');
+
+    return 1;
+}
 
 sub search {
     my $self = shift;
+    $self->limit_collection;
     $self->setup_paging;
-    return $self->limit_collection;
+    return 1;
 }
 
 sub serialize {
diff --git a/xt/search-simple.t b/xt/search-simple.t
new file mode 100644
index 0000000..a850a71
--- /dev/null
+++ b/xt/search-simple.t
@@ -0,0 +1,64 @@
+use strict;
+use warnings;
+use RT::Extension::REST2::Test tests => undef;
+
+my $mech = RT::Extension::REST2::Test->mech;
+my $auth = RT::Extension::REST2::Test->authorization_header;
+my $rest_base_path = '/REST/2.0';
+my $user = RT::Extension::REST2::Test->user;
+
+my $alpha = RT::Test->load_or_create_queue( Name => 'Alpha', Description => 'Queue for test' );
+my $beta  = RT::Test->load_or_create_queue( Name => 'Beta', Description => 'Queue for test' );
+my $bravo = RT::Test->load_or_create_queue( Name => 'Bravo', Description => 'Queue to test sorted search' );
+$user->PrincipalObj->GrantRight( Right => 'SuperUser' );
+
+my $alpha_id = $alpha->Id;
+my $beta_id  = $beta->Id;
+my $bravo_id = $bravo->Id;
+
+# without disabled
+{
+    my $res = $mech->get("$rest_base_path/queues/all",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    my $content = $mech->json_response;
+    is($content->{count}, 4);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 4);
+    is(scalar @{$content->{items}}, 4);
+}
+
+# Find disabled
+{
+    $alpha->SetDisabled(1);
+    my $res = $mech->get("$rest_base_path/queues/all",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    my $content = $mech->json_response;
+    is($content->{count}, 3);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 3);
+    is(scalar @{$content->{items}}, 3);
+
+    $res = $mech->get("$rest_base_path/queues/all?find_disabled_rows=1",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    $content = $mech->json_response;
+    is($content->{count}, 5);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 5);
+    is(scalar @{$content->{items}}, 5);
+
+}
+
+done_testing;
+

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list