| 1 | package Dancer::Plugin::Swagger::Path; | ||||||||||||||||||||||||||||
| 2 | 4 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Internal representation of a swagger path | ||||||||||||||||||||||||||||
| 4 | 4 | $Dancer::Plugin::Swagger::Path::VERSION = '0.2.1'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use MooseX::MungeHas 'is_ro'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Hash::Merge; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Clone 'clone'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use List::AllUtils qw/ first any none /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Class::Load qw/ load_class /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | has route => ( handles => [ 'pattern' ] ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | has tags => ( predicate => 1 ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | has method => sub { | ||||||||||||||||||||||||||||
| 25 | 8 | ▶ | eval { $_[0]->route->method } | ||||||||||||||||||||||||||
|
Subroutine
_build_method: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | or croak "no route or explicit method provided to path"; | ||||||||||||||||||||||||||||
| 27 | 4 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has path => sub { | ||||||||||||||||||||||||||||
| 30 | 7 | ▶ | dancer_pattern_to_swagger_path( $_[0]->route->pattern ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 4 | }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | has responses => ( predicate => 1); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | has description => ( predicate => 1 ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has parameters => | ||||||||||||||||||||||||||||
| 38 | lazy => 1, | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 4 | predicate => 1, | |||||||||||||||||||||||||||
| 41 | ; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # TODO allow to pass a hashref instead of an arrayref | ||||||||||||||||||||||||||||
| 44 | sub parameter { | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my( $self, $param, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parameter: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $args ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args ||= {}
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
| |||||||||||||||||||||||||||||
| 48 | 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
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $p = first { $_->{name} eq $param } @{ $self->parameters }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | push @{ $self->parameters || [] }, $p = { name => $param } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | unless $p; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | %$p = %{Hash::Merge::merge( $p, $args )}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub dancer_pattern_to_swagger_path { | ||||||||||||||||||||||||||||
| 59 | 8 | ▶ | my $pattern = shift; | ||||||||||||||||||||||||||
|
Subroutine
dancer_pattern_to_swagger_path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 8 | $pattern =~ s#(?<=/):(\w+)(?=/|$)#{$1}#g; | |||||||||||||||||||||||||||
| 61 | 8 | return $pattern; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub add_to_doc { | ||||||||||||||||||||||||||||
| 65 | 8 | ▶ | my( $self, $doc ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_to_doc: called CC=10 SCAR=25.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | my $path = $self->path; | |||||||||||||||||||||||||||
| 68 | 8 | my $method = $self->method; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # already there | ||||||||||||||||||||||||||||
| 71 | 8 | ▶ | next if $doc->{paths}{$path}{$method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 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}
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 8 | ▶ | $m->{description} = $self->description if $self->has_description; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 8 | ▶ | $m->{parameters} = $self->parameters if $self->has_parameters; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 8 | ▶ | $m->{tags} = $self->tags if $self->has_tags; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 8 | ▶ | if( $self->has_responses ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | $m->{responses} = clone $self->responses; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | for my $r ( values %{$m->{responses}} ) { | |||||||||||||||||||||||||||
| 83 | 1 | delete $r->{template}; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if( my $example = delete $r->{example} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | my $serializer = Dancer::engine('serializer'); | |||||||||||||||||||||||||||
| 87 | die "Don't know content type for serializer ", ref $serializer | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if none { $serializer->isa($_) } qw/ Dancer::Serializer::JSON Dancer::Serializer::YAML /; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $r->{examples}{$serializer->content_type} = $example; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub validate_response { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my( $self, $code, $data, $strict ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_response: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my $schema = $self->responses->{$code}{schema}; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 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
| |||||||||||||||||||||||||||||
| 103 | unless $schema or not $strict; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return unless $schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my $plugin = Dancer::Plugin::Swagger->instance; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | $schema = { | ||||||||||||||||||||||||||||
| 110 | definitions => $plugin->doc->{definitions}, | ||||||||||||||||||||||||||||
| 111 | 0 | properties => { response => $schema }, | |||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $result = load_class('JSON::Schema::AsType')->new( schema => $schema)->validate_explain({ response => $data }); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | return unless $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | die join "\n", map { "* " . $_ } @$result; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub BUILD { | ||||||||||||||||||||||||||||
| 122 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 8 | for my $param ( eval { @{ $self->route->{_params} } } ) { | |||||||||||||||||||||||||||
| 125 | 0 | $self->parameter( $param => { | |||||||||||||||||||||||||||
| 126 | in => 'path', | ||||||||||||||||||||||||||||
| 127 | required => JSON::true, | ||||||||||||||||||||||||||||
| 128 | type => "string", | ||||||||||||||||||||||||||||
| 129 | } ); | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =pod | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 NAME | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Dancer::Plugin::Swagger::Path - Internal representation of a swagger path | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 VERSION | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | version 0.2.1 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Objects of this class are used by L<Dancer::Plugin::Swagger> to represent | ||||||||||||||||||||||||||||
| 152 | a path in the Swagger document. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | This software is copyright (c) 2021, 2016, 2015 by Yanick Champoux. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 163 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||