Bug 24302: Add a way to specify nested objects to embed in OpenAPI

This patch introduces a helper for handling x-koha-embed headers on API
requests. It reads the embed definitions and adds them to the stash for
later use (either manually on the controllers, or in the objects.search
helper.

x-koha-embed needs to be defined as a list on the OpenAPI spec.

It throws an exception when invalid combinations are found.

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

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2019-12-23 10:26:04 -03:00 committed by Martin Renvoize
parent 954c742cc6
commit 4dff91004e
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
2 changed files with 176 additions and 1 deletions

View File

@ -18,6 +18,7 @@ package Koha::REST::Plugin::Query;
use Modern::Perl;
use Mojo::Base 'Mojolicious::Plugin';
use List::MoreUtils qw(any);
use Scalar::Util qw(reftype);
use Koha::Exceptions;
@ -143,6 +144,45 @@ is raised.
return $params;
}
);
=head3 stash_embed
$c->stash_embed( $c->match->endpoint->pattern->defaults->{'openapi.op_spec'} );
=cut
$app->helper(
'stash_embed' => sub {
my ( $c, $args ) = @_;
my $spec = $args->{spec} // {};
my $embed_spec = $spec->{'x-koha-embed'};
my $embed_header = $c->req->headers->header('x-koha-embed');
Koha::Exceptions::BadParameter->throw("Embedding objects is not allowed on this endpoint.")
if $embed_header and !defined $embed_spec;
if ( $embed_header ) {
my $THE_embed = {};
foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
my $matches = grep {lc $_ eq lc $embed_req} @{ $embed_spec };
Koha::Exceptions::BadParameter->throw(
error => 'Embeding '.$embed_req. ' is not authorised. Check your x-koha-embed headers or remove it.'
) unless $matches;
_merge_embed( _parse_embed($embed_req), $THE_embed);
}
$c->stash( 'koha.embed' => $THE_embed )
if $THE_embed;
}
return $c;
}
);
}
=head2 Internal methods
@ -198,4 +238,52 @@ sub _build_order_atom {
}
}
=head3 _parse_embed
my $embed = _parse_embed( $string );
Parses I<$string> and outputs data valid for passing to the Kohaa::Object(s)->to_api
method.
=cut
sub _parse_embed {
my $string = shift;
my $result;
my ( $curr, $next ) = split /\s*\.\s*/, $string, 2;
if ( $next ) {
$result->{$curr} = { children => _parse_embed( $next ) };
}
else {
$result->{$curr} = {};
}
return $result;
}
=head3 _merge_embed
_merge_embed( $parsed_embed, $global_embed );
Merges the hash referenced by I<$parsed_embed> into I<$global_embed>.
=cut
sub _merge_embed {
my ( $structure, $embed ) = @_;
my ($root) = keys %{ $structure };
if ( any { $root eq $_ } keys %{ $embed } ) {
# Recurse
_merge_embed( $structure->{$root}, $embed->{$root} );
}
else {
# Embed
$embed->{$root} = $structure->{$root};
}
}
1;

View File

@ -125,9 +125,64 @@ get '/build_query' => sub {
};
};
get '/stash_embed' => sub {
my $c = shift;
try {
$c->stash_embed(
{
spec => {
'x-koha-embed' => [
'checkouts',
'checkouts.item',
'library'
]
}
}
);
$c->render(
status => 200,
json => $c->stash( 'koha.embed' )
);
}
catch {
$c->render(
status => 400,
json => { error => "$_" }
);
};
};
get '/stash_embed_no_spec' => sub {
my $c = shift;
try {
$c->stash_embed({ spec => {} });
$c->render(
status => 200,
json => $c->stash( 'koha.embed' )
);
}
catch {
$c->render(
status => 400,
json => { error => "$_" }
);
};
};
sub to_model {
my ($args) = @_;
$args->{three} = delete $args->{tres}
if exists $args->{tres};
return $args;
}
# The tests
use Test::More tests => 3;
use Test::More tests => 4;
use Test::Mojo;
subtest 'extract_reserved_params() tests' => sub {
@ -228,3 +283,35 @@ subtest '_build_query_params_from_api' => sub {
->json_is( '/exception_type' => 'Koha::Exceptions::WrongParameter' );
};
subtest 'stash_embed() tests' => sub {
plan tests => 12;
my $t = Test::Mojo->new;
$t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item' } )
->status_is(200)
->json_is( { checkouts => { children => { item => {} } } } );
$t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,library' } )
->status_is(200)
->json_is( { checkouts => { children => { item => {} } }, library => {} } );
$t->get_ok( '/stash_embed' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
->status_is(400)
->json_is(
{
error => 'Embeding patron is not authorised. Check your x-koha-embed headers or remove it.'
}
);
$t->get_ok( '/stash_embed_no_spec' => { 'x-koha-embed' => 'checkouts,checkouts.item,patron' } )
->status_is(400)
->json_is(
{
error => 'Embedding objects is not allowed on this endpoint.'
}
);
};