Bug 24700: Avoid validating REST API spec multiple times
[koha.git] / Koha / REST / Plugin / PluginRoutes.pm
blob13e267be5601d1a4be8415a8d225c135505e03ef
1 package Koha::REST::Plugin::PluginRoutes;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Plugin';
22 use Koha::Exceptions::Plugin;
23 use Koha::Plugins;
25 use Clone qw(clone);
26 use Try::Tiny;
28 =head1 NAME
30 Koha::REST::Plugin::PluginRoutes
32 =head1 API
34 =head2 Helper methods
36 =head3 register
38 =cut
40 sub register {
41 my ( $self, $app, $config ) = @_;
43 my $spec = $config->{spec};
44 my $validator = $config->{validator};
46 my @plugins;
48 if ( C4::Context->config("enable_plugins") )
50 # plugin needs to define a namespace
51 @plugins = Koha::Plugins->new()->GetPlugins(
53 method => 'api_namespace',
58 foreach my $plugin ( @plugins ) {
59 $spec = inject_routes( $spec, $plugin, $validator );
62 return $spec;
65 =head3 inject_routes
67 =cut
69 sub inject_routes {
70 my ( $spec, $plugin, $validator ) = @_;
72 return merge_spec( $spec, $plugin ) unless $validator;
74 return try {
76 my $backup_spec = merge_spec( clone($spec), $plugin );
77 if ( spec_ok( $backup_spec, $validator ) ) {
78 $spec = merge_spec( $spec, $plugin );
80 else {
81 Koha::Exceptions::Plugin->throw(
82 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
86 return $spec;
88 catch {
89 warn "$_";
90 return $spec;
94 =head3 merge_spec
96 =cut
98 sub merge_spec {
99 my ( $spec, $plugin ) = @_;
101 if($plugin->can('api_routes')) {
102 my $plugin_spec = $plugin->api_routes;
104 foreach my $route ( keys %{ $plugin_spec } ) {
105 my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
106 if ( exists $spec->{ $THE_route } ) {
107 # Route exists, overwriting is forbidden
108 Koha::Exceptions::Plugin::ForbiddenAction->throw(
109 "Attempted to overwrite $THE_route"
113 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
117 if($plugin->can('static_routes')) {
118 my $plugin_spec = $plugin->static_routes;
120 foreach my $route ( keys %{ $plugin_spec } ) {
122 my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
123 if ( exists $spec->{ $THE_route } ) {
124 # Route exists, overwriting is forbidden
125 Koha::Exceptions::Plugin::ForbiddenAction->throw(
126 "Attempted to overwrite $THE_route"
130 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
133 return $spec;
136 =head3 spec_ok
138 =cut
140 sub spec_ok {
141 my ( $spec, $validator ) = @_;
143 return try {
144 $validator->load_and_validate_schema(
145 $spec,
147 allow_invalid_ref => 1,
150 return 1;
152 catch {
153 return 0;