From fe7cf590c2ebe3d355606f0781ca41edc773b8e9 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 17 Jul 2025 11:34:14 -0300 Subject: [PATCH] Bug 40424: Simplify existing code - preparation I wanted to isolate the changes for the query-in-body handling and it made sense to refactor this dupicated code at the same time. So I add this patch for tidying the area in preparation for the other part. Duplicated code gets simplified. To test: 1. Apply the unit tests patch 2. Run: $ ktd --shell k$ prove t/Koha/REST/Plugin/Query.t \ t/db_dependent/api/v1/*.t \ t/db_dependent/Koha/REST/Plugin/ => SUCCESS: Tests pass! 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! 5. Sign off :-D --- Koha/REST/Plugin/Objects.pm | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 68d4c036243..753f3b8e6e5 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -214,7 +214,10 @@ controller, and thus shouldn't be called twice in it. ); my $query_params; - if ( defined $reserved_params->{q} + + my $q_param = $reserved_params->{q}; + + if ( $q_param || defined $reserved_params->{query} ) { @@ -231,23 +234,12 @@ controller, and thus shouldn't be called twice in it. 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} } ) { - if ($q) { # skip if exists but is empty - foreach my $qf ( @{$query_fixers} ) { - $q = $qf->($q); - } - push @query_params_array, $json->decode($q); - } - } - } else { + # The q parameter can be an array if multiple passed + $q_param = [$q_param] + unless ref($q_param) eq 'ARRAY'; - # objects.search called outside OpenAPI context - # might be a hashref - if ( $reserved_params->{q} ) { - my $q = $reserved_params->{q}; + foreach my $q ( @{$q_param} ) { + if ($q) { # skip if exists but is empty foreach my $qf ( @{$query_fixers} ) { $q = $qf->($q); } -- 2.50.1