|
Lines 106-112
sub merge_spec {
Link Here
|
| 106 |
my $plugin_spec = $plugin->api_routes; |
106 |
my $plugin_spec = $plugin->api_routes; |
| 107 |
|
107 |
|
| 108 |
foreach my $route ( keys %{ $plugin_spec } ) { |
108 |
foreach my $route ( keys %{ $plugin_spec } ) { |
| 109 |
my $THE_route = '/contrib/' . $plugin->api_namespace . $route; |
109 |
my $THE_route = get_normalized_plugin_path($plugin->api_namespace, $route); |
| 110 |
if ( exists $spec->{ $THE_route } ) { |
110 |
if ( exists $spec->{ $THE_route } ) { |
| 111 |
# Route exists, overwriting is forbidden |
111 |
# Route exists, overwriting is forbidden |
| 112 |
Koha::Exceptions::Plugin::ForbiddenAction->throw( |
112 |
Koha::Exceptions::Plugin::ForbiddenAction->throw( |
|
Lines 118-129
sub merge_spec {
Link Here
|
| 118 |
} |
118 |
} |
| 119 |
} |
119 |
} |
| 120 |
|
120 |
|
|
|
121 |
if($plugin->can('api_spec')) { |
| 122 |
my $plugin_namespace = $plugin->api_namespace; |
| 123 |
my $plugin_spec = $plugin->api_spec; |
| 124 |
my @errors; |
| 125 |
if ($plugin_spec->{definitions}) { |
| 126 |
while (my ($k, $v) = each %{$plugin_spec->{definitions}}) { |
| 127 |
if ($spec->{definitions}->{$k}) { |
| 128 |
push(@errors, "Duplicate OpenAPI definition '$k'. Plugin needs to rename one of it's definitions."); |
| 129 |
} else { |
| 130 |
$spec->{definitions}->{$k} = $v; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
if ($plugin_spec->{parameters}) { |
| 135 |
while (my ($k, $v) = each %{$plugin_spec->{parameters}}) { |
| 136 |
if ($spec->{parameters}->{$k}) { |
| 137 |
push(@errors, "Duplicate OpenAPI parameter '$k'. Plugin needs to rename one of it's parameters."); |
| 138 |
} else { |
| 139 |
$spec->{parameters}->{$k} = $v; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
if ($plugin_spec->{tags}) { |
| 144 |
if ($spec->{tags}) { |
| 145 |
push(@{$spec->{tags}}, @{$plugin_spec->{tags}}); |
| 146 |
} else { |
| 147 |
$spec->{tags} = $plugin_spec->{tags}; |
| 148 |
} |
| 149 |
} |
| 150 |
if ($plugin_spec->{paths}) { |
| 151 |
while (my ($k, $v) = each %{$plugin_spec->{paths}}) { |
| 152 |
if ($spec->{paths}->{$k}) { |
| 153 |
push(@errors, "Duplicate OpenAPI parameter '$k'. Plugin needs to rename one of it's paths."); |
| 154 |
} else { |
| 155 |
$spec->{paths}->{get_normalized_plugin_path($plugin_namespace, $k)} = $v; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
if (@errors) { |
| 160 |
Koha::Exceptions::Plugin::ForbiddenAction->throw( |
| 161 |
"Plugin '".$plugin->{class}."' had the following errors while merging OpenAPI specs:\n".join(', ', @errors) |
| 162 |
); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 121 |
if($plugin->can('static_routes')) { |
166 |
if($plugin->can('static_routes')) { |
| 122 |
my $plugin_spec = $plugin->static_routes; |
167 |
my $plugin_spec = $plugin->static_routes; |
| 123 |
|
168 |
|
| 124 |
foreach my $route ( keys %{ $plugin_spec } ) { |
169 |
foreach my $route ( keys %{ $plugin_spec } ) { |
| 125 |
|
170 |
|
| 126 |
my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route; |
171 |
my $THE_route = get_normalized_plugin_static_path($plugin->api_namespace, $route); |
| 127 |
if ( exists $spec->{ $THE_route } ) { |
172 |
if ( exists $spec->{ $THE_route } ) { |
| 128 |
# Route exists, overwriting is forbidden |
173 |
# Route exists, overwriting is forbidden |
| 129 |
Koha::Exceptions::Plugin::ForbiddenAction->throw( |
174 |
Koha::Exceptions::Plugin::ForbiddenAction->throw( |
|
Lines 153-156
sub spec_ok {
Link Here
|
| 153 |
return !$schema->is_invalid; |
198 |
return !$schema->is_invalid; |
| 154 |
} |
199 |
} |
| 155 |
|
200 |
|
|
|
201 |
=head3 get_normalized_plugin_path |
| 202 |
|
| 203 |
public static subroutine |
| 204 |
Normalizes a plugin's route path to /contrib/<plugin-namespace>/<plugin-route-path> |
| 205 |
|
| 206 |
=cut |
| 207 |
|
| 208 |
sub get_normalized_plugin_path { |
| 209 |
my ($plugin_api_namespace, $plugin_path_name) = @_; |
| 210 |
return '/contrib/' . $plugin_api_namespace . $plugin_path_name; |
| 211 |
} |
| 212 |
|
| 213 |
=head3 get_normalized_plugin_static_path |
| 214 |
|
| 215 |
public static subroutine |
| 216 |
Normalizes a plugin's static route path to /contrib/<plugin-namespace>/static/<plugin-route-path> |
| 217 |
|
| 218 |
=cut |
| 219 |
|
| 220 |
sub get_normalized_plugin_static_path { |
| 221 |
my ($plugin_api_namespace, $plugin_path_name) = @_; |
| 222 |
return '/contrib/' . $plugin_api_namespace . '/static' . $plugin_path_name; |
| 223 |
} |
| 224 |
|
| 156 |
1; |
225 |
1; |