blib/lib/Raisin/Plugin/Swagger.pm

filter: Statementstmt 84.6% 221 / 261 Branchbran 69.7% 99 / 142 Conditioncond 47.1% 25 / 53 MC/DCmcdc 24.1% 7 / 29 Subroutinesub 93.9% 31 / 33 Podpod 100% 2 / 2 totaltotal 74.0% 385 / 520 CC 104 SCAR 28.8
CC
104
Cov
76%
swagger_security
110.0
_param_type
25.0
_param_type_object
20.8
CRAP 17.8
« blib/lib/Raisin/Entity.pm ↑ Summary blib/lib/Raisin.pm »
1 #!perl
2 #PODNAME: Raisin::Plugin::Swagger
3 #ABSTRACT: Generates API description in Swagger 2/OpenAPI compatible format
4 # vim:ts=4:shiftwidth=4:expandtab:syntax=perl
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Raisin::Plugin::Swagger;
10 6 $Raisin::Plugin::Swagger::VERSION = '0.94';
11 6 ▶use parent 'Raisin::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use JSON::MaybeXS qw/encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use List::Util qw/pairmap/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 my %DEFAULTS;
21 my %SETTINGS;
22  
23 6 my $HTTP_OK = 200;
24  
25 3 ▶sub build {
Subroutine build: called CC=1 SCAR=0.0
Pod documented
26 3 my $self = shift;
27  
28 $self->register(
29 19 ▶ swagger_build_spec => sub { $self->_spec_20 },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 3 ▶ swagger_setup => sub { %SETTINGS = @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 3 swagger_security => \&swagger_security,
32 );
33  
34 3 1;
35 }
36  
37 0 ▶sub swagger_security {
Subroutine swagger_security: not called CC=10 SCAR=47.0
Pod documented
38 0 my %p = @_;
39  
40 0 ▶ croak 'Invalid `type`' unless grep { $p{type} eq $_ } qw/basic api_key oauth2/;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$p{"type"} eq $_;} "basic", "api_key", "oauth2" 0 0 0
41  
42 0 my %security;
43  
44 0 ▶ if ($p{type} eq 'basic') {
Branch: 3 branches
BranchTrueFalseTotal
if ($p{'type'} eq 'basic') { } 0 0 0
elsif ($p{'type'} eq 'api_key') { } 0 0 0
elsif ($p{'type'} eq 'oauth2') { } 0 0 0
45 $security{ $p{name} } = {
46 0 type => 'basic',
47 };
48 }
49 elsif ($p{type} eq 'api_key') {
50 0 ▶ croak 'Invalid `in`' unless grep { $p{in} eq $_ } qw/query header/;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$p{"in"} eq $_;} "query", "header" 0 0 0
51  
52 $security{ $p{name} } = {
53 type => 'apiKey',
54 name => $p{name},
55 in => $p{in},
56 0 };
57 }
58 elsif ($p{type} eq 'oauth2') {
59 0 ▶ croak 'Invalid `flow`' unless grep { $p{flow} eq $_ } qw/implicit password application accessCode/;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$p{"flow"} eq $_;} "implicit", "password", "application", "accessCode" 0 0 0
60  
61 $security{ $p{name} } = {
62 type => 'oauth2',
63 flow => $p{flow},
64 scopes => $p{scopes},
65 0 };
66  
67 0 ▶ if (grep { $p{flow} eq $_ } qw/implicit accessCode/) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$p{"flow"} eq $_;} "implicit", "accessCode") 0 0 0
68 0 $security{ $p{name} }{authorizationUrl} = $p{authorization_url};
69 }
70  
71 0 ▶ if (grep { $p{flow} eq $_ } qw/password application accessCode/) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$p{"flow"} eq $_;} "password", "application", "accessCode") 0 0 0
72 0 $security{ $p{name} }{tokenUrl} = $p{token_url};
73 }
74 }
75  
76 $SETTINGS{security} = {
77 0 ▶ %{ $SETTINGS{security} || {} },
Condition0%0/2
$SETTINGS{'security'} || {}
l!l
00
MC/DC0%0/1
$SETTINGS{'security'} || {}
$SETTINGS{'security'}
Truth table0%0/2
A
A= $SETTINGS{'security'}
Aresult
11
00
78 %security,
79 };
80 }
81  
82 20 ▶sub _spec_20 {
Subroutine _spec_20: called CC=7 SCAR=20.8
83 20 my $self = shift;
84 20 ▶ return 1 if $self->{built};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"built"} 18 2 20
85  
86 2 my $app = $self->app;
87 2 my $req = $app->req;
88 2 my $routes = $app->routes->routes;
89  
90 2 ▶ my @content_types = $app->format
Branch: 1 branch
BranchTrueFalseTotal
$app->format ? : 0 2 2
91 ? $app->format
92 : qw(application/x-yaml application/json);
93  
94 2 my $base_path = $req->base->as_string;
95 ### Respect proxied requests
96 # A proxy map is used to fill the "basePath" attribute.
97 2 ▶ my $_base = $req->env->{HTTP_X_FORWARDED_SCRIPT_NAME} || q(/);
Condition50%1/2
$req->env->{'HTTP_X_FORWARDED_SCRIPT_NAME'} || '/'
l!l
02
MC/DC0%0/1
$req->env->{'HTTP_X_FORWARDED_SCRIPT_NAME'} || '/'
$req->env->{'HTTP_X_FORWARDED_SCRIPT_NAME'}
Truth table50%1/2
A
A= $req->env->{'HTTP_X_FORWARDED_SCRIPT_NAME'}
Aresult
11
00
98 2 $base_path =~ s#http(?:s?)://[^/]+/#$_base#msix;
99  
100 2 $DEFAULTS{consumes} = \@content_types;
101 2 $DEFAULTS{produces} = \@content_types;
102  
103 my %spec = (
104 swagger => '2.0',
105 info => _info_object($app),
106 ### Respect proxied requests
107 # The frontend hostname is used if set.
108 host => $req->env->{HTTP_X_FORWARDED_HOST}
109 || $req->env->{SERVER_NAME}
110 || $req->env->{HTTP_HOST},
111 2 ▶ basePath => $base_path,
Condition33%2/6
$req->env->{'HTTP_X_FORWARDED_HOST'} || $req->env->{'SERVER_NAME'} || $req->env->{'HTTP_HOST'}
l!l&&r!l&&!r
200
$req->env->{"HTTP_X_FORWARDED_HOST"} or $req->env->{"SERVER_NAME"}
l!l&&r!l&&!r
020
MC/DC0%0/3
$req->env->{'HTTP_X_FORWARDED_HOST'} || $req->env->{'SERVER_NAME'} || $req->env->{'HTTP_HOST'}
$req->env->{"HTTP_X_FORWARDED_HOST"} $req->env->{"SERVER_NAME"} $req->env->{'HTTP_HOST'}
Truth table25%1/4
A or B || C
A= $req->env->{"HTTP_X_FORWARDED_HOST"}B= $req->env->{"SERVER_NAME"}C= $req->env->{'HTTP_HOST'}
ABCresult
1XX1
01X1
0011
0000
112 schemes => [$req->scheme],
113 consumes => \@content_types,
114 produces => \@content_types,
115 paths => _paths_object($routes),
116 definitions => _definitions_object($routes),
117 #parameters => undef,
118 #responses => undef,
119 securityDefinitions => _security_definitions_object(),
120 security => _security_object(),
121 #tags => undef,
122 #externalDocs => undef,
123 );
124  
125 2 my $tags = _tags_object($self->app);
126 2 ▶ if (scalar @$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$tags) 0 2 2
127 0 $spec{tags} = $tags;
128 }
129  
130 # routes
131 $self->app->add_route(
132 method => 'GET',
133 path => '/swagger',
134 0 ▶ code => sub { \%spec }
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 2 );
136  
137 # mark as built
138 2 $self->{built} = 1;
139  
140 2 \%spec;
141 }
142  
143 2 ▶sub _contact_object {
Subroutine _contact_object: called CC=3 SCAR=11.1
144 2 my $contact = shift;
145 2 my %obj;
146 2 for (qw(name url email)) {
147 6 ▶ $obj{$_} = $contact->{$_} if $contact->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $contact->{$_} 6 0 6
148 }
149 2 \%obj;
150 }
151  
152 2 ▶sub _license_object {
Subroutine _license_object: called CC=2 SCAR=7.0
153 2 my $license = shift;
154 my %obj = (
155 name => $license->{name},
156 2 );
157 2 ▶ $obj{url} = $license->{url} if $license->{url};
Branch: 1 branch
BranchTrueFalseTotal
if $license->{"url"} 2 0 2
158 2 \%obj;
159 }
160  
161 4 ▶sub _info_object {
Subroutine _info_object: called CC=7 SCAR=19.5
162 4 my $app = shift;
163  
164 my %obj = (
165 4 ▶ title => $SETTINGS{title} || 'API',
Condition100%4/4
$SETTINGS{'title'} || 'API'
l!l
22
$app->api_version || '0.0.1'
l!l
22
MC/DC100%1/1
$SETTINGS{'title'} || 'API'
$SETTINGS{'title'}
MC/DC100%1/1
$app->api_version || '0.0.1'
$app->api_version
Truth table100%2/2
A
A= $SETTINGS{'title'}
Aresult
11
00
Truth table100%2/2
A
A= $app->api_version
Aresult
11
00
166 version => $app->api_version || '0.0.1',
167 );
168  
169 4 ▶ $obj{description} = $SETTINGS{description} if $SETTINGS{description};
Branch: 1 branch
BranchTrueFalseTotal
if $SETTINGS{"description"} 2 2 4
170 4 ▶ $obj{termsOfService} = $SETTINGS{terms_of_service} if $SETTINGS{terms_of_service};
Branch: 1 branch
BranchTrueFalseTotal
if $SETTINGS{"terms_of_service"} 1 3 4
171  
172 4 ▶ $obj{contact} = _contact_object($SETTINGS{contact}) if keys %{ $SETTINGS{contact} };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$SETTINGS{"contact"};} 2 2 4
173 4 ▶ $obj{license} = _license_object($SETTINGS{license}) if keys %{ $SETTINGS{license} };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$SETTINGS{"license"};} 2 2 4
174  
175 4 \%obj;
176 }
177  
178 2 ▶sub _security_object {
Subroutine _security_object: called CC=2 SCAR=9.2
179 0 ▶ my @obj = map { { $_->{name} => $_->{scopes} || [] } } values %{ $SETTINGS{security} };
Condition0%0/2
$_->{'scopes'} || []
l!l
00
MC/DC0%0/1
$_->{'scopes'} || []
$_->{'scopes'}
Truth table0%0/2
A
A= $_->{'scopes'}
Aresult
11
00
180 2 \@obj;
181 }
182  
183 2 ▶sub _security_definitions_object { $SETTINGS{security} || {} }
Subroutine _security_definitions_object: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$SETTINGS{"security"} or {} 0 2 2
184  
185 2 ▶sub _paths_object {
Subroutine _paths_object: called CC=3 SCAR=11.0
186 2 my $routes = shift;
187  
188 2 my %obj;
189 2 for my $r (sort { $a->path cmp $b->path } @$routes) {
190 26 ▶ next if lc($r->method) eq 'options';
Branch: 1 branch
BranchTrueFalseTotal
if lc $r->method eq "options" 6 20 26
191  
192 20 my $path = $r->path;
193 20 $path =~ s#:([^/]+)#{$1}#msixg;
194  
195 20 $obj{ $path }{ lc($r->method) } = _operation_object($r);
196 }
197  
198 2 \%obj;
199 }
200  
201 20 ▶sub _operation_object {
Subroutine _operation_object: called CC=5 SCAR=16.3
202 20 my $r = shift;
203  
204 20 my $path = $r->path;
205 20 $path =~ tr#/:#_#;
206 20 my $operation_id = lc($r->method) . $path;
207  
208 my %obj = (
209 consumes => $DEFAULTS{consumes},
210 #deprecated => 'false',
211 description => $r->desc || '',
212 #externalDocs => '',
213 operationId => $operation_id,
214 produces => $r->produces || $DEFAULTS{produces},
215 responses => {
216 default => {
217 description => 'Unexpected error',
218 #examples => '',
219 #headers => '',
220 #schema => '',
221 },
222 # Adds a response object from route's entity if it exists
223 20 ▶ %{ _response_object($r) },
Condition57%4/7
$r->desc || ''
l!l
020
$r->produces || $DEFAULTS{'produces'}
l!l&&r!l&&!r
0200
$r->summary || ''
l!l
164
MC/DC0%0/1
$r->desc || ''
$r->desc
MC/DC0%0/2
$r->produces || $DEFAULTS{'produces'}
$r->produces $DEFAULTS{'produces'}
MC/DC100%1/1
$r->summary || ''
$r->summary
Truth table50%1/2
A
A= $r->desc
Aresult
11
00
Truth table33%1/3
A || B
A= $r->producesB= $DEFAULTS{'produces'}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $r->summary
Aresult
11
00
224 },
225 #schemes => [],
226 #security => {}, # TODO per operation permissions
227 summary => $r->summary || '',
228 tags => $r->tags,
229 );
230  
231 20 my $params = _parameters_object($r->method, $r->params);
232 20 ▶ $obj{parameters} = $params if scalar @$params;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$params 19 1 20
233  
234 20 \%obj;
235 }
236  
237 21 ▶sub _response_object {
Subroutine _response_object: called CC=4 SCAR=14.5
238 21 my $r = shift;
239 21 ▶ return {} unless $r->entity;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->entity 20 1 21
240  
241 1 my $name = $r->entity;
242  
243 1 ▶ my %obj = (
Condition40%2/5
$r->desc || $r->summary || ''
l!l
10
$r->desc or $r->summary
l!l&&r!l&&!r
100
MC/DC0%0/2
$r->desc || $r->summary || ''
$r->desc $r->summary
Truth table33%1/3
A or B
A= $r->descB= $r->summary
ABresult
1X1
011
000
244 $HTTP_OK => {
245 description => $r->desc || $r->summary || '',
246 schema => {
247 '$ref' => sprintf('#/definitions/%s', _name_for_object($name)),
248 }
249 },
250 );
251  
252 1 \%obj;
253 }
254  
255 28 ▶sub _parameters_object {
Subroutine _parameters_object: called CC=11 SCAR=24.0
256 28 my ($method, $pp) = @_;
257  
258 28 my @obj;
259 28 for my $p (@$pp) {
260 43 my ($type) = _param_type($p->type);
261  
262 # Available: query, header, path, formData or body
263 43 my $location = do {
264 43 ▶ if ($p->in) { $p->in }
Branch: 4 branches
BranchTrueFalseTotal
if ($p->in) { } 2 41 43
elsif ($p->named) { } 14 27 41
elsif ($type eq 'object') { } 1 26 27
elsif ($method =~ /patch|post|put/i) { } 17 9 26
265 14 elsif ($p->named) { 'path' }
266 1 elsif ($type eq 'object') { 'body' }
267 17 elsif ($method =~ /patch|post|put/i) { 'formData' }
268 9 else { 'query' }
269 };
270  
271 43 my $ptype = _param_type_object($p);
272 43 ▶ if (_type_name($p->type) =~ /^HashRef$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_type_name($p->type) =~ /^HashRef$/) 1 42 43
273 1 $ptype->{schema}{'$ref'} = delete $ptype->{'$ref'};
274 }
275  
276 # If the type is an Enum, set type to string and give the enum values.
277 43 ▶ if (_type_is_enum($p->type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_type_is_enum($p->type)) 1 42 43
278 1 $ptype->{type} = 'string';
279 1 $ptype->{enum} = $p->type->values;
280 }
281  
282 43 ▶ my %param = (
Branch: 1 branch
BranchTrueFalseTotal
$p->required ? : 31 12 43
Condition100%2/2
$p->desc || ''
l!l
2716
MC/DC100%1/1
$p->desc || ''
$p->desc
Truth table100%2/2
A
A= $p->desc
Aresult
11
00
283 description => $p->desc || '',
284 in => $location,
285 name => $p->name,
286 required => $p->required ? JSON::MaybeXS::true : JSON::MaybeXS::false,
287 %$ptype,
288 );
289 43 ▶ $param{default} = $p->default if defined $p->default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->default 11 32 43
290  
291  
292 43 push @obj, \%param;
293 }
294  
295 28 \@obj;
296 }
297  
298 3 ▶sub _definitions_object {
Subroutine _definitions_object: called CC=6 SCAR=17.9
299 3 my $routes = shift;
300 3 my @objects;
301  
302 3 for my $r (@$routes) {
303 27 ▶ if ($r->entity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->entity) 1 26 27
304 1 push @objects, $r->entity;
305 }
306  
307 27 my @pp = @{ $r->params };
308 27 ▶ while (my $p = pop @pp) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = pop @pp) 41 27 68
309 41 ▶ next unless _type_name($p->type) =~ /^HashRef$/;
Branch: 1 branch
BranchTrueFalseTotal
unless _type_name($p->type) =~ /^HashRef$/ 39 2 41
310 2 ▶ push @pp, @{ $p->enclosed || [] };
Condition50%1/2
$p->enclosed || []
l!l
20
MC/DC0%0/1
$p->enclosed || []
$p->enclosed
Truth table50%1/2
A
A= $p->enclosed
Aresult
11
00
311 2 push @objects, $p;
312 }
313 }
314  
315 3 my %definitions = map { %{ _schema_object($_) } }
316 _collect_nested_definitions(@objects);
317 3 \%definitions;
318 }
319  
320 5 ▶sub _collect_nested_definitions {
Subroutine _collect_nested_definitions: called CC=7 SCAR=19.5
321 5 my @objects = @_;
322 5 ▶ return () unless scalar @objects;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @objects 3 2 5
323  
324 2 my @nested;
325 2 for my $obj (@objects) {
326 4 ▶ if( $obj->can('enclosed') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("enclosed")) 4 0 4
327 4 ▶ for my $expose ( @{ $obj->enclosed || [] } ) {
Condition50%1/2
$obj->enclosed || []
l!l
40
MC/DC0%0/1
$obj->enclosed || []
$obj->enclosed
Truth table50%1/2
A
A= $obj->enclosed
Aresult
11
00
328 9 ▶ if (exists $expose->{'using'} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expose->{"using"}) 1 8 9
329 1 push @nested, $expose->{using};
330 }
331 }
332 }
333 }
334 2 push @objects, _collect_nested_definitions(@nested);
335  
336 2 return @objects;
337 }
338  
339  
340 6 ▶sub _schema_object {
Subroutine _schema_object: called CC=5 SCAR=16.2
341 6 my $p = shift;
342 6 ▶ return unless _type_name($p->type) =~ /^HashRef$/;
Branch: 1 branch
BranchTrueFalseTotal
unless _type_name($p->type) =~ /^HashRef$/ 0 6 6
343  
344 6 my (@required, %properties);
345 6 ▶ for my $pp (@{ $p->enclosed || [] }) {
Condition50%1/2
$p->enclosed || []
l!l
60
MC/DC0%0/1
$p->enclosed || []
$p->enclosed
Truth table50%1/2
A
A= $p->enclosed
Aresult
11
00
346 14 $properties{ _type_name($pp) } = _param_type_object($pp);
347  
348 14 ▶ push @required, _type_name($pp) if $pp->required;
Branch: 1 branch
BranchTrueFalseTotal
if $pp->required 10 4 14
349 }
350  
351 6 my %object = (
352 _name_for_object($p) => {
353 type => 'object',
354 required => \@required,
355 properties => \%properties,
356 }
357 );
358 6 \%object;
359 }
360  
361 2 ▶sub _tags_object {
Subroutine _tags_object: called CC=6 SCAR=22.0
362 2 my $app = shift;
363  
364 2 my %tags;
365 2 for my $r (@{ $app->routes->routes }) {
366 26 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 26 0 26
367 0 $tags{ $_ }++ for @{ $r->tags };
368 }
369  
370 2 my @tags;
371 2 for my $t (keys %tags) {
372 0 ▶ my $tag = {
Condition0%0/2
$app->resource_desc($t) || ''
l!l
00
MC/DC0%0/1
$app->resource_desc($t) || ''
$app->resource_desc($t)
Truth table0%0/2
A
A= $app->resource_desc($t)
Aresult
11
00
373 name => $t,
374 description => $app->resource_desc($t) || '',
375 #externalDocs => {
376 # description => '',
377 # url => '', #R
378 #},
379 };
380 0 push @tags, $tag;
381 }
382  
383 2 \@tags;
384 }
385  
386 # get the name of a type
387 342 ▶sub _type_name {
Subroutine _type_name: called CC=5 SCAR=25.2
388 342 my $type = shift;
389 342 ▶ if ($type && $type->can('display_name')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type and $type->can('display_name')) { } 342 0 342
elsif ($type and $type->can('name')) { } 0 0 0
Condition33%1/3
$type and $type->can("display_name")
!ll&&!rl&&r
00342
MC/DC0%0/2
$type and $type->can("display_name")
$type $type->can("display_name")
Truth table33%1/3
A and B
A= $typeB= $type->can("display_name")
ABresult
0X0
100
111
390 342 return $type->display_name;
391 }
392 ▶ elsif ($type && $type->can('name')) {
Condition0%0/3
$type and $type->can("name")
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type->can("name")
$type $type->can("name")
Truth table0%0/3
A and B
A= $typeB= $type->can("name")
ABresult
0X0
100
111
393 # fall back to name() (e.g. Moose types do not have display_name)
394 0 return $type->name;
395 }
396 else {
397 0 return "$type";
398 }
399 }
400  
401 57 ▶sub _param_type_object {
Subroutine _param_type_object: called CC=19 SCAR=30.4
402 57 my $p = shift;
403 57 my %item;
404  
405 57 my $tt = $p->type;
406  
407  
408 57 ▶ if (_type_name($tt) =~ /^Maybe\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if (_type_name($tt) =~ /^Maybe\[/) 2 55 57
409 2 $item{nullable} = JSON::MaybeXS::true;
410 2 $tt = $tt->type_parameter;
411 }
412  
413 57 ▶ if (_type_name($tt) =~ /^HashRef$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if (_type_name($tt) =~ /^HashRef$/) { } 3 54 57
elsif (_type_name($tt) =~ /^HashRef\[.*\]$/) { } 0 54 54
elsif (_type_name($tt) =~ /^ArrayRef/) { } 3 51 54
414 3 ▶ $item{'$ref'} = sprintf('#/definitions/%s', _name_for_object($p->can('using')?$p->using:$p));
Branch: 1 branch
BranchTrueFalseTotal
$p->can('using') ? : 0 3 3
415 }
416 elsif (_type_name($tt) =~ /^HashRef\[.*\]$/) {
417 0 $item{'type'} = 'object';
418 0 $item{'additionalProperties'} = {
419 '$ref' => sprintf('#/definitions/%s', _name_for_object($p->using))
420 };
421 }
422 elsif (_type_name($tt) =~ /^ArrayRef/) {
423 3 $item{type} = 'array';
424  
425 3 my $type;
426 my $format;
427  
428 # Loop down to find type beneath coercion.
429 3 ▶ while (!defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 4 3 7
430 4 ▶ if($tt->can('type_parameter')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt->can('type_parameter')) { } 4 0 4
431 4 ($type, $format) = _param_type($tt->type_parameter);
432 }
433 else {
434 0 ($type, $format) = ('object', '' );
435 }
436 4 ▶ $tt = $tt->parent if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 3 4
437 }
438  
439 3 ▶ if ($type eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'object') { } 2 1 3
440 2 $item{items} = {}; # {} is the "any-type" schema.
441 2 ▶ if ($p->can('using') && $p->using) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->can('using') and $p->using) { } 1 1 2
elsif ($tt->can('type_parameter')) { } 1 0 1
Condition66%2/3
$p->can("using") and $p->using
!ll&&!rl&&r
101
MC/DC50%1/2
$p->can("using") and $p->using
$p->can("using") $p->using
Truth table66%2/3
A and B
A= $p->can("using")B= $p->using
ABresult
0X0
100
111
442 1 $item{items}{'$ref'} = sprintf('#/definitions/%s', _name_for_object($p->using));
443 }
444 elsif ($tt->can("type_parameter")) {
445 1 my ($subscript_type, $subscript_format) = _param_type($tt->type_parameter);
446 1 ▶ if (defined $subscript_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subscript_type) 1 0 1
447 1 $item{items}->{type} = $subscript_type;
448 1 ▶ $item{items}->{format} = $subscript_format if defined $subscript_format;
Branch: 1 branch
BranchTrueFalseTotal
if defined $subscript_format 0 1 1
449 }
450 }
451 }
452 else {
453 1 $item{items}->{type} = $type;
454 1 ▶ $item{items}->{format} = $format if $format;
Branch: 1 branch
BranchTrueFalseTotal
if $format 0 1 1
455 1 ▶ $item{description} = $p->desc if $p->desc;
Branch: 1 branch
BranchTrueFalseTotal
if $p->desc 0 1 1
456 }
457 }
458 else {
459 51 my ($type, $format) = _param_type($tt);
460 51 $item{type} = $type;
461 51 ▶ $item{format} = $format if $format;
Branch: 1 branch
BranchTrueFalseTotal
if $format 17 34 51
462 51 ▶ $item{description} = $p->desc if $p->desc;
Branch: 1 branch
BranchTrueFalseTotal
if $p->desc 27 24 51
463 }
464 57 \%item;
465 }
466  
467 99 ▶sub _param_type {
Subroutine _param_type: called CC=15 SCAR=32.2
468 99 my $t = shift;
469 99 ▶ if ($t && $t->can('name')) { # allow nested types as Str in ArrayRef[Str]
Branch: 1 branch
BranchTrueFalseTotal
if ($t and $t->can('name')) { } 98 1 99
Condition66%2/3
$t and $t->can("name")
!ll&&!rl&&r
1098
MC/DC50%1/2
$t and $t->can("name")
$t $t->can("name")
Truth table66%2/3
A and B
A= $tB= $t->can("name")
ABresult
0X0
100
111
470 98 ▶ if ($t->name =~ /int/i) { 'integer', 'int32' }
Branch: 11 branches
BranchTrueFalseTotal
if ($t->name =~ /int/i) { } 34 64 98
elsif ($t->name =~ /long/i) { } 0 64 64
elsif ($t->name =~ /num|float|real/i) { } 0 64 64
elsif ($t->name =~ /double/i) { } 0 64 64
elsif ($t->name =~ /str/i) { } 54 10 64
elsif ($t->name =~ /byte/i) { } 0 10 10
elsif ($t->name =~ /bool/i) { } 0 10 10
elsif ($t->name =~ /datetime/i) { } 0 10 10
elsif ($t->name =~ /date/i) { } 0 10 10
elsif ($t->name =~ /password/i) { } 0 10 10
elsif ($t->name =~ /hashref/i) { } 4 6 10
471 0 elsif ($t->name =~ /long/i) { 'integer', 'int64' }
472 0 elsif ($t->name =~ /num|float|real/i) { 'number', 'float' }
473 0 elsif ($t->name =~ /double/i) { 'number', 'double' }
474 54 elsif ($t->name =~ /str/i) { 'string', undef }
475 0 elsif ($t->name =~ /byte/i) { 'string', 'byte' }
476 0 elsif ($t->name =~ /bool/i) { 'boolean', undef }
477 0 elsif ($t->name =~ /datetime/i) { 'string', 'date-time' }
478 0 elsif ($t->name =~ /date/i) { 'string', 'date' }
479 0 elsif ($t->name =~ /password/i) { 'string', 'password' }
480 4 elsif ($t->name =~ /hashref/i) { 'object', undef }
481 else {
482 6 ▶ if (_type_name($t) =~ /ArrayRef/) { 'array', undef }
Branch: 1 branch
BranchTrueFalseTotal
if (_type_name($t) =~ /ArrayRef/) { } 2 4 6
483 4 else { 'object', undef } # fallback
484 }
485 }
486 else {
487 1 { $t, undef }
488 }
489 }
490  
491 12 ▶sub _name_for_object {
Subroutine _name_for_object: called CC=1 SCAR=0.0
492 12 my $obj = shift;
493  
494 12 local $Data::Dumper::Deparse = 1;
495 12 local $Data::Dumper::Indent = 0;
496 12 local $Data::Dumper::Maxdepth = 2;
497 12 local $Data::Dumper::Purity = 0;
498 12 local $Data::Dumper::Sortkeys = 1;
499 12 local $Data::Dumper::Terse = 1;
500  
501 12 my $fingerprint = md5_hex(Data::Dumper->Dump([$obj], [qw/obj/]));
502 12 my $objname = ucfirst($obj->name);
503 #--- $ref values must be RFC3986 compliant URIs ---
504 12 $objname =~ s/::/-/g;
505 12 sprintf '%s-%s', $objname, uc(substr($fingerprint, 0, 10));
506 }
507  
508 43 ▶sub _type_is_enum {
Subroutine _type_is_enum: called CC=3 SCAR=11.0
509 43 my $type = shift;
510  
511 43 ▶ return 1 if $type->isa('Moose::Meta::TypeConstraint::Enum')
Branch: 1 branch
BranchTrueFalseTotal
if $type->isa("Moose::Meta::TypeConstraint::Enum") or $type->isa("Type::Tiny::Enum") 1 42 43
Condition66%2/3
$type->isa("Moose::Meta::TypeConstraint::Enum") or $type->isa("Type::Tiny::Enum")
l!l&&r!l&&!r
0142
MC/DC50%1/2
$type->isa("Moose::Meta::TypeConstraint::Enum") or $type->isa("Type::Tiny::Enum")
$type->isa("Moose::Meta::TypeConstraint::Enum") $type->isa("Type::Tiny::Enum")
Truth table66%2/3
A or B
A= $type->isa("Moose::Meta::TypeConstraint::Enum")B= $type->isa("Type::Tiny::Enum")
ABresult
1X1
011
000
512 or $type->isa('Type::Tiny::Enum');
513  
514 42 return 0;
515 }
516  
517 6 1;
518  
519 __END__
« blib/lib/Raisin/Entity.pm ↑ Summary blib/lib/Raisin.pm »