From ea5f664bd3c961e742e56765c9c5dfea19e10af1 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Wed, 30 Apr 2025 15:22:25 +0100 Subject: [PATCH] Bug 38010: Migrate to_api logic to GET endpoint Signed-off-by: Jonathan Druart --- Koha/Acquisition/Bookseller.pm | 28 ------------------- Koha/REST/V1/Acquisitions/Vendors.pm | 13 ++++++++- t/db_dependent/Koha/Acquisition/Booksellers.t | 24 +--------------- 3 files changed, 13 insertions(+), 52 deletions(-) diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm index 3745e506739..b845aba7370 100644 --- a/Koha/Acquisition/Bookseller.pm +++ b/Koha/Acquisition/Bookseller.pm @@ -219,34 +219,6 @@ sub to_api_mapping { }; } -=head3 to_api - - my $json = $av->to_api; - -Overloaded method that returns a JSON representation of the Koha::Acquisition::Bookseller object, -suitable for API output. - -=cut - -sub to_api { - my ( $self, $params ) = @_; - - my $response = $self->SUPER::to_api($params); - my $overrides = {}; - - if ( $self->interfaces ) { - my $interfaces = $self->interfaces->as_list; - my @updated_interfaces; - foreach my $interface ( @{$interfaces} ) { - $interface->password( $interface->plain_text_password ); - push @updated_interfaces, $interface->unblessed; - } - $overrides->{interfaces} = ( \@updated_interfaces ); - } - - return { %$response, %$overrides }; -} - =head2 Internal methods =head3 _type diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index b508ec36625..28cf1e65f82 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -70,9 +70,20 @@ sub get { unless $vendor; return try { + my $vendor_to_return = $c->objects->to_api($vendor); + if ( $vendor_to_return->{interfaces} ) { + my $interfaces = $vendor->interfaces->as_list; + my @updated_interfaces; + foreach my $interface ( @{$interfaces} ) { + $interface->password( $interface->plain_text_password ); + push @updated_interfaces, $interface->unblessed; + } + $vendor_to_return->{interfaces} = \@updated_interfaces; + } + return $c->render( status => 200, - openapi => $c->objects->to_api($vendor), + openapi => $vendor_to_return, ); } catch { $c->unhandled_exception($_); diff --git a/t/db_dependent/Koha/Acquisition/Booksellers.t b/t/db_dependent/Koha/Acquisition/Booksellers.t index 14512113598..464d9d472dc 100755 --- a/t/db_dependent/Koha/Acquisition/Booksellers.t +++ b/t/db_dependent/Koha/Acquisition/Booksellers.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 9; +use Test::More tests => 8; use t::lib::TestBuilder; @@ -310,25 +310,3 @@ subtest 'invoices' => sub { $schema->storage->txn_rollback(); }; - -subtest 'to_api() tests' => sub { - - plan tests => 4; - - $schema->storage->txn_begin; - - my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } ); - - is( $vendor->interfaces->count, 0, 'Vendor has no interfaces' ); - - $vendor->interfaces( - [ { name => 'first interface' }, { name => 'second interface', login => 'one_login', password => 'Test1234' } ] - ); - - my $interfaces = $vendor->to_api->{interfaces}; - is( scalar(@$interfaces), 2, 'Vendor has two interfaces' ); - is( @{$interfaces}[0]->{password}, undef, 'No password set for the interface' ); - is( @{$interfaces}[1]->{password}, 'Test1234', 'password is unhashed' ); - - $schema->storage->txn_rollback; -}; -- 2.34.1