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