Bug 24528: Add a syntax to x-koha-embed to specify counts

This patch adds a + syntax to specify on the x-koha-embed header that we
want the count of a relation in the response. For example:

GET /patrons/1
x-koha-embed: checkouts+count

Would return a JSON representation of a Koha::Patron, with a new
attribute added: checkouts_count, which will be the result of calling
$patron->checkouts->count.

This is all done automatically in to_api. This patch makes parsing the
x-koha-embed header build the right structure for passing to the to_api
method.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/Koha/REST/Plugin/Query.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Sponsored-by: ByWater Solutions
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2020-01-29 09:24:35 -03:00 committed by Martin Renvoize
parent 58356c12bb
commit 2dde059059
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
2 changed files with 14 additions and 3 deletions

View File

@ -263,7 +263,13 @@ sub _parse_embed {
$result->{$curr} = { children => _parse_embed( $next ) };
}
else {
$result->{$curr} = {};
if ( $curr =~ m/^(?<relation>.*)\+count/ ) {
my $key = $+{relation} . "_count";
$result->{$key} = { is_count => 1 };
}
else {
$result->{$curr} = {};
}
}
return $result;

View File

@ -140,7 +140,8 @@ get '/stash_embed' => sub {
'x-koha-embed' => [
'checkouts',
'checkouts.item',
'library'
'library',
'holds+count'
]
}
}
@ -304,7 +305,7 @@ subtest '_build_query_params_from_api' => sub {
subtest 'stash_embed() tests' => sub {
plan tests => 12;
plan tests => 15;
my $t = Test::Mojo->new;
@ -316,6 +317,10 @@ subtest 'stash_embed() tests' => sub {
->status_is(200)
->json_is( { checkouts => { children => { item => {} } }, library => {} } );
$t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'holds+count' } )
->status_is(200)
->json_is( { holds_count => { is_count => 1 } } );
$t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
->status_is(400)
->json_is(