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>.
20 use Mojo::Base 'Mojolicious::Plugin';
22 use Koha::Exceptions::Plugin;
26 use Clone qw( clone );
27 use JSON::Validator::Schema::OpenAPIv2;
28 use Try::Tiny qw( catch try );
32 Koha::REST::Plugin::PluginRoutes
43 my ( $self, $app, $config ) = @_;
45 my $spec = $config->{spec};
46 my $validate = $config->{validate};
50 if ( C4::Context->config("enable_plugins") )
52 $self->{'swagger-v2-schema'} = $app->home->rel_file("api/swagger-v2-schema.json");
54 # plugin needs to define a namespace
55 @plugins = Koha::Plugins->new()->GetPlugins(
57 method => 'api_namespace',
61 foreach my $plugin ( @plugins ) {
62 $spec = $self->inject_routes( $spec, $plugin, $validate );
75 my ( $self, $spec, $plugin, $validate ) = @_;
77 return merge_spec( $spec, $plugin ) unless $validate;
81 my $backup_spec = merge_spec( clone($spec), $plugin );
82 if ( $self->spec_ok( $backup_spec, $validate ) ) {
83 $spec = merge_spec( $spec, $plugin );
86 Koha::Exceptions::Plugin->throw(
87 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
95 my $class = ref $plugin;
96 my $logger = Koha::Logger->get({ interface => 'api' });
97 $logger->error("Plugin $class route injection failed: $error");
107 my ( $spec, $plugin ) = @_;
109 if($plugin->can('api_routes')) {
110 my $plugin_spec = $plugin->api_routes;
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"
121 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
125 if($plugin->can('static_routes')) {
126 my $plugin_spec = $plugin->static_routes;
128 foreach my $route ( keys %{ $plugin_spec } ) {
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"
138 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
149 my ( $self, $spec ) = @_;
151 my $schema = JSON::Validator::Schema::OpenAPIv2->new( $spec );
153 if ($schema->is_invalid) {
154 warn $schema->errors->[0];
157 return !$schema->is_invalid;