From 0a80ff8c3d6bc8e22949a2b57af9692ea47c915e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 16 Jul 2025 23:31:58 -0300 Subject: [PATCH] Bug 40423: Add helper to stash x-koha-request-id header value This patch aligns handling of the header with how other headers are managed. * A helper is added which reads the header and stashes the value * The helper is called in the same place similar helpers are called (authenticate_api_request) * The `objects.search_rs` helper is adjusted to use the stashed value instead of picking it from the reserved params. To test: 1. Apply this patches 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! No behavior change! 3. Sign off :-D Signed-off-by: Matt Blenkinsop Signed-off-by: Martin Renvoize --- Koha/REST/Plugin/Objects.pm | 4 ++-- Koha/REST/Plugin/Query.pm | 32 ++++++++++++++++++++++++++++---- Koha/REST/V1/Auth.pm | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 486509abe71..68d4c036243 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -263,8 +263,8 @@ controller, and thus shouldn't be called twice in it. } # request sequence id (i.e. 'draw' Datatables parameter) - $c->res->headers->add( 'x-koha-request-id' => $reserved_params->{'x-koha-request-id'} ) - if $reserved_params->{'x-koha-request-id'}; + $c->res->headers->add( 'x-koha-request-id' => $c->stash('koha.request_id') ) + if $c->stash('koha.request_id'); # If search_limited exists, use it $result_set = $result_set->search_limited, diff --git a/Koha/REST/Plugin/Query.pm b/Koha/REST/Plugin/Query.pm index 99a7906a699..93806ecf0b0 100644 --- a/Koha/REST/Plugin/Query.pm +++ b/Koha/REST/Plugin/Query.pm @@ -284,12 +284,12 @@ Unwraps and stashes the x-koha-embed headers for use later query construction # Stash the overrides $c->stash_overrides(); - #Use it + # Use it my $overrides = $c->stash('koha.overrides'); if ( $overrides->{pickup_location} ) { ... } -This helper method parses 'x-koha-override' headers and stashes the passed overriders -in the for of a I for easy use in controller methods. +This helper method parses the 'x-koha-override' headers and stashes the passed overrides +in the form of a I for easy use in controller methods. FIXME: With the currently used JSON::Validator version we use, it is not possible to use the validated and coerced data (it doesn't validate array-type headers) so this @@ -309,6 +309,30 @@ reference: https://metacpan.org/changes/distribution/JSON-Validator#L14 $c->stash( 'koha.overrides' => $overrides ); + return $c; + } + ); + +=head3 stash_request_id + + # Stash the request ID + $c->stash_request_id(); + # Use it + my $request_id = $c->stash('koha.request_id'); + +This helper method parses the 'x-koha-request-id' header and stashes the value. + +=cut + + $app->helper( + 'stash_request_id' => sub { + + my ($c) = @_; + + my $request_id = $c->req->headers->header('x-koha-request-id') || q{}; + + $c->stash( 'koha.request_id' => $request_id ); + return $c; } ); @@ -324,7 +348,7 @@ reference: https://metacpan.org/changes/distribution/JSON-Validator#L14 sub _reserved_words { - my @reserved_words = qw( _match _order_by _order_by[] _page _per_page q query x-koha-request-id x-koha-embed); + my @reserved_words = qw( _match _order_by _order_by[] _page _per_page q query); return \@reserved_words; } diff --git a/Koha/REST/V1/Auth.pm b/Koha/REST/V1/Auth.pm index fd22f1be68c..d7deddcb76f 100644 --- a/Koha/REST/V1/Auth.pm +++ b/Koha/REST/V1/Auth.pm @@ -149,6 +149,7 @@ sub authenticate_api_request { $c->stash_embed( { spec => $spec } ); $c->stash_overrides(); + $c->stash_request_id(); my $cookie_auth = 0; -- 2.50.1