Bug 30194: (follow-up) Remove reference to swagger-v2-schema
[koha.git] / Koha / REST / Plugin / PluginRoutes.pm
1 package Koha::REST::Plugin::PluginRoutes;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Plugin';
21
22 use Koha::Exceptions::Plugin;
23 use Koha::Plugins;
24 use Koha::Logger;
25
26 use Clone qw( clone );
27 use JSON::Validator::Schema::OpenAPIv2;
28 use Try::Tiny qw( catch try );
29
30 =head1 NAME
31
32 Koha::REST::Plugin::PluginRoutes
33
34 =head1 API
35
36 =head2 Helper methods
37
38 =head3 register
39
40 =cut
41
42 sub register {
43     my ( $self, $app, $config ) = @_;
44
45     my $spec     = $config->{spec};
46     my $validate = $config->{validate};
47
48     my @plugins;
49
50     if ( C4::Context->config("enable_plugins") )
51     {
52         # plugin needs to define a namespace
53         @plugins = Koha::Plugins->new()->GetPlugins(
54             {
55                 method => 'api_namespace',
56             }
57         );
58
59         foreach my $plugin ( @plugins ) {
60             $spec = $self->inject_routes( $spec, $plugin, $validate );
61         }
62
63     }
64
65     return $spec;
66 }
67
68 =head3 inject_routes
69
70 =cut
71
72 sub inject_routes {
73     my ( $self, $spec, $plugin, $validate ) = @_;
74
75     return merge_spec( $spec, $plugin ) unless $validate;
76
77     return try {
78
79         my $backup_spec = merge_spec( clone($spec), $plugin );
80         if ( $self->spec_ok( $backup_spec ) ) {
81             $spec = merge_spec( $spec, $plugin );
82         }
83         else {
84             Koha::Exceptions::Plugin->throw(
85                 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
86             );
87         }
88
89         return $spec;
90     }
91     catch {
92         my $error = $_;
93         my $class = ref $plugin;
94         my $logger = Koha::Logger->get({ interface => 'api' });
95         $logger->error("Plugin $class route injection failed: $error");
96         return $spec;
97     };
98 }
99
100 =head3 merge_spec
101
102 =cut
103
104 sub merge_spec {
105     my ( $spec, $plugin ) = @_;
106
107     if($plugin->can('api_routes')) {
108         my $plugin_spec = $plugin->api_routes;
109
110         foreach my $route ( keys %{ $plugin_spec } ) {
111             my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
112             if ( exists $spec->{ $THE_route } ) {
113                 # Route exists, overwriting is forbidden
114                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
115                     "Attempted to overwrite $THE_route"
116                 );
117             }
118
119             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
120         }
121     }
122
123     if($plugin->can('static_routes')) {
124         my $plugin_spec = $plugin->static_routes;
125
126         foreach my $route ( keys %{ $plugin_spec } ) {
127
128             my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
129             if ( exists $spec->{ $THE_route } ) {
130                 # Route exists, overwriting is forbidden
131                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
132                     "Attempted to overwrite $THE_route"
133                 );
134             }
135
136             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
137         }
138     }
139     return $spec;
140 }
141
142 =head3 spec_ok
143
144 =cut
145
146 sub spec_ok {
147     my ( $self, $spec ) = @_;
148
149     my $schema = JSON::Validator::Schema::OpenAPIv2->new( $spec );
150
151     if ($schema->is_invalid) {
152         warn $schema->errors->[0];
153     }
154
155     return !$schema->is_invalid;
156 }
157
158 1;