| 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
| |||||||||||||||||||||||||||||
| 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 ||= {}
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $args->{name} ||= $param; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"name"} ||= $param
MC/DC0%0/2
$args->{"name"} ||= $param
$args->{"name"}
$param
Truth table0%0/3
A ||= B
A= $args->{"name"}B= $param
| |||||||||||||||||||||||||||||
| 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
Condition0%0/2
$self->parameters || []
MC/DC0%0/1
$self->parameters || []
$self->parameters
Truth table0%0/2
A
A= $self->parameters
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 8 | ▶ | my $m = $doc->{paths}{$path}{$method} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$doc->{"paths"}{$path}{$method} ||= {}
MC/DC0%0/1
$doc->{"paths"}{$path}{$method} ||= {}
$doc->{"paths"}{$path}{$method}
Truth table50%1/2
A
A= $doc->{"paths"}{$path}{$method}
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 8 | ▶ | $m->{description} = $self->description if $self->has_description; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 8 | ▶ | $m->{parameters} = $self->parameters if $self->has_parameters; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 8 | ▶ | $m->{tags} = $self->tags if $self->has_tags; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 8 | ▶ | if( $self->has_responses ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$schema or not $strict
MC/DC0%0/2
$schema or not $strict
$schema
not $strict
Truth table0%0/3
A or not(B)
A= $schemaB= not $strict
| |||||||||||||||||||||||||||||
| 106 | unless $schema or not $strict; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return unless $schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||