From fc6b63afa762b00970657e1e1950352af11fc7ab Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Thu, 26 Dec 2019 19:31:04 -0300 Subject: [PATCH] Bug 24228: (follow-up) Check wether each child or children can('to_api') and throw exception otherwise --- Koha/Object.pm | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index c55016a800..1dc4bf9ce8 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -26,6 +26,7 @@ use Scalar::Util qw( looks_like_number ); use Try::Tiny; use Koha::Database; +use Koha::Exceptions; use Koha::Exceptions::Object; use Koha::DateUtils; @@ -429,21 +430,36 @@ sub to_api { my $next = $embeds->{$curr}->{children}; my $children = $self->$curr; + + my $res; if ( ref $children eq 'ARRAY' ) { + #when $self->$curr returns an array of Koha::Object my @list; - my $pos = 0; foreach my $child (@$children) { - my $res = $child->to_api({ embed => $next }); - $res = { $json_object->{$curr}->[$pos], $res } + Koha::Exceptions::BadParameter->throw($curr." is not a valid embeddable option.") unless $child->can('to_api'); + my $r = $child->to_api({ embed => $next }); + push @list, $r; + } + $res = \@list; + } + else { + #when $self->$curr returns a single Koha::Object or Koha::Objects + Koha::Exceptions::BadParameter->throw($curr." is not a valid embeddable option.") unless $children->can('to_api'); + $res = $children->to_api({ embed => $next }); + } + + if(ref $res eq 'ARRAY') { + my @list; + my $pos = 0; + foreach my $r (@$res) { + $r = { $json_object->{$curr}->[$pos], $r } if defined $json_object->{$curr} && defined $json_object->{$curr}->[$pos]; - push @list, $res; + push @list, $r; $pos++; } $json_object->{$curr} = \@list; - } - else { - my $res = $children->to_api({ embed => $next }); + } else { $res = { $json_object->{$curr}, $res } if defined $json_object->{$curr}; $json_object->{$curr} = $res; -- 2.17.1