From df3316eaad75efbce137707d34ee29213dd25907 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 24 Mar 2022 09:52:04 -0300 Subject: [PATCH] [OPTION 2] Bug 30165: (follow-up) Allow objects.search usage outside OpenAPI Our objects.search helper is written for being used in our API controllers, which get their query parameters processed by the OpenAPI plugin, using JSON::Validator. Particularly, the 'q' parameter is defined as 'multi' on our spec, which means objects.search always gets it as an arrayref. As the Objects.t tests are not using the OpenAPI plugin, a hashref is generated as only one q= is being passed. This patch adds an extra validation on objects.search, for the non-arrayref use case and does the right thing. To test: 1. Run: $ kshell k$ prove t/db_dependent/Koha/REST/Plugin/Objects.t => FAIL: Tests fail! 2. Apply this patch 3. Repeat 1 => SUCCESS: Tests pass! 4. Sign off :-D Signed-off-by: Tomas Cohen Arazi --- Koha/REST/Plugin/Objects.pm | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 6a94dac11c..86c1d297e0 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -152,10 +152,18 @@ shouldn't be called twice in it. my $json = JSON->new; - # 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 ( 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 + } + } + else { + # objects.search called outside OpenAPI context + # might be a hashref + push @query_params_array, $json->decode($reserved_params->{q}) + if $reserved_params->{q}; } push @query_params_array, -- 2.32.0