Browse Source

Bug 23653: use local copy of swagger v2 schema

By default, JSON::Validator::OpenAPI tries to fetch the
swagger v2 schema from http://swagger.io/v2/schema.json.

If you've installed from CPAN, JSON::Validator::OpenAPI will
come with a cached copy, so it won't try to fetch it over HTTP.

However, if you've installed from libjson-validator-perl
from Debian/Ubuntu, the Debian package excludes the cached copy,
so JSON::Validator::OpenAPI tries to fetch it over HTTP.

Unfortunately, today and other days in the past, the file at
http://swagger.io/v2/schema.json has been unavailable, and this causes
Koha to crash in a perpetual loop.

This patch includes a copy of the swagger v2 schema, and it loads
it locally rather than fetching over HTTP.

The changes to Koha/REST/Plugin/PluginRoutes.pm are not required,
since the validator isn't currently called there, but I've added
a patch to future proof it.

To Test:
0a) Remove /usr/share/perl5/JSON/Validator/cache/36d1bd12eeed51e86c8695bd8876a9df
if it exists
0b) Block external access to http://swagger.io/v2/schema.json or
test during an outage when it's unavailable
0c) Do not apply patch
1) koha-plack --restart kohadev
2) Note that it crashes in a loop and is unavailable in web browser
3) Apply patch
4) koha-plack --restart kohadev
5) Note that Koha comes up and there are no errors in the Plack logs

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
20.11.x
David Cook 1 year ago
committed by Jonathan Druart
parent
commit
e0a4bad3fc
  1. 16
      Koha/REST/Plugin/PluginRoutes.pm
  2. 3
      Koha/REST/V1.pm
  3. 1607
      api/swagger-v2-schema.json

16
Koha/REST/Plugin/PluginRoutes.pm

@ -47,6 +47,11 @@ sub register {
if ( C4::Context->config("enable_plugins") )
{
my $schema = $app->home->rel_file("api/swagger-v2-schema.json");
if ($schema){
$self->{'swagger-v2-schema'} = $schema;
}
# plugin needs to define a namespace
@plugins = Koha::Plugins->new()->GetPlugins(
{
@ -55,7 +60,7 @@ sub register {
);
foreach my $plugin ( @plugins ) {
$spec = inject_routes( $spec, $plugin, $validator );
$spec = $self->inject_routes( $spec, $plugin, $validator );
}
}
@ -68,14 +73,14 @@ sub register {
=cut
sub inject_routes {
my ( $spec, $plugin, $validator ) = @_;
my ( $self, $spec, $plugin, $validator ) = @_;
return merge_spec( $spec, $plugin ) unless $validator;
return try {
my $backup_spec = merge_spec( clone($spec), $plugin );
if ( spec_ok( $backup_spec, $validator ) ) {
if ( $self->spec_ok( $backup_spec, $validator ) ) {
$spec = merge_spec( $spec, $plugin );
}
else {
@ -139,13 +144,16 @@ sub merge_spec {
=cut
sub spec_ok {
my ( $spec, $validator ) = @_;
my ( $self, $spec, $validator ) = @_;
my $schema = $self->{'swagger-v2-schema'};
return try {
$validator->load_and_validate_schema(
$spec,
{
allow_invalid_ref => 1,
schema => ( $schema ) ? $schema : undef,
}
);
return 1;

3
Koha/REST/V1.pm

@ -72,6 +72,7 @@ sub startup {
# Try to load and merge all schemas first and validate the result just once.
my $spec;
my $swagger_schema = $self->home->rel_file("api/swagger-v2-schema.json");
try {
$spec = $validator->bundle(
{
@ -91,6 +92,7 @@ sub startup {
OpenAPI => {
spec => $spec,
route => $self->routes->under('/api/v1')->to('Auth#under'),
schema => ( $swagger_schema ) ? $swagger_schema : undef,
allow_invalid_ref =>
1, # required by our spec because $ref directly under
# Paths-, Parameters-, Definitions- & Info-object
@ -105,6 +107,7 @@ sub startup {
$self->home->rel_file("api/v1/swagger/swagger.json"),
{
allow_invalid_ref => 1,
schema => ( $swagger_schema ) ? $swagger_schema : undef,
}
);

1607
api/swagger-v2-schema.json

File diff suppressed because it is too large
Loading…
Cancel
Save