Lines 22-28
use Mojo::Base 'Mojolicious';
Link Here
|
22 |
use C4::Context; |
22 |
use C4::Context; |
23 |
use Koha::Logger; |
23 |
use Koha::Logger; |
24 |
|
24 |
|
25 |
use JSON::Validator::OpenAPI::Mojolicious; |
25 |
use JSON::Validator::Schema::OpenAPIv2; |
|
|
26 |
|
26 |
use Try::Tiny qw( catch try ); |
27 |
use Try::Tiny qw( catch try ); |
27 |
|
28 |
|
28 |
=head1 NAME |
29 |
=head1 NAME |
Lines 68-92
sub startup {
Link Here
|
68 |
$self->secrets([$secret_passphrase]); |
69 |
$self->secrets([$secret_passphrase]); |
69 |
} |
70 |
} |
70 |
|
71 |
|
71 |
my $validator = JSON::Validator::OpenAPI::Mojolicious->new; |
72 |
my $spec_file = $self->home->rel_file("api/v1/swagger/swagger.yaml"); |
72 |
|
73 |
|
73 |
push @{$self->routes->namespaces}, 'Koha::Plugin'; |
74 |
push @{$self->routes->namespaces}, 'Koha::Plugin'; |
74 |
|
75 |
|
75 |
# Try to load and merge all schemas first and validate the result just once. |
76 |
# Try to load and merge all schemas first and validate the result just once. |
76 |
my $spec; |
|
|
77 |
my $swagger_schema = $self->home->rel_file("api/swagger-v2-schema.json"); |
78 |
try { |
77 |
try { |
79 |
$spec = $validator->bundle( |
78 |
|
80 |
{ |
79 |
my $schema = JSON::Validator::Schema::OpenAPIv2->new; |
81 |
replace => 1, |
80 |
|
82 |
schema => $self->home->rel_file("api/v1/swagger/swagger.yaml") |
81 |
$schema->resolve( $spec_file ); |
83 |
} |
82 |
|
84 |
); |
83 |
my $spec = $schema->bundle->data; |
85 |
|
84 |
|
86 |
$self->plugin( |
85 |
$self->plugin( |
87 |
'Koha::REST::Plugin::PluginRoutes' => { |
86 |
'Koha::REST::Plugin::PluginRoutes' => { |
88 |
spec => $spec, |
87 |
spec => $spec, |
89 |
validator => undef |
88 |
validate => 0, |
90 |
} |
89 |
} |
91 |
) unless C4::Context->needs_install; # load only if Koha is installed |
90 |
) unless C4::Context->needs_install; # load only if Koha is installed |
92 |
|
91 |
|
Lines 94-104
sub startup {
Link Here
|
94 |
OpenAPI => { |
93 |
OpenAPI => { |
95 |
spec => $spec, |
94 |
spec => $spec, |
96 |
route => $self->routes->under('/api/v1')->to('Auth#under'), |
95 |
route => $self->routes->under('/api/v1')->to('Auth#under'), |
97 |
schema => ( $swagger_schema ) ? $swagger_schema : undef, |
|
|
98 |
allow_invalid_ref => |
99 |
1, # required by our spec because $ref directly under |
100 |
# Paths-, Parameters-, Definitions- & Info-object |
101 |
# is not allowed by the OpenAPI specification. |
102 |
} |
96 |
} |
103 |
); |
97 |
); |
104 |
} |
98 |
} |
Lines 111-129
sub startup {
Link Here
|
111 |
$logger->error("Warning: Could not load REST API spec bundle: " . $_); |
105 |
$logger->error("Warning: Could not load REST API spec bundle: " . $_); |
112 |
|
106 |
|
113 |
try { |
107 |
try { |
114 |
$validator->load_and_validate_schema( |
|
|
115 |
$self->home->rel_file("api/v1/swagger/swagger.yaml"), |
116 |
{ |
117 |
allow_invalid_ref => 1, |
118 |
schema => ( $swagger_schema ) ? $swagger_schema : undef, |
119 |
} |
120 |
); |
121 |
|
108 |
|
122 |
$spec = $validator->schema->data; |
109 |
my $schema = JSON::Validator::Schema::OpenAPIv2->new; |
|
|
110 |
$schema->resolve( $spec_file ); |
111 |
|
112 |
my $spec = $schema->bundle->data; |
113 |
|
123 |
$self->plugin( |
114 |
$self->plugin( |
124 |
'Koha::REST::Plugin::PluginRoutes' => { |
115 |
'Koha::REST::Plugin::PluginRoutes' => { |
125 |
spec => $spec, |
116 |
spec => $spec, |
126 |
validator => $validator |
117 |
validate => 1 |
127 |
} |
118 |
} |
128 |
) unless C4::Context->needs_install; # load only if Koha is installed |
119 |
) unless C4::Context->needs_install; # load only if Koha is installed |
129 |
|
120 |
|
Lines 131-140
sub startup {
Link Here
|
131 |
OpenAPI => { |
122 |
OpenAPI => { |
132 |
spec => $spec, |
123 |
spec => $spec, |
133 |
route => $self->routes->under('/api/v1')->to('Auth#under'), |
124 |
route => $self->routes->under('/api/v1')->to('Auth#under'), |
134 |
allow_invalid_ref => |
|
|
135 |
1, # required by our spec because $ref directly under |
136 |
# Paths-, Parameters-, Definitions- & Info-object |
137 |
# is not allowed by the OpenAPI specification. |
138 |
} |
125 |
} |
139 |
); |
126 |
); |
140 |
} |
127 |
} |