Bug 23653: use local copy of 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
25 use Clone qw(clone);
26 use Try::Tiny;
27
28 =head1 NAME
29
30 Koha::REST::Plugin::PluginRoutes
31
32 =head1 API
33
34 =head2 Helper methods
35
36 =head3 register
37
38 =cut
39
40 sub register {
41     my ( $self, $app, $config ) = @_;
42
43     my $spec      = $config->{spec};
44     my $validator = $config->{validator};
45
46     my @plugins;
47
48     if ( C4::Context->config("enable_plugins") )
49     {
50         my $schema = $app->home->rel_file("api/swagger-v2-schema.json");
51         if ($schema){
52            $self->{'swagger-v2-schema'} = $schema;
53         }
54
55         # plugin needs to define a namespace
56         @plugins = Koha::Plugins->new()->GetPlugins(
57             {
58                 method => 'api_namespace',
59             }
60         );
61
62         foreach my $plugin ( @plugins ) {
63             $spec = $self->inject_routes( $spec, $plugin, $validator );
64         }
65
66     }
67
68     return $spec;
69 }
70
71 =head3 inject_routes
72
73 =cut
74
75 sub inject_routes {
76     my ( $self, $spec, $plugin, $validator ) = @_;
77
78     return merge_spec( $spec, $plugin ) unless $validator;
79
80     return try {
81
82         my $backup_spec = merge_spec( clone($spec), $plugin );
83         if ( $self->spec_ok( $backup_spec, $validator ) ) {
84             $spec = merge_spec( $spec, $plugin );
85         }
86         else {
87             Koha::Exceptions::Plugin->throw(
88                 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
89             );
90         }
91
92         return $spec;
93     }
94     catch {
95         warn "$_";
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, $validator ) = @_;
148
149     my $schema = $self->{'swagger-v2-schema'};
150
151     return try {
152         $validator->load_and_validate_schema(
153             $spec,
154             {
155                 allow_invalid_ref => 1,
156                 schema => ( $schema ) ? $schema : undef,
157             }
158         );
159         return 1;
160     }
161     catch {
162         return 0;
163     }
164 }
165
166 1;