From 2aca3de94222e9e449ab2dd257b7a4b91b138081 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Thu, 19 Dec 2019 15:50:49 -0300 Subject: [PATCH] Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects This patch makes Koha::Object(s)->to_api have an 'embeds' parameter that using dot notation (e.g. resource.related_resource.another_one) allows embedding objects recursively. To test: 1. Apply this patch 2. prove t/db_dependent/Koha/Object.t Signed-off-by: Tomas Cohen Arazi --- Koha/Object.pm | 35 +++++++++++++++++++++++++++++-- Koha/Objects.pm | 4 ++-- t/db_dependent/Koha/Object.t | 40 +++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index ce4c231591..553e53d28b 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -380,12 +380,12 @@ Returns a representation of the object, suitable for API output. =cut sub to_api { - my ( $self ) = @_; + my ( $self, $embeds ) = @_; my $json_object = $self->TO_JSON; # Rename attributes if there's a mapping if ( $self->can('to_api_mapping') ) { - foreach my $column ( keys %{$self->to_api_mapping} ) { + foreach my $column ( keys %{ $self->to_api_mapping } ) { my $mapped_column = $self->to_api_mapping->{$column}; if ( exists $json_object->{$column} && defined $mapped_column ) @@ -402,9 +402,40 @@ sub to_api { } } + if ($embeds) { + foreach my $embed (@$embeds) { + my ( $curr, $next ) = split /\s*\.\s*/, $embed, 2; + my @nxembeds; + + @nxembeds = ($next) if $next; + + my $children = $self->$curr; + if ( ref $children eq 'ARRAY' ) { + my @list; + my $pos = 0; + foreach my $child (@$children) { + my $res = $child->to_api( \@nxembeds ); + $res = { $json_object->{$curr}->[$pos], $res } + if defined $json_object->{$curr} + && defined $json_object->{$curr}->[$pos]; + push @list, $res; + $pos++; + } + $json_object->{$curr} = \@list; + } + else { + my $res = $children->to_api( \@nxembeds ); + $res = { $json_object->{$curr}, $res } + if defined $json_object->{$curr}; + $json_object->{$curr} = $res; + } + } + } + return $json_object; } + =head3 $object->unblessed_all_relateds my $everything_into_one_hashref = $object->unblessed_all_relateds diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 31387e1442..05b1de467d 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -315,9 +315,9 @@ Returns a representation of the objects, suitable for API output . =cut sub to_api { - my ($self) = @_; + my ($self, $embeds) = @_; - return [ map { $_->to_api } $self->as_list ]; + return [ map { $_->to_api($embeds) } $self->as_list ]; } =head3 Koha::Objects->_wrap diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index a37695bc1f..af97834a7d 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -215,7 +215,7 @@ subtest 'TO_JSON tests' => sub { subtest "to_api() tests" => sub { - plan tests => 11; + plan tests => 18; $schema->storage->txn_begin; @@ -262,6 +262,44 @@ subtest "to_api() tests" => sub { my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' }); is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' ); + my $item_class = Test::MockModule->new('Koha::Item'); + $item_class->mock( 'to_api_mapping', + sub { + return { + itemnumber => 'item_id' + }; + } + ); + + my $hold_class = Test::MockModule->new('Koha::Hold'); + $hold_class->mock( 'to_api_mapping', + sub { + return { + reserve_id => 'hold_id' + }; + } + ); + + my $biblio = $builder->build_sample_biblio(); + my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } }); + + my @embeds = ('items'); + + my $biblio_api = $biblio->to_api(\@embeds); + + ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); + is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches'); + ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet'); + + @embeds = ('items.holds'); + $biblio_api = $biblio->to_api(\@embeds); + + ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); + is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches'); + ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded'); + is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches'); + $schema->storage->txn_rollback; }; -- 2.24.1