From dad5c416f34aa6a62d5993f57ea1a2f83aeed4e5 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Thu, 30 Oct 2025 10:26:38 +0000 Subject: [PATCH] Bug 17387: item cannot be restored if associated biblio does not exist --- Koha/REST/V1/DeletedItems.pm | 13 ++++++++++++- api/v1/swagger/paths/deleted_items.yaml | 4 ++++ t/db_dependent/api/v1/deleted_items.t | 13 ++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Koha/REST/V1/DeletedItems.pm b/Koha/REST/V1/DeletedItems.pm index d3fa1a21df1..fa3522477c1 100644 --- a/Koha/REST/V1/DeletedItems.pm +++ b/Koha/REST/V1/DeletedItems.pm @@ -19,9 +19,11 @@ use Modern::Perl; use Mojo::Base 'Mojolicious::Controller'; +use Koha::Exceptions; use Koha::Old::Items; -use Try::Tiny qw( catch try ); +use Scalar::Util qw( blessed ); +use Try::Tiny qw( catch try ); =head1 API @@ -90,6 +92,15 @@ sub restore { openapi => $c->objects->to_api($item), ); } catch { + if ( blessed $_ && $_->isa('Koha::Exceptions::ObjectNotFound') ) { + return $c->render( + status => 409, + openapi => { + error => + "Bibliographic record not found for this item. Cannot restore item without its bibliographic record." + } + ); + } $c->unhandled_exception($_); }; } diff --git a/api/v1/swagger/paths/deleted_items.yaml b/api/v1/swagger/paths/deleted_items.yaml index 923ce641ccc..31a4d3f9e01 100644 --- a/api/v1/swagger/paths/deleted_items.yaml +++ b/api/v1/swagger/paths/deleted_items.yaml @@ -129,6 +129,10 @@ description: Deleted item not found schema: $ref: "../swagger.yaml#/definitions/error" + "409": + description: Cannot restore item - bibliographic record not found + schema: + $ref: "../swagger.yaml#/definitions/error" "500": description: | Internal server error. Possible `error_code` attribute values: diff --git a/t/db_dependent/api/v1/deleted_items.t b/t/db_dependent/api/v1/deleted_items.t index 464e1a18b78..4b2ca64bcb3 100755 --- a/t/db_dependent/api/v1/deleted_items.t +++ b/t/db_dependent/api/v1/deleted_items.t @@ -127,7 +127,7 @@ subtest 'get() tests' => sub { subtest 'restore() tests' => sub { - plan tests => 11; + plan tests => 15; $schema->storage->txn_begin; @@ -183,5 +183,16 @@ subtest 'restore() tests' => sub { $t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(404); + my $item_without_biblio = $builder->build_sample_item( { barcode => 'TEST_NO_BIBLIO' } ); + my $orphan_item_id = $item_without_biblio->itemnumber; + my $orphan_biblio_id = $item_without_biblio->biblionumber; + my $orphan_item_data = $item_without_biblio->unblessed; + my $deleted_orphan_item = Koha::Old::Item->new($orphan_item_data)->store; + $item_without_biblio->delete; + Koha::Biblios->find($orphan_biblio_id)->delete; + + $t->put_ok("//$userid:$password@/api/v1/deleted/items/$orphan_item_id")->status_is(409)->json_has('/error') + ->json_like( '/error', qr/Bibliographic record not found/ ); + $schema->storage->txn_rollback; }; -- 2.39.5