Bug 30194: Adapt Koha to the latest JSON::Validator version
[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         $self->{'swagger-v2-schema'} = $app->home->rel_file("api/swagger-v2-schema.json");
53
54         # plugin needs to define a namespace
55         @plugins = Koha::Plugins->new()->GetPlugins(
56             {
57                 method => 'api_namespace',
58             }
59         );
60
61         foreach my $plugin ( @plugins ) {
62             $spec = $self->inject_routes( $spec, $plugin, $validate );
63         }
64
65     }
66
67     return $spec;
68 }
69
70 =head3 inject_routes
71
72 =cut
73
74 sub inject_routes {
75     my ( $self, $spec, $plugin, $validate ) = @_;
76
77     return merge_spec( $spec, $plugin ) unless $validate;
78
79     return try {
80
81         my $backup_spec = merge_spec( clone($spec), $plugin );
82         if ( $self->spec_ok( $backup_spec, $validate ) ) {
83             $spec = merge_spec( $spec, $plugin );
84         }
85         else {
86             Koha::Exceptions::Plugin->throw(
87                 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
88             );
89         }
90
91         return $spec;
92     }
93     catch {
94         my $error = $_;
95         my $class = ref $plugin;
96         my $logger = Koha::Logger->get({ interface => 'api' });
97         $logger->error("Plugin $class route injection failed: $error");
98         return $spec;
99     };
100 }
101
102 =head3 merge_spec
103
104 =cut
105
106 sub merge_spec {
107     my ( $spec, $plugin ) = @_;
108
109     if($plugin->can('api_routes')) {
110         my $plugin_spec = $plugin->api_routes;
111
112         foreach my $route ( keys %{ $plugin_spec } ) {
113             my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
114             if ( exists $spec->{ $THE_route } ) {
115                 # Route exists, overwriting is forbidden
116                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
117                     "Attempted to overwrite $THE_route"
118                 );
119             }
120
121             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
122         }
123     }
124
125     if($plugin->can('static_routes')) {
126         my $plugin_spec = $plugin->static_routes;
127
128         foreach my $route ( keys %{ $plugin_spec } ) {
129
130             my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
131             if ( exists $spec->{ $THE_route } ) {
132                 # Route exists, overwriting is forbidden
133                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
134                     "Attempted to overwrite $THE_route"
135                 );
136             }
137
138             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
139         }
140     }
141     return $spec;
142 }
143
144 =head3 spec_ok
145
146 =cut
147
148 sub spec_ok {
149     my ( $self, $spec ) = @_;
150
151     my $schema = JSON::Validator::Schema::OpenAPIv2->new( $spec );
152
153     if ($schema->is_invalid) {
154         warn $schema->errors->[0];
155     }
156
157     return !$schema->is_invalid;
158 }
159
160 1;