Bug 18137: Make /cities Mojolicious::Plugin::OpenAPI compatible
authorLari Taskula <lari.taskula@jns.fi>
Mon, 20 Feb 2017 17:58:28 +0000 (19:58 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 21 Sep 2017 14:27:05 +0000 (11:27 -0300)
Also:
- adding some missing and new response definitions into Swagger spec.

To test:
1. prove t/db_dependent/api/v1/cities.t

Signed-off-by: Olli-Antti Kivilahti <olli-antti.kivilahti@jns.fi>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Koha/REST/V1/Cities.pm
api/v1/swagger/paths/cities.json
t/db_dependent/api/v1/cities.t

index 5d32ea9..0feb36a 100644 (file)
@@ -25,11 +25,11 @@ use Koha::Cities;
 use Try::Tiny;
 
 sub list {
-    my ( $c, $args, $cb ) = @_;
+    my $c = shift->openapi->valid_input or return;
 
     my $cities;
     my $filter;
-    $args //= {};
+    my $args = $c->req->params->to_hash;
 
     for my $filter_param ( keys %$args ) {
         $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
@@ -37,96 +37,105 @@ sub list {
 
     return try {
         $cities = Koha::Cities->search($filter);
-        return $c->$cb( $cities, 200 );
+        return $c->render( status => 200, openapi => $cities );
     }
     catch {
         if ( $_->isa('DBIx::Class::Exception') ) {
-            return $c->$cb( { error => $_->{msg} }, 500 );
+            return $c->render( status  => 500,
+                               openapi => { error => $_->msg } );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render( status => 500,
+                openapi => { error => "Something went wrong, check the logs."} );
         }
     };
+
 }
 
 sub get {
-    my ( $c, $args, $cb ) = @_;
+    my $c = shift->openapi->valid_input or return;
 
-    my $city = Koha::Cities->find( $args->{cityid} );
+    my $city = Koha::Cities->find( $c->validation->param('cityid') );
     unless ($city) {
-        return $c->$cb( { error => "City not found" }, 404 );
+        return $c->render( status  => 404,
+                           openapi => { error => "City not found" } );
     }
 
-    return $c->$cb( $city, 200 );
+    return $c->render( status => 200, openapi => $city );
 }
 
 sub add {
-    my ( $c, $args, $cb ) = @_;
+    my $c = shift->openapi->valid_input or return;
 
-    my $city = Koha::City->new( $args->{body} );
+    my $city = Koha::City->new( $c->validation->param('body') );
 
     return try {
         $city->store;
-        return $c->$cb( $city, 200 );
+        return $c->render( status => 200, openapi => $city );
     }
     catch {
         if ( $_->isa('DBIx::Class::Exception') ) {
-            return $c->$cb( { error => $_->msg }, 500 );
+            return $c->render( status  => 500,
+                               openapi => { error => $_->message } );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render( status => 500,
+                openapi => { error => "Something went wrong, check the logs."} );
         }
     };
 }
 
 sub update {
-    my ( $c, $args, $cb ) = @_;
+    my $c = shift->openapi->valid_input or return;
 
     my $city;
 
     return try {
-        $city = Koha::Cities->find( $args->{cityid} );
-        $city->set( $args->{body} );
+        $city = Koha::Cities->find( $c->validation->param('cityid') );
+        my $params = $c->req->json;
+        $city->set( $params );
         $city->store();
-        return $c->$cb( $city, 200 );
+        return $c->render( status => 200, openapi => $city );
     }
     catch {
         if ( not defined $city ) {
-            return $c->$cb( { error => "Object not found" }, 404 );
+            return $c->render( status  => 404,
+                               openapi => { error => "Object not found" } );
         }
         elsif ( $_->isa('Koha::Exceptions::Object') ) {
-            return $c->$cb( { error => $_->message }, 500 );
+            return $c->render( status  => 500,
+                               openapi => { error => $_->message } );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render( status => 500,
+                openapi => { error => "Something went wrong, check the logs."} );
         }
     };
 
 }
 
 sub delete {
-    my ( $c, $args, $cb ) = @_;
+    my $c = shift->openapi->valid_input or return;
 
     my $city;
 
     return try {
-        $city = Koha::Cities->find( $args->{cityid} );
+        $city = Koha::Cities->find( $c->validation->param('cityid') );
         $city->delete;
-        return $c->$cb( "", 200 );
+        return $c->render( status => 200, openapi => "" );
     }
     catch {
         if ( not defined $city ) {
-            return $c->$cb( { error => "Object not found" }, 404 );
+            return $c->render( status  => 404,
+                               openapi => { error => "Object not found" } );
         }
         elsif ( $_->isa('DBIx::Class::Exception') ) {
-            return $c->$cb( { error => $_->msg }, 500 );
+            return $c->render( status  => 500,
+                               openapi => { error => $_->msg } );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render( status => 500,
+                openapi => { error => "Something went wrong, check the logs."} );
         }
     };
 
index 650d224..8b739d6 100644 (file)
@@ -1,7 +1,7 @@
 {
   "/cities": {
     "get": {
-      "x-mojo-controller": "Koha::REST::V1::Cities",
+      "x-mojo-to": "Cities#list",
       "operationId": "list",
       "tags": ["cities"],
       "produces": [
           "schema": {
             "$ref": "../definitions.json#/error"
           }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
         }
       }
     },
     "post": {
-      "x-mojo-controller": "Koha::REST::V1::Cities",
+      "x-mojo-to": "Cities#add",
       "operationId": "add",
       "tags": ["cities"],
       "parameters": [{
             "$ref": "../definitions.json#/city"
           }
         },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
         "403": {
           "description": "Access forbidden",
           "schema": {
           "schema": {
             "$ref": "../definitions.json#/error"
           }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
         }
       },
       "x-koha-authorization": {
   },
   "/cities/{cityid}": {
     "get": {
-      "x-mojo-controller": "Koha::REST::V1::Cities",
+      "x-mojo-to": "Cities#get",
       "operationId": "get",
       "tags": ["cities"],
       "parameters": [{
             "$ref": "../definitions.json#/city"
           }
         },
-        "403": {
-          "description": "Access forbidden",
-          "schema": {
-            "$ref": "../definitions.json#/error"
-          }
-        },
         "404": {
           "description": "City not found",
           "schema": {
           "schema": {
             "$ref": "../definitions.json#/error"
           }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
         }
       }
     },
     "put": {
-      "x-mojo-controller": "Koha::REST::V1::Cities",
+      "x-mojo-to": "Cities#update",
       "operationId": "update",
       "tags": ["cities"],
       "parameters": [{
             "$ref": "../definitions.json#/city"
           }
         },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
         "403": {
           "description": "Access forbidden",
           "schema": {
           "schema": {
             "$ref": "../definitions.json#/error"
           }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
         }
       },
       "x-koha-authorization": {
       }
     },
     "delete": {
-      "x-mojo-controller": "Koha::REST::V1::Cities",
+      "x-mojo-to": "Cities#delete",
       "operationId": "delete",
       "tags": ["cities"],
       "parameters": [{
             "type": "string"
           }
         },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
         "403": {
           "description": "Access forbidden",
           "schema": {
           "schema": {
             "$ref": "../definitions.json#/error"
           }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
         }
       },
       "x-koha-authorization": {
index bf2a8f0..aeace2f 100644 (file)
@@ -332,7 +332,7 @@ subtest 'delete() tests' => sub {
     $tx->req->cookies(
         { name => 'CGISESSID', value => $authorized_session_id } );
     $tx->req->env( { REMOTE_ADDR => $remote_address } );
-    $t->request_ok($tx)->status_is(200)->content_is('');
+    $t->request_ok($tx)->status_is(200)->content_is('""');
 
     $tx = $t->ua->build_tx( DELETE => "/api/v1/cities/$city_id" );
     $tx->req->cookies(