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

(-)a/Koha/REST/V1.pm (+37 lines)
Lines 18-23 package Koha::REST::V1; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
use Mojo::Base 'Mojolicious';
19
use Mojo::Base 'Mojolicious';
20
20
21
use File::Find::Rule;
22
21
use C4::Auth qw( check_cookie_auth get_session );
23
use C4::Auth qw( check_cookie_auth get_session );
22
use C4::Context;
24
use C4::Context;
23
use Koha::Patrons;
25
use Koha::Patrons;
Lines 43-48 sub startup { Link Here
43
    # Force charset=utf8 in Content-Type header for JSON responses
45
    # Force charset=utf8 in Content-Type header for JSON responses
44
    $self->types->type(json => 'application/json; charset=utf8');
46
    $self->types->type(json => 'application/json; charset=utf8');
45
47
48
    $self->minifySwagger();
49
46
    my $secret_passphrase = C4::Context->config('api_secret_passphrase');
50
    my $secret_passphrase = C4::Context->config('api_secret_passphrase');
47
    if ($secret_passphrase) {
51
    if ($secret_passphrase) {
48
        $self->secrets([$secret_passphrase]);
52
        $self->secrets([$secret_passphrase]);
Lines 54-57 sub startup { Link Here
54
    });
58
    });
55
}
59
}
56
60
61
sub minifySwagger {
62
    my ($self, $swaggerPath) = @_;
63
64
    $swaggerPath = C4::Context->config("intranetdir").'/api/v1/' unless $swaggerPath;
65
    my $pathToMinifier = $swaggerPath.'minifySwagger.pl';
66
    my $pathToSwaggerJson = $swaggerPath.'swagger.json';
67
    my $pathToSwaggerMinJson = $swaggerPath.'swagger.min.json';
68
69
    if (_isMinificationRequired($swaggerPath, $pathToSwaggerJson, $pathToSwaggerMinJson)) {
70
        my $output = `perl $pathToMinifier -s $pathToSwaggerJson -d $pathToSwaggerMinJson`;
71
        if ($output) {
72
            die $output;
73
        }
74
    }
75
}
76
77
sub _isMinificationRequired {
78
    my ($swaggerPath, $pathToSwaggerJson, $pathToSwaggerMinJson) = @_;
79
80
    return 1 unless -e $pathToSwaggerJson; # swagger.json exists?
81
    return 1 unless -e $pathToSwaggerMinJson; # swagger.min.json exists?
82
    my $latestModification = -C $pathToSwaggerJson;
83
    
84
    # Recursively go through specification files and find the last modified
85
    my $lastModifiedFile;
86
    my @files = File::Find::Rule->file()->name("*.json")->in($swaggerPath);
87
    foreach my $file (@files){
88
        next if $file eq $pathToSwaggerMinJson;
89
        $latestModification = -C $file if -C $file < $latestModification;
90
    }
91
    return 1 if $latestModification < -C $pathToSwaggerMinJson; # compare modification time
92
}
93
57
1;
94
1;
(-)a/t/api/v1/minifier.t (-1 / +146 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# Copyright (C) 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 File::Copy qw/copy/;
23
use File::Temp;
24
use File::Path qw/make_path rmtree/;
25
26
use Test::More tests => 12;
27
28
use C4::Context;
29
use Koha::REST::V1;
30
31
my $oldSwagger_title = "Swagger Sample App";
32
my $newSwagger_title = "Koha REST API";
33
34
# Construct an example swagger.json
35
my $swaggerJsonTest =
36
qq ({
37
  "swagger": "2.0",
38
  "info": {
39
    "title": "$oldSwagger_title",
40
    "version": "1"
41
  },
42
  "paths": {},
43
  "definitions": {},
44
  "parameters": {}
45
});
46
47
my $swaggerJsonTestModified =
48
qq ({
49
  "swagger": "2.0",
50
  "info": {
51
    "title": "$newSwagger_title",
52
    "version": "1"
53
  },
54
  "paths": {},
55
  "definitions": {
56
    "\$ref": "definitions.json"
57
  },
58
  "parameters": {}
59
});
60
my $swaggerJsonTestDefinitions =
61
qq ({
62
  "test": {
63
    "type": "object",
64
    "properties": {
65
      "id": {
66
        "type": "integer",
67
        "description": "test integer"
68
      }
69
    }
70
  }
71
});
72
73
# Path containing actual swagger specification files
74
my $real_swaggerPath = C4::Context->config("intranetdir").'/api/v1/';
75
76
# Create a tmp directory where we can modify swagger.json
77
my $swaggerPath = File::Temp->newdir()."/";
78
make_path($swaggerPath);
79
80
# Copy minifySwagger.pl to this location
81
copy($real_swaggerPath.'minifySwagger.pl', $swaggerPath);
82
83
my $pathToMinifier = $swaggerPath.'minifySwagger.pl';
84
my $pathToSwaggerJson = $swaggerPath.'swagger.json';
85
my $pathToSwaggerDefinitionsJson = $swaggerPath.'definitions.json';
86
my $pathToSwaggerMinJson = $swaggerPath.'swagger.min.json';
87
88
is(-e $pathToSwaggerJson, undef, "swagger.json does not yet exist");
89
is(-e $pathToSwaggerMinJson, undef, "swagger.min.json does not yet exist");
90
is(-e $pathToSwaggerDefinitionsJson, undef, "definitions.json does not yet exist");
91
92
my $exists = -e $pathToSwaggerJson;
93
# Create swagger.json test file
94
open my $fh, '>', $pathToSwaggerJson;
95
print $fh $swaggerJsonTest;
96
close $fh;
97
98
ok(-e $pathToSwaggerJson, "swagger.json created!");
99
100
Koha::REST::V1::minifySwagger(undef, $swaggerPath);
101
ok(-e $pathToSwaggerMinJson, "swagger.min.json created!");
102
my $lastmodified = -C $pathToSwaggerMinJson;
103
104
# Read swagger.min.json
105
require Swagger2;
106
my $swaggerMin = Swagger2->new($pathToSwaggerMinJson);
107
$swaggerMin = $swaggerMin->expand;
108
my $oldSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'};
109
is($oldSwaggerTitle, $oldSwagger_title, "old title found");
110
111
# Modify swagger.json
112
sleep(1); # make sure modification time differs from swagger.min.json
113
open $fh, '>', $pathToSwaggerJson;
114
print $fh $swaggerJsonTestModified;
115
close $fh;
116
117
# Add definitions.json
118
sleep(1); # make sure modification time differs from swagger.min.json
119
open $fh, '>', $pathToSwaggerDefinitionsJson;
120
print $fh $swaggerJsonTestDefinitions;
121
close $fh;
122
123
ok(-e $pathToSwaggerDefinitionsJson, "definitions.json created!");
124
125
Koha::REST::V1::minifySwagger(undef, $swaggerPath);
126
127
ok($lastmodified > -C $pathToSwaggerMinJson,
128
   "swagger.min.json modified after editing swagger.json");
129
$lastmodified = -C $pathToSwaggerMinJson;
130
131
# Re-read swagger.min.json
132
$swaggerMin = Swagger2->new($pathToSwaggerMinJson);
133
$swaggerMin = $swaggerMin->expand;
134
my $newSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'};
135
is($newSwaggerTitle, $newSwagger_title, "new title found"); # was minified
136
is($swaggerMin->{'api_spec'}->{'data'}->{'definitions'}
137
   ->{'test'}->{'properties'}->{'id'}->{'description'},
138
   "test integer", "test integer found!");
139
140
Koha::REST::V1::minifySwagger(undef, $swaggerPath);
141
ok($lastmodified == -C $pathToSwaggerMinJson,
142
   "swagger.min.json not modified because no modifications to swagger.json");
143
144
# Cleanup
145
rmtree($swaggerPath) if $swaggerPath ne $real_swaggerPath;
146
is(-e $swaggerPath, undef, "tmp files deleted");

Return to bug 16212