View | Details | Raw Unified | Return to bug 17008
Collapse All | Expand All

(-)a/Koha/Object.pm (-1 / +13 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::Util::Swagger;
25
26
26
=head1 NAME
27
=head1 NAME
27
28
Lines 219-224 sub unblessed { Link Here
219
    return { $self->_result->get_columns };
220
    return { $self->_result->get_columns };
220
}
221
}
221
222
223
=head3 $object->swaggerize();
224
225
Returns an unblessed, Swagger-ready representation of object
226
227
=cut
228
229
sub swaggerize {
230
    my ($self) = @_;
231
232
    return Koha::Util::Swagger::swaggerize_koha_object($self);
233
}
234
222
=head3 $object->_result();
235
=head3 $object->_result();
223
236
224
Returns the internal DBIC Row object
237
Returns the internal DBIC Row object
Lines 250-256 sub _columns { Link Here
250
    return $self->{_columns};
263
    return $self->{_columns};
251
}
264
}
252
265
253
254
=head3 AUTOLOAD
266
=head3 AUTOLOAD
255
267
256
The autoload method is used only to get and set values for an objects properties.
268
The autoload method is used only to get and set values for an objects properties.
(-)a/Koha/Objects.pm (+12 lines)
Lines 220-225 sub unblessed { Link Here
220
    return [ map { $_->unblessed } $self->as_list ];
220
    return [ map { $_->unblessed } $self->as_list ];
221
}
221
}
222
222
223
=head3 Koha::Objects->swaggerize
224
225
Returns an unblessed, Swagger-ready representation of objects.
226
227
=cut
228
229
sub swaggerize {
230
    my ($self) = @_;
231
232
    return Koha::Util::Swagger::swaggerize_koha_objects($self);
233
}
234
223
=head3 Koha::Objects->_wrap
235
=head3 Koha::Objects->_wrap
224
236
225
wraps the DBIC object in a corresponding Koha object
237
wraps the DBIC object in a corresponding Koha object
(-)a/Koha/Util/Swagger.pm (+148 lines)
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_object
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;
(-)a/t/db_dependent/Koha/Util/Swagger.t (-1 / +71 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
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
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 6;
23
24
use Koha::Util::Swagger;
25
use Koha::Patrons;
26
27
use t::lib::TestBuilder;
28
29
my $schema = Koha::Database->new->schema;
30
$schema->storage->txn_begin;
31
32
my $builder       = t::lib::TestBuilder->new;
33
my $library = $builder->build({source => 'Branch' });
34
my $category = $builder->build({source => 'Category' });
35
my $nb_of_patrons = Koha::Patrons->search->count;
36
my $new_patron_1  = Koha::Patron->new(
37
    {   cardnumber => 'test_cn_1',
38
        branchcode => $library->{branchcode},
39
        categorycode => $category->{categorycode},
40
        surname => 'surname for patron1',
41
        firstname => 'firstname for patron1',
42
        userid => 'a_nonexistent_userid_1',
43
    }
44
)->store;
45
my $new_patron_2  = Koha::Patron->new(
46
    {   cardnumber => 'test_cn_2',
47
        branchcode => $library->{branchcode},
48
        categorycode => $category->{categorycode},
49
        surname => 'surname for patron2',
50
        firstname => 'firstname for patron2',
51
        userid => 'a_nonexistent_userid_2',
52
    }
53
)->store;
54
55
ok($new_patron_1->can("swaggerize"), "Koha::Patron can swaggerize");
56
ok(Koha::Patrons->can("swaggerize"), "Koha::Patrons can swaggerize");
57
58
my $swaggerized = $new_patron_1->swaggerize;
59
my $unblessed = $new_patron_1->unblessed;
60
my $swaggerizeds = Koha::Patrons->search->swaggerize;
61
62
my $unblesseds = Koha::Patrons->search->unblessed;
63
64
is(Koha::Util::Swagger::column_type_to_swagger_type("varchar"), "string", "varchar = string");
65
is(Koha::Util::Swagger::column_type_to_swagger_type("mediumint"), "integer", "mediumint = integer");
66
is(Koha::Util::Swagger::column_type_to_swagger_type("decimal"), "number", "decimal = number");
67
is(@$swaggerizeds, @$unblesseds, "Koha::Objects->swaggerize returned as many objects as unblessed (".@$unblesseds.")");
68
69
$schema->storage->txn_rollback;
70
71
1;

Return to bug 17008