|
Line 0
Link Here
|
|
|
1 |
package Koha::Util::Swagger; |
| 2 |
|
| 3 |
# Copyright 2016 KohaSuomi |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Mojo::JSON; |
| 23 |
|
| 24 |
use Koha::Database; |
| 25 |
|
| 26 |
=head1 NAME |
| 27 |
|
| 28 |
Koha::Util::Swagger - utility class with routines for Swagger |
| 29 |
|
| 30 |
=head1 FUNCTIONS |
| 31 |
|
| 32 |
=head2 column_type_to_swagger_type |
| 33 |
|
| 34 |
Takes a C<$column_type> and converts it to one of the data types defined in |
| 35 |
Swagger. |
| 36 |
|
| 37 |
See http://swagger.io/specification/ |
| 38 |
|
| 39 |
=cut |
| 40 |
|
| 41 |
sub column_type_to_swagger_type { |
| 42 |
my ($column_type) = @_; |
| 43 |
|
| 44 |
my $mapping = { |
| 45 |
############ BOOLEAN ############ |
| 46 |
'bool' => 'boolean', |
| 47 |
'boolean' => 'boolean', |
| 48 |
|
| 49 |
############ INTEGERS ########### |
| 50 |
'bigint' => 'integer', |
| 51 |
'integer' => 'integer', |
| 52 |
'int' => 'integer', |
| 53 |
'mediumint' => 'integer', |
| 54 |
'smallint' => 'integer', |
| 55 |
'tinyint' => 'integer', |
| 56 |
|
| 57 |
############ NUMBERS ############ |
| 58 |
'decimal' => 'number', |
| 59 |
'double precision' => 'number', |
| 60 |
'float' => 'number', |
| 61 |
|
| 62 |
############ STRINGS ############ |
| 63 |
'blob' => 'string', |
| 64 |
'char' => 'string', |
| 65 |
'date' => 'string', |
| 66 |
'datetime' => 'string', |
| 67 |
'enum' => 'string', |
| 68 |
'longblob' => 'string', |
| 69 |
'longtext' => 'string', |
| 70 |
'mediumblob' => 'string', |
| 71 |
'mediumtext' => 'string', |
| 72 |
'text' => 'string', |
| 73 |
'tinyblob' => 'string', |
| 74 |
'tinytext' => 'string', |
| 75 |
'timestamp' => 'string', |
| 76 |
'varchar' => 'string', |
| 77 |
|
| 78 |
}; |
| 79 |
|
| 80 |
return $mapping->{$column_type} if exists $mapping->{$column_type}; |
| 81 |
} |
| 82 |
|
| 83 |
=head2 swaggerize_koha_object |
| 84 |
|
| 85 |
Returns an unblessed Swagger-ready representation of Koha-object. Checks |
| 86 |
column type and attempts to convert it into Swagger data type. This allows |
| 87 |
us to represent e.g. numbers as actual numbers in JSON, without quotations. |
| 88 |
|
| 89 |
=cut |
| 90 |
|
| 91 |
sub swaggerize_koha_object { |
| 92 |
my ($object) = @_; |
| 93 |
|
| 94 |
return unless $object; |
| 95 |
|
| 96 |
if ($object->can("as_list")) { |
| 97 |
return swaggerize_koha_objects($object); |
| 98 |
} |
| 99 |
|
| 100 |
my $unblessed = $object->unblessed; |
| 101 |
|
| 102 |
my $columns = Koha::Database->new->schema->resultset($object->_type) |
| 103 |
->result_source->{_columns}; |
| 104 |
foreach my $col (keys $columns) { |
| 105 |
if ($columns->{$col}->{is_nullable} && !defined $unblessed->{$col}) { |
| 106 |
$unblessed->{$col} = undef; |
| 107 |
next; |
| 108 |
} |
| 109 |
|
| 110 |
my $col_type = $columns->{$col}->{data_type}; |
| 111 |
my $swagger_type = column_type_to_swagger_type($col_type); |
| 112 |
if ($swagger_type eq "string") { |
| 113 |
unless ($unblessed->{$col}) { |
| 114 |
$unblessed->{$col} = ""; |
| 115 |
} else { |
| 116 |
$unblessed->{$col} = "$unblessed->{$col}"; |
| 117 |
} |
| 118 |
} |
| 119 |
elsif ($swagger_type eq "boolean") { |
| 120 |
if ($unblessed->{$col}) { |
| 121 |
$unblessed->{$col} = Mojo::JSON->true; |
| 122 |
} else { |
| 123 |
$unblessed->{$col} = Mojo::JSON->false; |
| 124 |
} |
| 125 |
} |
| 126 |
else { |
| 127 |
$unblessed->{$col} += 0; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $unblessed; |
| 132 |
} |
| 133 |
|
| 134 |
=head2 swaggerize_koha_objects |
| 135 |
|
| 136 |
Returns an unblessed Swagger-ready representation of Koha-objects. |
| 137 |
|
| 138 |
=cut |
| 139 |
|
| 140 |
sub swaggerize_koha_objects { |
| 141 |
my ($objects) = @_; |
| 142 |
|
| 143 |
return unless $objects; |
| 144 |
|
| 145 |
return [ map { $_->swaggerize } $objects->as_list ]; |
| 146 |
} |
| 147 |
|
| 148 |
1; |