Bug 32030: ERM - Agreement documents (FIXED)
[koha-ffzg.git] / Koha / REST / V1.pm
index af6bc88..13bbcfc 100644 (file)
@@ -20,8 +20,11 @@ use Modern::Perl;
 use Mojo::Base 'Mojolicious';
 
 use C4::Context;
-use JSON::Validator::OpenAPI::Mojolicious;
-use Try::Tiny;
+use Koha::Logger;
+
+use JSON::Validator::Schema::OpenAPIv2;
+
+use Try::Tiny qw( catch try );
 
 =head1 NAME
 
@@ -40,6 +43,9 @@ Overloaded Mojolicious->startup method. It is called at application startup.
 sub startup {
     my $self = shift;
 
+    my $logger = Koha::Logger->get({ interface => 'api' });
+    $self->log($logger);
+
     $self->hook(
         before_dispatch => sub {
             my $c = shift;
@@ -66,24 +72,23 @@ sub startup {
         $self->secrets([$secret_passphrase]);
     }
 
-    my $validator = JSON::Validator::OpenAPI::Mojolicious->new;
+    my $spec_file = $self->home->rel_file("api/v1/swagger/swagger.yaml");
 
     push @{$self->routes->namespaces}, 'Koha::Plugin';
 
     # Try to load and merge all schemas first and validate the result just once.
-    my $spec;
     try {
-        $spec = $validator->bundle(
-            {
-                replace => 1,
-                schema => $self->home->rel_file("api/v1/swagger/swagger.json")
-            }
-        );
+
+        my $schema = JSON::Validator::Schema::OpenAPIv2->new;
+
+        $schema->resolve( $spec_file );
+
+        my $spec = $schema->bundle->data;
 
         $self->plugin(
             'Koha::REST::Plugin::PluginRoutes' => {
-                spec               => $spec,
-                validator          => undef
+                spec     => $spec,
+                validate => 0,
             }
         ) unless C4::Context->needs_install; # load only if Koha is installed
 
@@ -91,41 +96,44 @@ sub startup {
             OpenAPI => {
                 spec  => $spec,
                 route => $self->routes->under('/api/v1')->to('Auth#under'),
-                allow_invalid_ref =>
-                1,    # required by our spec because $ref directly under
-                        # Paths-, Parameters-, Definitions- & Info-object
-                        # is not allowed by the OpenAPI specification.
             }
         );
+
+        $self->plugin('RenderFile');
     }
     catch {
         # Validation of the complete spec failed. Resort to validation one-by-one
         # to catch bad ones.
-        $validator->load_and_validate_schema(
-            $self->home->rel_file("api/v1/swagger/swagger.json"),
-            {
-                allow_invalid_ref  => 1,
-            }
-        );
 
-        $spec = $validator->schema->data;
-        $self->plugin(
-            'Koha::REST::Plugin::PluginRoutes' => {
-                spec      => $spec,
-                validator => $validator
-            }
-        )  unless C4::Context->needs_install; # load only if Koha is installed
+        # JSON::Validator uses confess, so trim call stack from the message.
+        my $logger = Koha::Logger->get({ interface => 'api' });
+        $logger->error("Warning: Could not load REST API spec bundle: " . $_);
 
-        $self->plugin(
-            OpenAPI => {
-                spec  => $spec,
-                route => $self->routes->under('/api/v1')->to('Auth#under'),
-                allow_invalid_ref =>
-                1,    # required by our spec because $ref directly under
-                        # Paths-, Parameters-, Definitions- & Info-object
-                        # is not allowed by the OpenAPI specification.
-            }
-        );
+        try {
+
+            my $schema = JSON::Validator::Schema::OpenAPIv2->new;
+            $schema->resolve( $spec_file );
+
+            my $spec = $schema->bundle->data;
+
+            $self->plugin(
+                'Koha::REST::Plugin::PluginRoutes' => {
+                    spec     => $spec,
+                    validate => 1
+                }
+            )  unless C4::Context->needs_install; # load only if Koha is installed
+
+            $self->plugin(
+                OpenAPI => {
+                    spec  => $spec,
+                    route => $self->routes->under('/api/v1')->to('Auth#under'),
+                }
+            );
+        }
+        catch {
+            # JSON::Validator uses confess, so trim call stack from the message.
+            $logger->error("Warning: Could not load REST API spec bundle: " . $_);
+        };
     };
 
     $self->plugin( 'Koha::REST::Plugin::Pagination' );