From 83a1fbac24f6f870a25dd560181e85917b17fa52 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 16 Jun 2023 12:50:07 -0300 Subject: [PATCH] Bug 33974: Make biblioitem columns searchable in a generic way This patch makes biblioitem attributes be searchable on the biblios endpoint. It does so by using the new method in Koha::Biblios, and by adjusting objects.search(_rs) to accept a $query_fixer arrayref of functions to be applied to each query or order_by parameters. The result is cleaner code to write, but complex internals. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/api/v1/biblios.t => SUCCESS: Tests pass! Searching for biblioitem attributes works on the API! 3. Sign off :-D Signed-off-by: Sam Lau Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 353f328ad3a99cc5a9ee913cd1f72a1d841705f2) Signed-off-by: Martin Renvoize (cherry picked from commit 21ebede0de667f2e17d6935920c8b9cebd16b880) Signed-off-by: Matt Blenkinsop --- Koha/REST/Plugin/Objects.pm | 58 +++++++++++++++++++++++++-------- Koha/REST/V1/Biblios.pm | 14 +++++--- t/db_dependent/api/v1/biblios.t | 15 ++++++++- 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 94baf03753..2bf87bccae 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -112,10 +112,10 @@ shouldn't be called twice in it. $app->helper( 'objects.search' => sub { - my ( $c, $result_set ) = @_; + my ( $c, $result_set, $query_fixers ) = @_; # Generate the resultset using the HTTP request information - my $objects_rs = $c->objects->search_rs($result_set); + my $objects_rs = $c->objects->search_rs( $result_set, $query_fixers ); # Add pagination headers $c->add_pagination_headers(); @@ -127,13 +127,21 @@ shouldn't be called twice in it. =head3 objects.search_rs my $patrons_object = Koha::Patrons->new; - my $patrons_rs = $c->objects->search_rs( $patrons_object ); + my $patrons_rs = $c->objects->search_rs( $patrons_object [, $query_fixers ] ); . . . return $c->render({ status => 200, openapi => $patrons_rs->to_api }); Returns the passed Koha::Objects resultset filtered as requested by the api query parameters and with requested embeds applied. +The optional I<$query_fixers> parameter is expected to be a reference to a list of +function references. This functions need to accept two parameters: ( $query, $no_quotes ). + +The I<$query> is a string to be adapted. A modified version will be returned. The +I<$no_quotes> parameter controls if quotes need to be added for matching inside the string. +Quoting should be used by default, for replacing JSON keys e.g "biblio.isbn" would match +and biblio.isbn wouldn't. + Warning: this helper stashes base values for the pagination headers to the calling controller, and thus shouldn't be called twice in it. @@ -141,15 +149,26 @@ controller, and thus shouldn't be called twice in it. $app->helper( 'objects.search_rs' => sub { - my ( $c, $result_set ) = @_; + my ( $c, $result_set, $query_fixers ) = @_; my $args = $c->validation->output; my $attributes = {}; + $query_fixers //= []; + # Extract reserved params my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args); + if ( exists $reserved_params->{_order_by} ) { + # _order_by passed, fix if required + for my $p ( @{$reserved_params->{_order_by}} ) { + foreach my $qf ( @{$query_fixers} ) { + $p = $qf->($p, 1); # 1 => no quotes on matching + } + } + } + # Merge sorting into query attributes $c->dbic_merge_sorting( { @@ -204,26 +223,39 @@ controller, and thus shouldn't be called twice in it. my @query_params_array; - # query in request body, JSON::Validator already decoded it - push @query_params_array, $reserved_params->{query} - if defined $reserved_params->{query}; - my $json = JSON->new; + # query in request body, JSON::Validator already decoded it + if ( $reserved_params->{query} ) { + my $query = $json->encode( $reserved_params->{query} ); + foreach my $qf ( @{$query_fixers} ) { + $query = $qf->($query); + } + push @query_params_array, $json->decode($query); + } + if ( ref( $reserved_params->{q} ) eq 'ARRAY' ) { # q is defined as multi => JSON::Validator generates an array foreach my $q ( @{ $reserved_params->{q} } ) { - push @query_params_array, $json->decode($q) - if $q; # skip if exists but is empty + if ( $q ) { # skip if exists but is empty + foreach my $qf (@{$query_fixers}) { + $q = $qf->($q); + } + push @query_params_array, $json->decode($q); + } } } else { # objects.search called outside OpenAPI context # might be a hashref - push @query_params_array, - $json->decode( $reserved_params->{q} ) - if $reserved_params->{q}; + if ( $reserved_params->{q} ) { + my $q = $reserved_params->{q}; + foreach my $qf (@{$query_fixers}) { + $q = $qf->($q); + } + push @query_params_array, $json->decode( $q ); + } } push @query_params_array, diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index 9b4750b65e..d343e5265d 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -25,6 +25,12 @@ use Koha::RecordProcessor; use C4::Biblio qw( DelBiblio AddBiblio ModBiblio ); use C4::Search qw( FindDuplicate ); +use C4::Barcodes::ValueBuilder; +use C4::Context; + +use Koha::Items; + +use Clone qw( clone ); use List::MoreUtils qw( any ); use MARC::Record::MiJ; @@ -622,12 +628,12 @@ Controller function that handles retrieving a single biblio object sub list { my $c = shift->openapi->valid_input or return; - my $attributes; - $attributes = - { prefetch => ['metadata'] } # don't prefetch metadata if not needed + my @prefetch = qw(biblioitem); + push @prefetch, 'metadata' # don't prefetch metadata if not needed unless $c->req->headers->accept =~ m/application\/json/; - my $biblios = $c->objects->search_rs( Koha::Biblios->new ); + my $rs = Koha::Biblios->search( undef, { prefetch => \@prefetch }); + my $biblios = $c->objects->search_rs( $rs, [(sub{ $rs->api_query_fixer( $_[0], '', $_[1] ) })] ); return try { diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t index e3d7fe6eba..ce67cda4be 100755 --- a/t/db_dependent/api/v1/biblios.t +++ b/t/db_dependent/api/v1/biblios.t @@ -36,6 +36,8 @@ use Koha::Database; use Koha::Checkouts; use Koha::Old::Checkouts; +use Mojo::JSON qw(encode_json); + my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -1622,7 +1624,7 @@ subtest 'put() tests' => sub { subtest 'list() tests' => sub { - plan tests => 15; + plan tests => 17; $schema->storage->txn_begin; @@ -1691,5 +1693,16 @@ subtest 'list() tests' => sub { { Accept => 'text/plain', 'x-koha-query' => $search } ) ->status_is(200, 'Status is 200 for text/plain'); + # DELETE any biblio with ISBN = TOMAS + Koha::Biblios->search({ 'biblioitem.isbn' => 'TOMAS' }, { join => [ 'biblioitem' ] }) + ->delete; + + + my $isbn_query = encode_json({ isbn => 'TOMAS' }); + $biblio->biblioitem->set({ isbn => 'TOMAS' })->store; + $t->get_ok( "//$userid:$password@/api/v1/biblios?q=$isbn_query" => + { Accept => 'text/plain' } ) + ->status_is(200); + $schema->storage->txn_rollback; }; -- 2.39.5