3 # Copyright 2016 KohaSuomi
5 # This file is part of Koha.
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
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.
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.
22 use Getopt::Long qw(:config no_ignore_case);
26 my $swaggerFile = "swagger.json";
27 my $swaggerMinifiedFile = "swagger.min.json";
31 'v|verbose:i' => \$verbose,
32 's|source:s' => \$swaggerFile,
33 'd|destination:s' => \$swaggerMinifiedFile,
40 Reads the swagger.json -file and all referenced JSON-Schema -files and merges
41 them into one merged and minified version. This minified swagger-file is
42 intended to be shared to our API consumers.
44 By default you must run this script from the same directory as the 'swagger.json'
45 and the minified specification is written to 'swagger.min.json'.
46 This is a convenience to make the minification process as easy as possible.
48 !DO NOT! set the executable flag on, or this can be ran from the internetz.
50 -h --help This happy helpful help!
52 -v --verbose 0, default, no output.
53 1, turn on internal Swagger debugging flags
54 2, more verbose Swagger2 debugging
56 -s --source Which Swagger2-specification file to minify?
57 Defaults to "swagger.json"
59 -d --destination Where to write the minified Swagger2-spec?
60 Defaults to "swagger.min.json"
64 perl minifySwagger.pl -v 1 -s api/v1/swagger/swagger.json -d swag.json
65 cd api/v1/swagger && perl minifySwagger.pl
74 $ENV{SWAGGER2_DEBUG} = $verbose;
78 require Swagger2; #When you import the Swagger2-libraries, the environment variables are checked and cannot be altered anymore. So set verbosity first, then load libs.
79 my $swagger = Swagger2->new($swaggerFile);
80 $swagger = $swagger->expand; #Fetch all JSON-Schema references
82 my @errors = $swagger->validate;
83 print join("\n", "Swagger2: Invalid spec:", @errors)."\n" if @errors;
86 removeNonSwagger2Values($swagger);
88 open(SWOUT, ">:encoding(UTF-8)", $swaggerMinifiedFile) or die "$0: Couldn't open the minified Swagger2 output file '$swaggerMinifiedFile':\n $!";
89 print SWOUT $swagger->to_string();
92 ##For some reason stringifying the Swagger2-spec adds non-valid parameters, like "/id"
93 sub removeNonSwagger2Values {
96 my $data = $swagger->api_spec->data;