OpenAPI/Path.pm

filter: Statementstmt 69.8% 65 / 93 Branchbran 38.4% 10 / 26 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 71.4% 15 / 21 Podpod 0.0% 0 / 5 totaltotal 55.4% 91 / 164 CC 21 SCAR 24.0
CC
21
Cov
58%
parameter
30.0
validate_response
30.0
add_to_doc
12.5
CRAP 11.0
↑ Summary OpenAPI.pm »
1 package Dancer2::Plugin::OpenAPI::Path;
2 5 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Internal representation of a OpenAPI path
4 5 $Dancer2::Plugin::OpenAPI::Path::VERSION = '1.0.2';
5  
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use MooseX::MungeHas 'is_ro';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use List::AllUtils qw/ first any none /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Class::Load qw/ load_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 has route => ( handles => [ 'pattern' ] );
21  
22 5 has tags => ( predicate => 1 );
23  
24 5 has plugin => ();
25  
26 0 ▶has method => sub {
Subroutine _build_method: not called CC=2 SCAR=12.4
27 0 ▶ eval { $_[0]->route->method }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $_[0]->route->method } } or croak("no route or explicit method provided to path") 0 0 0
eval { do { $_[0]->route->method } } or croak("no route or explicit method provided to path") 8 0 8
28 or croak "no route or explicit method provided to path";
29 5 };
30  
31 7 ▶has path => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_path: called CC=1 SCAR=0.0
32 7 dancer_pattern_to_swagger_path( $_[0]->route->spec_route );
33 5 };
34  
35 5 has responses => ( predicate => 1);
36  
37 5 has description => ( predicate => 1 );
38  
39 has parameters =>
40 lazy => 1,
41 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 5 predicate => 1,
43 ;
44  
45 # TODO allow to pass a hashref instead of an arrayref
46 0 ▶sub parameter {
Subroutine parameter: not called CC=5 SCAR=34.0
Pod undocumented
47 0 my( $self, $param, $args ) = @_;
48  
49 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
50 0 ▶ $args->{name} ||= $param;
Condition0%0/3
$args->{"name"} ||= $param
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"name"} ||= $param
$args->{"name"} $param
Truth table0%0/3
A ||= B
A= $args->{"name"}B= $param
ABresult
1X1
011
000
51  
52 0 ▶ my $p = first { $_->{name} eq $param } @{ $self->parameters };
Subroutine __ANON__: not called CC=1 SCAR=6.9
53
54 0 ▶ push @{ $self->parameters || [] }, $p = { name => $param }
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
Condition0%0/2
$self->parameters || []
l!l
00
MC/DC0%0/1
$self->parameters || []
$self->parameters
Truth table0%0/2
A
A= $self->parameters
Aresult
11
00
55 unless $p;
56  
57 0 %$p = %{Hash::Merge::merge( $p, $args )};
58 }
59  
60 8 ▶sub dancer_pattern_to_swagger_path {
Subroutine dancer_pattern_to_swagger_path: called CC=1 SCAR=0.0
Pod undocumented
61 8 my $pattern = shift;
62 8 $pattern =~ s#(?<=/):(\w+)(?=/|$)#{$1}#g;
63 8 return $pattern;
64 }
65  
66 8 ▶sub add_to_doc {
Subroutine add_to_doc: called CC=10 SCAR=25.3
Pod undocumented
67 8 my( $self, $doc ) = @_;
68  
69 8 my $path = $self->path;
70 8 my $method = $self->method;
71  
72 # already there
73 8 ▶ next if $doc->{paths}{$path}{$method};
Branch: 1 branch
BranchTrueFalseTotal
if $doc->{"paths"}{$path}{$method} 0 8 8
74  
75 8 ▶ my $m = $doc->{paths}{$path}{$method} ||= {};
Condition50%1/2
$doc->{"paths"}{$path}{$method} ||= {}
l!l
08
MC/DC0%0/1
$doc->{"paths"}{$path}{$method} ||= {}
$doc->{"paths"}{$path}{$method}
Truth table50%1/2
A
A= $doc->{"paths"}{$path}{$method}
Aresult
11
00
76  
77 8 ▶ $m->{description} = $self->description if $self->has_description;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_description 2 6 8
78 8 ▶ $m->{parameters} = $self->parameters if $self->has_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_parameters 4 4 8
79 8 ▶ $m->{tags} = $self->tags if $self->has_tags;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_tags 0 8 8
80  
81 8 ▶ if( $self->has_responses ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_responses) 1 7 8
82 1 $m->{responses} = clone $self->responses;
83  
84 1 for my $r ( values %{$m->{responses}} ) {
85 1 delete $r->{template};
86  
87 1 ▶ if( my $example = delete $r->{example} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $example = delete $r->{"example"}) 0 1 1
88 0 my $serializer =
89 $self->plugin->app->serializer_engine;
90 die "Don't know content type for serializer ", ref $serializer
91 0 ▶ if none { $serializer->isa($_) } qw/ Dancer2::Serializer::JSON Dancer2::Serializer::YAML /;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $serializer->isa($_); } , "Dancer2::Serializer::JSON", "Dancer2::Serializer::YAML") 0 0 0
92 0 $r->{examples}{$serializer->content_type} = $example;
93 }
94 }
95 }
96  
97  
98 }
99  
100 0 ▶sub validate_response {
Subroutine validate_response: not called CC=5 SCAR=34.0
Pod undocumented
101 0 my( $self, $code, $data, $strict ) = @_;
102  
103 0 my $schema = $self->responses->{$code}{schema};
104  
105 0 ▶ die "no schema found for return code $code for ", join ' ' , uc($self->method), $self->path
Branch: 1 branch
BranchTrueFalseTotal
unless $schema or not $strict 0 0 0
Condition0%0/3
$schema or not $strict
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema or not $strict
$schema not $strict
Truth table0%0/3
A or not(B)
A= $schemaB= not $strict
ABresult
1X1
011
000
106 unless $schema or not $strict;
107  
108 0 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
109  
110 0 my $plugin = Dancer2::Plugin::OpenAPI->instance;
111  
112 $schema = {
113 definitions => $plugin->doc->{definitions},
114 0 properties => { response => $schema },
115 };
116  
117 0 my $result = load_class('JSON::Schema::AsType')->new( schema => $schema)->validate_explain({ response => $data });
118  
119 0 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
120  
121 0 die join "\n", map { "* " . $_ } @$result;
122 }
123  
124 9 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.1
Pod undocumented
125 9 my $self = shift;
126  
127 9 for my $param ( eval { @{ $self->route->{_params} } } ) {
128 0 $self->parameter( $param => {
129 in => 'path',
130 required => JSON::true,
131 type => "string",
132 } );
133 }
134 }
135  
136 5 1;
137  
138 __END__
↑ Summary OpenAPI.pm »