[Bps-public-commit] rt-extension-rest2 branch, collection-hyperlinks-field, created. 1.09-23-g2dd7def

? sunnavy sunnavy at bestpractical.com
Wed Apr 14 12:11:03 EDT 2021


The branch, collection-hyperlinks-field has been created
        at  2dd7def03bbe4f48c3447a7b364d7cfa319c1514 (commit)

- Log -----------------------------------------------------------------
commit 1c787698f14378a9edb0ee416b8d267955e4c7a2
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Apr 14 22:39:33 2021 +0800

    Support _hyperlinks field in collection

diff --git a/lib/RT/Extension/REST2/Resource/Collection.pm b/lib/RT/Extension/REST2/Resource/Collection.pm
index 848bc1b..ee3245a 100644
--- a/lib/RT/Extension/REST2/Resource/Collection.pm
+++ b/lib/RT/Extension/REST2/Resource/Collection.pm
@@ -72,8 +72,32 @@ sub serialize {
         # Allow selection of desired fields
         if ($result) {
             for my $field (@fields) {
-                my $field_result = $self->expand_field($item, $field);
-                $result->{$field} = $field_result if defined $field_result;
+                if ( $field eq '_hyperlinks' ) {
+                    my $class = ref $item;
+                    $class =~ s!^RT::!RT::Extension::REST2::Resource::!;
+                    if ( $class->require ) {
+                        my $object = $class->new(
+                            record_class => ref $item,
+                            record_id    => $item->id,
+                            record       => $item,
+                            request      => $self->request,
+                            response     => Plack::Response->new,
+                        );
+                        if ( $object->can('hypermedia_links') ) {
+                            $result->{$field} = $object->hypermedia_links;
+                        }
+                        else {
+                            RT->Logger->warning("_hyperlinks is not supported by $class, skipping");
+                        }
+                    }
+                    else {
+                        RT->Logger->warning("Couldn't load $class, skipping _hyperlinks");
+                    }
+                }
+                else {
+                    my $field_result = $self->expand_field($item, $field);
+                    $result->{$field} = $field_result if defined $field_result;
+                }
             }
         }
         push @results, $result;

commit 2dd7def03bbe4f48c3447a7b364d7cfa319c1514
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Apr 14 22:51:51 2021 +0800

    Test _hyperlinks field for ticket search

diff --git a/xt/tickets.t b/xt/tickets.t
index 6c651d0..8bb61e2 100644
--- a/xt/tickets.t
+++ b/xt/tickets.t
@@ -194,7 +194,7 @@ my ($ticket_url, $ticket_id);
 
 # Ticket Search - Fields
 {
-    my $res = $mech->get("$rest_base_path/tickets?query=id>0&fields=Status,Subject",
+    my $res = $mech->get("$rest_base_path/tickets?query=id>0&fields=Status,Subject,_hyperlinks",
         'Authorization' => $auth,
     );
     is($res->code, 200);
@@ -204,7 +204,13 @@ my ($ticket_url, $ticket_id);
     my $ticket = $content->{items}->[0];
     is($ticket->{Subject}, 'Ticket creation using REST');
     is($ticket->{Status}, 'new');
-    is(scalar keys %$ticket, 5);
+
+    my $links = $ticket->{_hyperlinks};
+    is( @$links, 2, '2 links by default' );
+    like( $links->[0]{_url}, qr{$rest_base_path/ticket/1$},         'Self link' );
+    like( $links->[1]{_url}, qr{$rest_base_path/ticket/1/history$}, 'History link' );
+
+    is(scalar keys %$ticket, 6);
 }
 
 # Ticket Search - Fields, sub objects, no right to see Queues

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


More information about the Bps-public-commit mailing list