@@ -, +, @@ $ patron_json = $patron->to_api({ embed => { holds_count => { is_count => 1 } } }); $json->{holds_count} = $self->holds->count; $ kshell k$ prove t/db_dependent/Koha/Object.t --- Koha/Object.pm | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) --- a/Koha/Object.pm +++ a/Koha/Object.pm @@ -428,21 +428,29 @@ sub to_api { if ($embeds) { foreach my $embed ( keys %{$embeds} ) { - my $curr = $embed; - my $next = $embeds->{$curr}->{children}; + if ( $embed =~ m/^(?.*)_count$/ + and $embeds->{$embed}->{is_count} ) { - my $children = $self->$curr; - - if ( defined $children and ref($children) eq 'ARRAY' ) { - my @list = map { - $self->_handle_to_api_child( - { child => $_, next => $next, curr => $curr } ) - } @{$children}; - $json_object->{$curr} = \@list; + my $relation = $+{relation}; + $json_object->{$embed} = $self->$relation->count; } else { - $json_object->{$curr} = $self->_handle_to_api_child( - { child => $children, next => $next, curr => $curr } ); + my $curr = $embed; + my $next = $embeds->{$curr}->{children}; + + my $children = $self->$curr; + + if ( defined $children and ref($children) eq 'ARRAY' ) { + my @list = map { + $self->_handle_to_api_child( + { child => $_, next => $next, curr => $curr } ) + } @{$children}; + $json_object->{$curr} = \@list; + } + else { + $json_object->{$curr} = $self->_handle_to_api_child( + { child => $children, next => $next, curr => $curr } ); + } } } } --