From b9f00ab1a10f648cd55b48eec516d261263edef9 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 20 Jan 2025 12:38:32 -0300 Subject: [PATCH] Bug 38929: Make POST endpoints return the `Location` header This patch makes the POST endpoints missing the Location header return it. Some endpoints I skipped: * /patrons/account/credits (doesn't have a GET route to point to) * /patrons/account/debits (doesn't have a GET route to point to) To test: 1. Apply the regression tests 2. Run: $ ktd --shell k$ prove t/db_dependent/api/v1/authorities.t \ t/db_dependent/api/v1/biblios.t \ t/db_dependent/api/v1/clubs_holds.t \ t/db_dependent/api/v1/holds.t \ t/db_dependent/api/v1/ill_batchstatuses.t \ t/db_dependent/api/v1/import_batch_profiles.t \ t/db_dependent/api/v1/transfer_limits.t => FAIL: Tests fail! The endpoints don't return the header! 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! 5. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind Signed-off-by: Matt Blenkinsop --- Koha/REST/V1/Biblios.pm | 6 ++++++ Koha/REST/V1/Clubs/Holds.pm | 2 ++ Koha/REST/V1/Holds.pm | 2 ++ Koha/REST/V1/ILL/Batch/Statuses.pm | 1 + Koha/REST/V1/ImportBatchProfiles.pm | 1 + Koha/REST/V1/TransferLimits.pm | 2 ++ 6 files changed, 14 insertions(+) diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index 794a968af83..4c1221c2ab8 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -370,6 +370,10 @@ sub add_item { $item->store->discard_changes; + my $base_url = $c->req->url->to_string; + $base_url =~ s|/biblios/\d+||; + $c->res->headers->location( $base_url . '/' . $item->id ); + $c->render( status => 201, openapi => $c->objects->to_api($item), @@ -719,6 +723,8 @@ sub add { ); } + $c->res->headers->location( $c->req->url->to_string . '/' . $biblio_id ); + return $c->render( status => 200, openapi => { id => $biblio_id } diff --git a/Koha/REST/V1/Clubs/Holds.pm b/Koha/REST/V1/Clubs/Holds.pm index 851cc9358a2..0643d4b7df5 100644 --- a/Koha/REST/V1/Clubs/Holds.pm +++ b/Koha/REST/V1/Clubs/Holds.pm @@ -108,6 +108,8 @@ sub add { } ); + $c->res->headers->location( $c->req->url->to_string . '/' . $club_hold->id ); + return $c->render( status => 201, openapi => $c->objects->to_api($club_hold), diff --git a/Koha/REST/V1/Holds.pm b/Koha/REST/V1/Holds.pm index 121e68313e4..685a3697c18 100644 --- a/Koha/REST/V1/Holds.pm +++ b/Koha/REST/V1/Holds.pm @@ -216,6 +216,8 @@ sub add { my $hold = Koha::Holds->find($hold_id); + $c->res->headers->location( $c->req->url->to_string . '/' . $hold_id ); + return $c->render( status => 201, openapi => $c->objects->to_api($hold), diff --git a/Koha/REST/V1/ILL/Batch/Statuses.pm b/Koha/REST/V1/ILL/Batch/Statuses.pm index 45612f5f8d8..ac22b0ca1c0 100644 --- a/Koha/REST/V1/ILL/Batch/Statuses.pm +++ b/Koha/REST/V1/ILL/Batch/Statuses.pm @@ -84,6 +84,7 @@ sub add { openapi => $return ); } else { + $c->res->headers->location( $c->req->url->to_string . '/' . $status->code ); return $c->render( status => 201, openapi => $status diff --git a/Koha/REST/V1/ImportBatchProfiles.pm b/Koha/REST/V1/ImportBatchProfiles.pm index c61b2166909..51d43f4f0f1 100644 --- a/Koha/REST/V1/ImportBatchProfiles.pm +++ b/Koha/REST/V1/ImportBatchProfiles.pm @@ -68,6 +68,7 @@ sub add { return try { my $profile = Koha::ImportBatchProfile->new_from_api( $body )->store; + $c->res->headers->location( $c->req->url->to_string . '/' . $profile->id ); return $c->render( status => 201, openapi => $c->objects->to_api($profile), diff --git a/Koha/REST/V1/TransferLimits.pm b/Koha/REST/V1/TransferLimits.pm index 8e8515621f3..50799a887e0 100644 --- a/Koha/REST/V1/TransferLimits.pm +++ b/Koha/REST/V1/TransferLimits.pm @@ -74,6 +74,8 @@ sub add { Koha::Exceptions::TransferLimit::Duplicate->throw(); } + $c->res->headers->location( $c->req->url->to_string . '/' . $transfer_limit->id ); + return $c->render( status => 201, openapi => $c->objects->to_api($transfer_limit), -- 2.48.1