Bug 18137: Make /patrons Mojolicious::Plugin::OpenAPI compatible
authorLari Taskula <lari.taskula@jns.fi>
Fri, 17 Feb 2017 12:59:24 +0000 (14:59 +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.
- fixing failing test due to Bug 17932's change of boolean values

To test:
1. prove t/db_dependent/api/v1/patrons.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/Patron.pm
api/v1/swagger/paths/patrons.json
t/db_dependent/api/v1/patrons.t

index 477980e..7cc7bed 100644 (file)
@@ -22,26 +22,23 @@ use Mojo::Base 'Mojolicious::Controller';
 use Koha::Patrons;
 
 sub list {
-    my ($c, $args, $cb) = @_;
-
-    my $user = $c->stash('koha.user');
+    my $c = shift->openapi->valid_input or return;
 
     my $patrons = Koha::Patrons->search;
 
-    $c->$cb($patrons, 200);
+    return $c->render(status => 200, openapi => $patrons);
 }
 
 sub get {
-    my ($c, $args, $cb) = @_;
-
-    my $user = $c->stash('koha.user');
+    my $c = shift->openapi->valid_input or return;
 
-    my $patron = Koha::Patrons->find($args->{borrowernumber});
+    my $borrowernumber = $c->validation->param('borrowernumber');
+    my $patron = Koha::Patrons->find($borrowernumber);
     unless ($patron) {
-        return $c->$cb({error => "Patron not found"}, 404);
+        return $c->render(status => 404, openapi => { error => "Patron not found." });
     }
 
-    return $c->$cb($patron, 200);
+    return $c->render(status => 200, openapi => $patron);
 }
 
 1;
index 67632e1..204ccdf 100644 (file)
@@ -1,6 +1,7 @@
 {
   "/patrons": {
     "get": {
+      "x-mojo-to": "Patron#list",
       "operationId": "listPatrons",
       "tags": ["patrons"],
       "produces": [
             }
           }
         },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
         "403": {
           "description": "Access forbidden",
           "schema": {
             "$ref": "../definitions.json#/error"
           }
+        },
+        "500": {
+          "description": "Internal server error",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
         }
       },
       "x-koha-authorization": {
@@ -32,6 +51,7 @@
   },
   "/patrons/{borrowernumber}": {
     "get": {
+      "x-mojo-to": "Patron#get",
       "operationId": "getPatron",
       "tags": ["patrons"],
       "parameters": [{
             "$ref": "../definitions.json#/patron"
           }
         },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
         "403": {
           "description": "Access forbidden",
           "schema": {
           "schema": {
             "$ref": "../definitions.json#/error"
           }
+        },
+        "500": {
+          "description": "Internal server error",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
         }
       },
       "x-koha-authorization": {
index 909e36e..a8579db 100644 (file)
@@ -133,6 +133,6 @@ $t->request_ok($tx)
   ->status_is(200)
   ->json_is('/borrowernumber' => $borrower->{ borrowernumber })
   ->json_is('/surname' => $borrower->{ surname })
-  ->json_is('/lost' => 1 );
+  ->json_is('/lost' => Mojo::JSON->true );
 
 $schema->storage->txn_rollback;