OpenAPI.pm

filter: Statementstmt 83.4% 136 / 163 Branchbran 54.1% 26 / 48 Conditioncond 50.0% 10 / 20 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 81.2% 26 / 32 Podpod 66.6% 4 / 6 totaltotal 73.3% 206 / 281 CC 38 SCAR 23.8
CC
38
Cov
74%
openapi_auto_discover
56.0
openapi_path
14.0
__ANON__
12.0
CRAP 10.9
« OpenAPI/Path.pm ↑ Summary
1 # TODO: add responses
2 # TODO: add examples
3 # TODO: then add the template for different responses values
4 # TODO: override send_error ?
5 # TODO: add 'validate_schema'
6 # TODO: add 'strict_schema'
7 # TODO: make /swagger.json configurable
8  
9 package Dancer2::Plugin::OpenAPI;
10 4 our $AUTHORITY = 'cpan:YANICK';
11 # ABSTRACT: create OpenAPI documentation of your application
12 4 $Dancer2::Plugin::OpenAPI::VERSION = '1.0.2';
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use Dancer2::Plugin::OpenAPI::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 ▶use MooseX::MungeHas 'is_ro';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use File::ShareDir::Tarball;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 has doc => (
28 is => 'ro',
29 lazy => 1,
30 3 ▶ default => sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
31 3 my $self = shift;
32  
33 3 my $doc = {
34 openapi => '3.1.0',
35 paths => {},
36 };
37  
38 3 $doc->{info}{$_} = '' for qw/ title description version /;
39  
40 3 ▶ $doc->{info}{title} = $self->main_api_module if $self->main_api_module;
Branch: 1 branch
BranchTrueFalseTotal
if $self->main_api_module 3 0 3
41  
42 3 ▶ if( my( $desc) = $self->main_api_module_content =~ /
Branch: 1 branch
BranchTrueFalseTotal
if (my($desc) = $self->main_api_module_content =~ / ^(?:\s* \# \s* ABSTRACT: \s* |=head1 \s+ NAME \s+ (?:\w+) \s+ - \s+ ) ([^\n]+) /mx) 0 3 3
43 ^(?:\s* \# \s* ABSTRACT: \s* |=head1 \s+ NAME \s+ (?:\w+) \s+ - \s+ ) ([^\n]+)
44 /xm
45 ) {
46 0 $doc->{info}{description} = $desc;
47 }
48  
49 3 ▶ $doc->{info}{version} = eval {
Condition100%2/2
eval { do { $self->main_api_module->VERSION } } // "0.0.0"
l!l
12
MC/DC100%1/1
eval { do { $self->main_api_module->VERSION } } // "0.0.0"
eval { do { $self->main_api_module->VERSION } }
Truth table100%2/2
A
A= eval { do { $self->main_api_module->VERSION } }
Aresult
11
00
50 3 $self->main_api_module->VERSION
51 } // '0.0.0';
52  
53 3 $doc;
54
55 },
56 4 );
57  
58 4 our $FIRST_LOADED = caller(1);
59  
60 has main_api_module => (
61 is => 'ro',
62 lazy => 1,
63 from_config => 1,
64 3 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
65 3 ▶ return if $] lt '5.036000';
Branch: 1 branch
BranchTrueFalseTotal
if $] lt "5.036000" 0 3 3
66  
67 3 ▶ $Dancer2::Plugin::OpenAPI::FIRST_LOADED //= caller;
Condition33%1/3
$Dancer2::Plugin::OpenAPI::FIRST_LOADED //= caller
l!l&&r!l&&!r
300
MC/DC0%0/2
$Dancer2::Plugin::OpenAPI::FIRST_LOADED //= caller
$Dancer2::Plugin::OpenAPI::FIRST_LOADED caller
Truth table33%1/3
A //= B
A= $Dancer2::Plugin::OpenAPI::FIRST_LOADEDB= caller
ABresult
1X1
011
000
68 },
69 4 );
70  
71 has main_api_module_content => (
72 is => 'ro',
73 lazy => 1,
74 3 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
75 3 ▶ my $mod = $_[0]->main_api_module or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $mod = $_[0]->main_api_module or return "" 3 0 3
76  
77 3 $mod =~ s#::#/#g;
78 3 $mod .= '.pm';
79 3 ▶ my $path = $INC{$mod} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $path = $INC{$mod} or return "" 0 3 3
80  
81 0 return Path::Tiny::path($path)->slurp;
82 }
83 4 );
84  
85 has show_ui => (
86 is => 'ro',
87 lazy => 1,
88 3 ▶ from_config => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 4 );
90  
91 has ui_url => (
92 is => 'ro',
93 lazy => 1,
94 3 ▶ from_config => sub { '/doc' },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 4 );
96  
97 has ui_dir => (
98 is => 'ro',
99 lazy => 1,
100 0 ▶ from_config => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 Path::Tiny::path(
102 File::ShareDir::Tarball::dist_dir('Dancer2-Plugin-OpenAPI')
103 )
104 },
105 4 );
106  
107 has auto_discover_skip => (
108 is => 'ro',
109 lazy => 1,
110 0 ▶ default => sub { [
Subroutine __ANON__: not called CC=3 SCAR=24.8
111 0 ▶ map { /^qr/ ? eval $_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
/^qr/ ? : 0 0 0
112 0 @{ $_[0]->config->{auto_discover_skip} || [
113 0 ▶ '/openapi.json', ( 'qr!' . $_[0]->ui_url . '!' ) x $_[0]->show_ui
Condition0%0/2
$_[0]->config->{'auto_discover_skip'} || ['/openapi.json', ('qr!' . $_[0]->ui_url . '!') x $_[0]->show_ui]
l!l
00
MC/DC0%0/1
$_[0]->config->{'auto_discover_skip'} || ['/openapi.json', ('qr!' . $_[0]->ui_url . '!') x $_[0]->show_ui]
$_[0]->config->{'auto_discover_skip'}
Truth table0%0/2
A
A= $_[0]->config->{'auto_discover_skip'}
Aresult
11
00
114 ] }
115 ];
116 },
117 4 );
118  
119 4 has validate_response => ( from_config => 1 );
120 4 has strict_validation => ( from_config => 1 );
121  
122  
123 3 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=10.4
Pod undocumented
124 3 my $self = shift;
125  
126 # TODO make the doc url configurable
127 $self->app->add_route(
128 method => 'get',
129 regexp => '/openapi.json',
130 1 ▶ code => sub { $self->doc },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 3 );
132  
133 3 ▶ if ( $self->show_ui ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_ui) 3 0 3
134 3 my $base_url = $self->ui_url;
135  
136 $self->app->add_route(
137 method => 'get',
138 regexp => $base_url,
139 0 ▶ code => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 $self->app->redirect( $base_url . '/?url=/openapi.json' );
141 },
142 3 );
143  
144 $self->app->add_route(
145 method => 'get',
146 regexp => $base_url . '/',
147 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
148 0 my $file = $self->ui_dir->child('index.html');
149  
150 0 ▶ $self->app->send_error( "file not found", 404 ) unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
151  
152 0 return $self->app->send_as( html => $file->slurp );
153 }
154 3 );
155  
156 $self->app->add_route(
157 method => 'get',
158 regexp => $base_url . '/**',
159 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
160 0 my $file = $self->ui_dir->child( @{ ($self->app->request->splat())[0] } );
161  
162 0 ▶ $self->app->send_error( "file not found", 404 ) unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
163  
164 0 $self->app->send_file( $file, system_path => 1);
165 }
166 3 );
167 }
168
169 }
170  
171 0 ▶sub openapi_auto_discover :PluginKeyword {
Subroutine openapi_auto_discover: not called CC=7 SCAR=40.3
Pod documented
172 0 my( $plugin, %args ) = @_;
173  
174 0 ▶ $args{skip} ||= $plugin->auto_discover_skip;
Condition0%0/3
$args{"skip"} ||= $plugin->auto_discover_skip
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"skip"} ||= $plugin->auto_discover_skip
$args{"skip"} $plugin->auto_discover_skip
Truth table0%0/3
A ||= B
A= $args{"skip"}B= $plugin->auto_discover_skip
ABresult
1X1
011
000
175  
176 0 my $routes = Dancer2::App->current->registry->routes;
177  
178 # my $doc = $plugin->doc->{paths};
179  
180 0 for my $method ( qw/ get post put delete / ) {
181 0 for my $r ( @{ $routes->{$method} } ) {
182 0 my $pattern = $r->pattern;
183  
184 0 ▶ next if ref $pattern eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $pattern eq "Regexp" 0 0 0
185  
186 0 ▶ next if grep { ref $_ ? $pattern =~ $_ : $pattern eq $_ } @{ $args{skip} };
Branch: 2 branches
BranchTrueFalseTotal
if grep {ref $_ ? $pattern =~ /$_/ : $pattern eq $_;} @{$args{"skip"};} 0 0 0
ref $_ ? : 0 0 0
187  
188 0 my $path = Dancer2::Plugin::OpenAPI::Path->new(
189 plugin => $plugin,
190 route => $r );
191  
192 0 $path->add_to_doc($plugin->doc);
193 }
194 }
195 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 8 ▶sub openapi_path :PluginKeyword {
Subroutine openapi_path: called CC=14 SCAR=26.4
Pod documented
198 8 my $plugin = shift;
199  
200 8 $DB::single = 1;
201 8 my @routes;
202 8 ▶ push @routes, pop @_ while eval { $_[-1]->isa('Dancer2::Core::Route') };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_[-1]->isa("Dancer2::Core::Route") } } and do { push @routes, pop @_ } 16 8 24
203  
204 # we don't process HEAD
205 8 @routes = grep { $_->method ne 'head' } @routes;
206  
207  
208 8 my $description;
209 8 ▶ if( @_ and not ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not ref $_[0]) 1 7 8
Condition100%3/3
@_ and not ref $_[0]
!ll&&!rl&&r
161
MC/DC100%2/2
@_ and not ref $_[0]
@_ not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0]
ABresult
0X0
100
111
210 1 $description = shift;
211 1 $description =~ s/^\s*\n//;
212
213 1 ▶ $description =~ s/^$1//mg
Branch: 1 branch
BranchTrueFalseTotal
if $description =~ /^(\s+)/ 1 0 1
214 if $description =~ /^(\s+)/;
215 }
216  
217 8 ▶ my $arg = shift @_ || {};
Condition100%2/2
shift @_ || {}
l!l
62
MC/DC100%1/1
shift @_ || {}
shift @_
Truth table100%2/2
A
A= shift @_
Aresult
11
00
218  
219 8 ▶ $arg->{description} = $description if $description;
Branch: 1 branch
BranchTrueFalseTotal
if $description 1 7 8
220  
221 # groom the parameters
222 8 ▶ if ( my $p = $arg->{parameters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $arg->{"parameters"}) 4 4 8
223 4 ▶ if( ref $p eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "HASH") 2 2 4
224 2 $_ = { description => $_ } for grep { ! ref } values %$p;
225 2 $p = [ map { +{ name => $_, %{$p->{$_}} } } sort keys %$p ];
226 }
227  
228 # deal with named parameters
229 4 my @p;
230 4 ▶ while( my $k = shift @$p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $k = shift @$p) 9 4 13
231 9 ▶ unless( ref $k ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $k) 2 7 9
232 2 my $value = shift @$p;
233 2 ▶ $value = { description => $value } unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 1 1 2
234 2 $value->{name} = $k;
235 2 $k = $value;
236 }
237 9 push @p, $k;
238 }
239 4 $p = \@p;
240  
241 # set defaults
242 4 $p = [ map { +{ in => 'query', type => 'string', %$_ } } @$p ];
243
244 4 $arg->{parameters} = $p;
245 }
246  
247  
248 8 for my $route ( @routes ) {
249 8 my $path = Dancer2::Plugin::OpenAPI::Path->new(%$arg, route => $route, plugin => $plugin );
250  
251 8 $path->add_to_doc( $plugin->doc );
252  
253 8 my $code = $route->{code};
254 # TODO change this so I don't play in D2's guts directly
255 5 ▶ $route->{code} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
256 5 local $Dancer2::Plugin::OpenAPI::THIS_ACTION = $path;
257 5 $code->();
258 8 };
259 }
260 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 1 ▶sub openapi_template :PluginKeyword {
Subroutine openapi_template: called CC=4 SCAR=15.0
Pod documented
263 1 my $plugin = shift;
264  
265 1 my $vars = pop;
266 1 ▶ my $status = shift || $Dancer2::Core::Route::RESPONSE->status( @_ );
Condition33%1/3
shift() || $Dancer2::Core::Route::RESPONSE->status(@_)
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $Dancer2::Core::Route::RESPONSE->status(@_)
shift() $Dancer2::Core::Route::RESPONSE->status(@_)
Truth table33%1/3
A || B
A= shift()B= $Dancer2::Core::Route::RESPONSE->status(@_)
ABresult
1X1
011
000
267  
268 1 my $template = $Dancer2::Plugin::OpenAPI::THIS_ACTION->{responses}{$status}{template};
269  
270 1 ▶ $Dancer2::Core::Route::RESPONSE->status( $status ) if $status =~ /^\d{3}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $status =~ /^\d{3}$/ 1 0 1
271  
272 1 ▶ return $plugin->openapi_response( $status, $template ? $template->($vars) : $vars );
Branch: 1 branch
BranchTrueFalseTotal
$template ? : 0 1 1
273 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 1 ▶sub openapi_response :PluginKeyword {
Subroutine openapi_response: called CC=2 SCAR=7.0
Pod undocumented
276 1 my $plugin = shift;
277  
278 1 my $data = pop;
279  
280 1 my $status = $Dancer2::Core::Route::RESPONSE->status(@_);
281 # $Dancer2::Plugin::OpenAPI::THIS_ACTION->validate_response(
282 1 my $path = Dancer2::Plugin::OpenAPI::Path->new(plugin=>$plugin);
283 1 ▶$path->validate_response(
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->validate_response 0 1 1
284 $status => $data, $plugin->strict_validation
285 ) if $plugin->validate_response;
286  
287 1 $data;
288 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
289  
290 1 ▶sub openapi_definition :PluginKeyword {
Subroutine openapi_definition: called CC=2 SCAR=7.0
Pod documented
291 1 my $plugin = shift;
292  
293 1 my( $name, $def ) = @_;
294  
295 1 ▶ $plugin->doc->{definitions} ||= {};
Condition50%1/2
$plugin->doc->{"definitions"} ||= {}
l!l
01
MC/DC0%0/1
$plugin->doc->{"definitions"} ||= {}
$plugin->doc->{"definitions"}
Truth table50%1/2
A
A= $plugin->doc->{"definitions"}
Aresult
11
00
296  
297 1 $plugin->doc->{definitions}{$name} = $def;
298  
299 1 return { '$ref', => '#/definitions/'.$name };
300  
301 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
302  
303 4 1;
304  
305 __END__
« OpenAPI/Path.pm ↑ Summary