| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Dancer::Plugin::Swagger::Path; |
|
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: Internal representation of a swagger path |
|
4
|
|
|
|
|
|
|
$Dancer::Plugin::Swagger::Path::VERSION = '0.2.1'; |
|
5
|
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
15
|
use strict; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
69
|
|
|
7
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
52
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
1271
|
use Moo; |
|
|
2
|
|
|
|
|
18587
|
|
|
|
2
|
|
|
|
|
12
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
4389
|
use MooseX::MungeHas 'is_ro'; |
|
|
2
|
|
|
|
|
8504
|
|
|
|
2
|
|
|
|
|
16
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
1912
|
use Carp; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
135
|
|
|
14
|
2
|
|
|
2
|
|
1269
|
use Hash::Merge; |
|
|
2
|
|
|
|
|
9736
|
|
|
|
2
|
|
|
|
|
116
|
|
|
15
|
2
|
|
|
2
|
|
20
|
use Clone 'clone'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
119
|
|
|
16
|
2
|
|
|
2
|
|
1594
|
use List::AllUtils qw/ first any none /; |
|
|
2
|
|
|
|
|
27677
|
|
|
|
2
|
|
|
|
|
232
|
|
|
17
|
2
|
|
|
2
|
|
1544
|
use JSON; |
|
|
2
|
|
|
|
|
22539
|
|
|
|
2
|
|
|
|
|
17
|
|
|
18
|
2
|
|
|
2
|
|
1428
|
use Class::Load qw/ load_class /; |
|
|
2
|
|
|
|
|
22522
|
|
|
|
2
|
|
|
|
|
1889
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has route => ( handles => [ 'pattern' ] ); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has tags => ( predicate => 1 ); |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has method => sub { |
|
25
|
8
|
50
|
|
8
|
|
89
|
eval { $_[0]->route->method } |
|
|
8
|
|
|
|
|
45
|
|
|
26
|
|
|
|
|
|
|
or croak "no route or explicit method provided to path"; |
|
27
|
|
|
|
|
|
|
}; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has path => sub { |
|
30
|
8
|
|
|
8
|
|
104
|
dancer_pattern_to_swagger_path( $_[0]->route->pattern ); |
|
31
|
|
|
|
|
|
|
}; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has responses => ( predicate => 1); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has description => ( predicate => 1 ); |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has parameters => |
|
38
|
|
|
|
|
|
|
lazy => 1, |
|
39
|
|
|
|
|
|
|
default => sub { [] }, |
|
40
|
|
|
|
|
|
|
predicate => 1, |
|
41
|
|
|
|
|
|
|
; |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# TODO allow to pass a hashref instead of an arrayref |
|
44
|
|
|
|
|
|
|
sub parameter { |
|
45
|
0
|
|
|
0
|
0
|
0
|
my( $self, $param, $args ) = @_; |
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
|
0
|
|
|
0
|
$args ||= {}; |
|
48
|
0
|
|
0
|
|
|
0
|
$args->{name} ||= $param; |
|
49
|
|
|
|
|
|
|
|
|
50
|
0
|
|
|
0
|
|
0
|
my $p = first { $_->{name} eq $param } @{ $self->parameters }; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
0
|
push @{ $self->parameters || [] }, $p = { name => $param } |
|
|
0
|
0
|
|
|
|
0
|
|
|
53
|
|
|
|
|
|
|
unless $p; |
|
54
|
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
0
|
%$p = %{Hash::Merge::merge( $p, $args )}; |
|
|
0
|
|
|
|
|
0
|
|
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub dancer_pattern_to_swagger_path { |
|
59
|
8
|
|
|
8
|
0
|
84
|
my $pattern = shift; |
|
60
|
8
|
|
|
|
|
24
|
$pattern =~ s#(?<=/):(\w+)(?=/|$)#{$1}#g; |
|
61
|
8
|
|
|
|
|
32
|
return $pattern; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub add_to_doc { |
|
65
|
8
|
|
|
8
|
0
|
86
|
my( $self, $doc ) = @_; |
|
66
|
|
|
|
|
|
|
|
|
67
|
8
|
|
|
|
|
155
|
my $path = $self->path; |
|
68
|
8
|
|
|
|
|
145
|
my $method = $self->method; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# already there |
|
71
|
8
|
50
|
|
|
|
111
|
next if $doc->{paths}{$path}{$method}; |
|
72
|
|
|
|
|
|
|
|
|
73
|
8
|
|
50
|
|
|
49
|
my $m = $doc->{paths}{$path}{$method} ||= {}; |
|
74
|
|
|
|
|
|
|
|
|
75
|
8
|
100
|
|
|
|
53
|
$m->{description} = $self->description if $self->has_description; |
|
76
|
8
|
100
|
|
|
|
97
|
$m->{parameters} = $self->parameters if $self->has_parameters; |
|
77
|
8
|
50
|
|
|
|
70
|
$m->{tags} = $self->tags if $self->has_tags; |
|
78
|
|
|
|
|
|
|
|
|
79
|
8
|
100
|
|
|
|
38
|
if( $self->has_responses ) { |
|
80
|
1
|
|
|
|
|
22
|
$m->{responses} = clone $self->responses; |
|
81
|
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
3
|
for my $r ( values %{$m->{responses}} ) { |
|
|
1
|
|
|
|
|
4
|
|
|
83
|
1
|
|
|
|
|
4
|
delete $r->{template}; |
|
84
|
|
|
|
|
|
|
|
|
85
|
1
|
50
|
|
|
|
10
|
if( my $example = delete $r->{example} ) { |
|
86
|
0
|
|
|
|
|
0
|
my $serializer = Dancer::engine('serializer'); |
|
87
|
|
|
|
|
|
|
die "Don't know content type for serializer ", ref $serializer |
|
88
|
0
|
0
|
|
0
|
|
0
|
if none { $serializer->isa($_) } qw/ Dancer::Serializer::JSON Dancer::Serializer::YAML /; |
|
|
0
|
|
|
|
|
0
|
|
|
89
|
0
|
|
|
|
|
0
|
$r->{examples}{$serializer->content_type} = $example; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub validate_response { |
|
98
|
0
|
|
|
0
|
0
|
0
|
my( $self, $code, $data, $strict ) = @_; |
|
99
|
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
my $schema = $self->responses->{$code}{schema}; |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
0
|
0
|
|
|
0
|
die "no schema found for return code $code for ", join ' ' , uc($self->method), $self->path |
|
103
|
|
|
|
|
|
|
unless $schema or not $strict; |
|
104
|
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
0
|
return unless $schema; |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
my $plugin = Dancer::Plugin::Swagger->instance; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
$schema = { |
|
110
|
|
|
|
|
|
|
definitions => $plugin->doc->{definitions}, |
|
111
|
0
|
|
|
|
|
0
|
properties => { response => $schema }, |
|
112
|
|
|
|
|
|
|
}; |
|
113
|
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
0
|
my $result = load_class('JSON::Schema::AsType')->new( schema => $schema)->validate_explain({ response => $data }); |
|
115
|
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
0
|
return unless $result; |
|
117
|
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
0
|
die join "\n", map { "* " . $_ } @$result; |
|
|
0
|
|
|
|
|
0
|
|
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub BUILD { |
|
122
|
8
|
|
|
8
|
0
|
7008
|
my $self = shift; |
|
123
|
|
|
|
|
|
|
|
|
124
|
8
|
|
|
|
|
21
|
for my $param ( eval { @{ $self->route->{_params} } } ) { |
|
|
8
|
|
|
|
|
13
|
|
|
|
8
|
|
|
|
|
66
|
|
|
125
|
0
|
|
|
|
|
|
$self->parameter( $param => { |
|
126
|
|
|
|
|
|
|
in => 'path', |
|
127
|
|
|
|
|
|
|
required => JSON::true, |
|
128
|
|
|
|
|
|
|
type => "string", |
|
129
|
|
|
|
|
|
|
} ); |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
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 |