Bug 40424: Make query-in-body rely on `$c->req->json`

This patch removes the reliance on `$c->validation->output` for the
query-in-body feature. This will allow bug 40417 to move forward.

It also fixes some wrong design decisions we made 5-6 years ago. This
was discussed with the Mojolicious::Plugin::OpenAPI maintainer and the
agreement was `$c->validation->output` was not stable nor intended to be
accessed like we do.

To test:
1. 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!
2. Apply this patchset
3. Repeat 1
=> SUCCESS: Tests pass!
4. Sign off :-D

Signed-off-by: Matt Blenkinsop <matt.blenkinsop@openfifth.co.uk>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 17b851bff0)
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
This commit is contained in:
Tomas Cohen Arazi 2025-07-17 11:54:00 -03:00 committed by Paul Derscheid
parent d81809b4ba
commit 274bf141ab
No known key found for this signature in database
GPG Key ID: C25F9FD44CED595B
2 changed files with 7 additions and 13 deletions

View File

@ -216,28 +216,22 @@ controller, and thus shouldn't be called twice in it.
my $query_params;
my $q_param = $reserved_params->{q};
my $q_body = $c->req->json;
if ( $q_param
|| defined $reserved_params->{query} )
{
if ( $q_param || $q_body ) {
my @query_params_array;
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);
}
# The q parameter can be an array if multiple passed
$q_param = [$q_param]
unless ref($q_param) eq 'ARRAY';
# Encode the already decoded request body and add it for processing
push @{$q_param}, $json->encode($q_body)
if $q_body;
foreach my $q ( @{$q_param} ) {
if ($q) { # skip if exists but is empty
foreach my $qf ( @{$query_fixers} ) {

View File

@ -348,7 +348,7 @@ This helper method parses the 'x-koha-request-id' header and stashes the value.
sub _reserved_words {
my @reserved_words = qw( _match _order_by _order_by[] _page _per_page q query);
my @reserved_words = qw( _match _order_by _order_by[] _page _per_page q );
return \@reserved_words;
}