line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Swagger2; |
2
|
29
|
|
|
29
|
|
50064
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
29
|
|
|
|
|
46
|
|
|
29
|
|
|
|
|
179
|
|
3
|
29
|
|
|
29
|
|
5669
|
use Mojo::JSON; |
|
29
|
|
|
|
|
42
|
|
|
29
|
|
|
|
|
1252
|
|
4
|
29
|
|
|
29
|
|
117
|
use Mojo::Loader; |
|
29
|
|
|
|
|
42
|
|
|
29
|
|
|
|
|
1026
|
|
5
|
29
|
|
|
29
|
|
140
|
use Mojo::Util qw(decamelize deprecated); |
|
29
|
|
|
|
|
39
|
|
|
29
|
|
|
|
|
1451
|
|
6
|
29
|
|
|
29
|
|
9205
|
use Swagger2; |
|
29
|
|
|
|
|
83
|
|
|
29
|
|
|
|
|
222
|
|
7
|
29
|
|
|
29
|
|
16096
|
use JSON::Validator::OpenAPI::Mojolicious; |
|
29
|
|
|
|
|
16490
|
|
|
29
|
|
|
|
|
1833
|
|
8
|
29
|
|
|
29
|
|
148
|
use constant DEBUG => $ENV{SWAGGER2_DEBUG}; |
|
29
|
|
|
|
|
38
|
|
|
29
|
|
|
|
|
2132
|
|
9
|
29
|
|
|
29
|
|
110
|
use constant IO_LOGGING => $ENV{SWAGGER2_IO_LOGGING}; # EXPERIMENTAL |
|
29
|
|
|
|
|
35
|
|
|
29
|
|
|
|
|
76112
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
deprecated "Mojolicious::Plugin::Swagger2 is deprecated in favor of Mojolicious::Plugin::OpenAPI"; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $SKIP_OP_RE = qr(By|From|For|In|Of|To|With); |
14
|
|
|
|
|
|
|
my $LAYOUT = Mojo::Loader::data_section(__PACKAGE__, 'layouts/mojolicious_plugin_swagger.html.ep'); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has url => ''; |
17
|
|
|
|
|
|
|
has _validator => sub { JSON::Validator::OpenAPI::Mojolicious->new; }; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub dispatch_to_swagger { |
20
|
5
|
50
|
33
|
5
|
1
|
28247
|
return undef unless $_[1]->{op} and $_[1]->{id} and ref $_[1]->{params} eq 'HASH'; |
|
|
|
33
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
5
|
|
|
|
|
8
|
my ($c, $data) = @_; |
23
|
5
|
|
|
|
|
14
|
my $self = $c->stash('swagger.plugin'); |
24
|
|
|
|
|
|
|
my $reply |
25
|
5
|
|
50
|
5
|
|
61
|
= sub { $_[0]->send({json => {code => $_[2] || 200, id => $data->{id}, body => $_[1]}}) }; |
|
5
|
|
|
|
|
47
|
|
26
|
|
|
|
|
|
|
my $defaults = $self->{route_defaults}{$data->{op}} |
27
|
5
|
100
|
|
|
|
27
|
or return $c->$reply(_error('Unknown operationId.'), 400); |
28
|
4
|
|
|
|
|
6
|
my ($e, $input, @errors); |
29
|
|
|
|
|
|
|
|
30
|
4
|
50
|
|
|
|
12
|
return $c->$reply(_error($e), 501) if $e = _find_action($c, $defaults); |
31
|
|
|
|
|
|
|
|
32
|
4
|
100
|
|
|
|
6
|
for my $p (@{$defaults->{swagger_operation_spec}{parameters} || []}) { |
|
4
|
|
|
|
|
19
|
|
33
|
2
|
|
|
|
|
5
|
my $name = $p->{name}; |
34
|
2
|
|
33
|
|
|
7
|
my $value = $data->{params}{$name} // $p->{default}; |
35
|
2
|
|
|
|
|
7
|
my @e = $self->_validator->_validate_request_value($p, $name => $value); |
36
|
2
|
100
|
|
|
|
449
|
$input->{$name} = $value unless @e; |
37
|
2
|
|
|
|
|
5
|
push @errors, @e; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
4
|
100
|
|
|
|
15
|
return $c->$reply({errors => \@errors}, 400) if @errors; |
41
|
|
|
|
|
|
|
return Mojo::IOLoop->delay( |
42
|
|
|
|
|
|
|
sub { |
43
|
3
|
|
|
3
|
|
690
|
my $delay = shift; |
44
|
3
|
|
|
|
|
6
|
my $action = $defaults->{action}; |
45
|
3
|
|
|
|
|
16
|
my $sc = $delay->data->{sc} = $defaults->{controller}->new(%$c); |
46
|
3
|
|
|
|
|
51
|
$sc->stash(swagger_operation_spec => $defaults->{swagger_operation_spec}); |
47
|
3
|
|
|
|
|
37
|
$sc->$action($input, $delay->begin); |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
sub { |
50
|
3
|
|
|
3
|
|
270
|
my $delay = shift; |
51
|
3
|
|
|
|
|
4
|
my $data = shift; |
52
|
3
|
|
50
|
|
|
7
|
my $status = shift || 200; |
53
|
|
|
|
|
|
|
my @errors = $self->_validator->validate_response($c, $defaults->{swagger_operation_spec}, |
54
|
3
|
|
|
|
|
11
|
$status, $data); |
55
|
|
|
|
|
|
|
|
56
|
3
|
100
|
|
|
|
880
|
return $c->$reply($data, $status) unless @errors; |
57
|
1
|
|
|
|
|
2
|
warn "[Swagger2] Invalid response: @errors\n" if DEBUG; |
58
|
1
|
|
|
|
|
6
|
$c->$reply({errors => \@errors}, 500); |
59
|
|
|
|
|
|
|
}, |
60
|
3
|
|
|
|
|
32
|
); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub render_swagger { |
64
|
115
|
|
|
115
|
1
|
3354
|
my ($c, $err, $data, $status) = @_; |
65
|
|
|
|
|
|
|
|
66
|
115
|
100
|
|
|
|
533
|
return $c->render(json => $err, status => $status) if %$err; |
67
|
80
|
100
|
|
|
|
568
|
return $c->render(ref $data ? (json => $data) : (text => $data), status => $status); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub register { |
71
|
41
|
|
|
41
|
1
|
81555
|
my ($self, $app, $config) = @_; |
72
|
41
|
|
|
|
|
65
|
my ($base_path, $paths, $r, $swagger); |
73
|
|
|
|
|
|
|
|
74
|
41
|
|
33
|
|
|
459
|
$swagger = $config->{swagger} || Swagger2->new->load($config->{url} || '"url" is missing'); |
75
|
41
|
|
|
|
|
161
|
$swagger = $swagger->expand; |
76
|
41
|
|
50
|
|
|
570
|
$paths = $swagger->api_spec->get('/paths') || {}; |
77
|
|
|
|
|
|
|
|
78
|
41
|
50
|
|
|
|
1311
|
$app->plugin(PODRenderer => {no_perldoc => 1}) unless $app->renderer->helpers->{pod_to_html}; |
79
|
|
|
|
|
|
|
|
80
|
41
|
50
|
50
|
|
|
453959
|
if ($config->{validate} // 1) { |
81
|
41
|
|
|
|
|
291
|
my @errors = $swagger->validate; |
82
|
41
|
50
|
|
|
|
7710405
|
die join "\n", "Swagger2: Invalid spec:", @errors if @errors; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
41
|
50
|
|
|
|
319
|
if ($app->plugins->has_subscribers('swagger_route_added')) { |
86
|
0
|
|
|
|
|
0
|
warn |
87
|
|
|
|
|
|
|
"swagger_route_added hook will be deprecated. https://github.com/jhthorsen/swagger2/issues/65"; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else { |
90
|
41
|
|
|
|
|
1250
|
$app->hook(swagger_route_added => \&_on_route_added); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
41
|
|
33
|
|
|
837
|
local $config->{coerce} = $config->{coerce} || $ENV{SWAGGER_COERCE_VALUES}; |
94
|
41
|
50
|
|
|
|
158
|
$self->_validator->coerce($config->{coerce}) if $config->{coerce}; |
95
|
41
|
|
|
|
|
231
|
$self->_validator->schema($swagger->api_spec->data); |
96
|
41
|
|
|
|
|
32458
|
$self->url($swagger->url); |
97
|
41
|
50
|
|
|
|
375
|
$app->helper(dispatch_to_swagger => \&dispatch_to_swagger) |
98
|
|
|
|
|
|
|
unless $app->renderer->get_helper('dispatch_to_swagger'); |
99
|
41
|
50
|
|
|
|
7233
|
$app->helper(render_swagger => \&render_swagger) |
100
|
|
|
|
|
|
|
unless $app->renderer->get_helper('render_swagger'); |
101
|
|
|
|
|
|
|
|
102
|
41
|
|
|
|
|
5196
|
$r = $config->{route}; |
103
|
|
|
|
|
|
|
|
104
|
41
|
50
|
66
|
|
|
191
|
if ($r and !$r->pattern->unparsed) { |
105
|
0
|
|
|
|
|
0
|
$r->to(swagger => $swagger); |
106
|
0
|
|
|
|
|
0
|
$r = $r->any($swagger->base_url->path->to_string); |
107
|
|
|
|
|
|
|
} |
108
|
41
|
100
|
|
|
|
152
|
if (!$r) { |
109
|
40
|
|
|
|
|
218
|
$r = $app->routes->any($swagger->base_url->path->to_string); |
110
|
40
|
|
|
|
|
11204
|
$r->to(swagger => $swagger); |
111
|
|
|
|
|
|
|
} |
112
|
41
|
100
|
|
|
|
881
|
if (my $ws = $config->{ws}) { |
113
|
1
|
|
|
|
|
3
|
$ws->to('swagger.plugin' => $self); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
41
|
|
|
|
|
197
|
$base_path = $swagger->api_spec->data->{basePath} = $r->to_string; |
117
|
41
|
|
|
|
|
1483
|
$base_path =~ s!/$!!; |
118
|
|
|
|
|
|
|
|
119
|
41
|
|
|
|
|
259
|
for my $path (sort { length $a <=> length $b } keys %$paths) { |
|
41
|
|
|
|
|
140
|
|
120
|
|
|
|
|
|
|
my $around_action |
121
|
74
|
|
100
|
|
|
507
|
= $paths->{$path}{'x-mojo-around-action'} || $swagger->api_spec->get('/x-mojo-around-action'); |
122
|
|
|
|
|
|
|
my $controller |
123
|
74
|
|
100
|
|
|
1799
|
= $paths->{$path}{'x-mojo-controller'} || $swagger->api_spec->get('/x-mojo-controller'); |
124
|
|
|
|
|
|
|
|
125
|
74
|
|
|
|
|
1121
|
for my $http_method (grep { !/^x-/ } keys %{$paths->{$path}}) { |
|
92
|
|
|
|
|
298
|
|
|
74
|
|
|
|
|
247
|
|
126
|
83
|
|
|
|
|
171
|
my $op_spec = $paths->{$path}{$http_method}; |
127
|
83
|
|
|
|
|
120
|
my $route_path = $path; |
128
|
83
|
100
|
|
|
|
96
|
my %parameters = map { ($_->{name}, $_) } @{$op_spec->{parameters} || []}; |
|
63
|
|
|
|
|
265
|
|
|
83
|
|
|
|
|
318
|
|
129
|
|
|
|
|
|
|
|
130
|
83
|
|
|
|
|
324
|
$route_path =~ s/{([^}]+)}/{ |
131
|
25
|
|
|
|
|
43
|
my $name = $1; |
|
25
|
|
|
|
|
77
|
|
132
|
25
|
|
100
|
|
|
175
|
my $type = $parameters{$name}{'x-mojo-placeholder'} || ':'; |
133
|
25
|
|
|
|
|
123
|
"($type$name)"; |
134
|
|
|
|
|
|
|
}/ge; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
$op_spec->{'x-mojo-around-action'} = $around_action |
137
|
83
|
100
|
100
|
|
|
418
|
if !$op_spec->{'x-mojo-around-action'} and $around_action; |
138
|
|
|
|
|
|
|
$op_spec->{'x-mojo-controller'} = $controller |
139
|
83
|
100
|
100
|
|
|
303
|
if !$op_spec->{'x-mojo-controller'} and $controller; |
140
|
83
|
|
|
|
|
297
|
$app->plugins->emit(swagger_route_added => |
141
|
|
|
|
|
|
|
$r->$http_method($route_path => $self->_generate_request_handler($op_spec))); |
142
|
83
|
|
|
|
|
797
|
warn "[Swagger2] Add route $http_method $base_path$route_path\n" if DEBUG; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
41
|
50
|
50
|
|
|
363
|
if (my $spec_path = $config->{spec_path} // '/') { |
147
|
41
|
|
|
|
|
196
|
my $title = $swagger->api_spec->get('/info/title'); |
148
|
41
|
|
|
|
|
1053
|
$title =~ s!\W!_!g; |
149
|
41
|
|
|
4
|
|
162
|
$r->get($spec_path)->to(cb => sub { _render_spec(shift, $swagger) })->name(lc $title); |
|
4
|
|
|
|
|
77591
|
|
150
|
|
|
|
|
|
|
} |
151
|
41
|
100
|
|
|
|
4449
|
if ($config->{ensure_swagger_response}) { |
152
|
2
|
|
|
|
|
8
|
$self->_ensure_swagger_response($app, $config->{ensure_swagger_response}, $swagger); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _ensure_swagger_response { |
157
|
2
|
|
|
2
|
|
4
|
my ($self, $app, $responses, $swagger) = @_; |
158
|
2
|
|
|
|
|
10
|
my $base_path = $swagger->api_spec->data->{basePath}; |
159
|
|
|
|
|
|
|
|
160
|
2
|
|
50
|
|
|
21
|
$responses->{exception} ||= 'Internal server error.'; |
161
|
2
|
|
50
|
|
|
15
|
$responses->{not_found} ||= 'Not found.'; |
162
|
2
|
|
|
|
|
27
|
$base_path = qr{^$base_path}; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
$app->hook( |
165
|
|
|
|
|
|
|
before_render => sub { |
166
|
16
|
|
|
16
|
|
42964
|
my ($c, $args) = @_; |
167
|
|
|
|
|
|
|
|
168
|
16
|
100
|
|
|
|
54
|
return unless my $template = $args->{template}; |
169
|
8
|
100
|
|
|
|
31
|
return unless my $msg = $responses->{$template}; |
170
|
3
|
100
|
|
|
|
16
|
return unless $c->req->url->path->to_string =~ $base_path; |
171
|
|
|
|
|
|
|
|
172
|
2
|
|
|
|
|
100
|
$args->{json} = _error($msg); |
173
|
|
|
|
|
|
|
} |
174
|
2
|
|
|
|
|
18
|
); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _generate_request_handler { |
178
|
83
|
|
|
83
|
|
452
|
my ($self, $op_spec) = @_; |
179
|
83
|
|
|
|
|
206
|
my $defaults = {swagger_operation_spec => $op_spec}; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
my $handler = sub { |
182
|
116
|
|
|
116
|
|
870631
|
my $c = shift; |
183
|
116
|
|
|
|
|
184
|
my ($e, @errors, %input); |
184
|
|
|
|
|
|
|
|
185
|
116
|
100
|
|
|
|
406
|
return $c->render_swagger(_error($e), {}, 501) if $e = _find_action($c, $defaults); |
186
|
114
|
|
|
|
|
672
|
$c = $defaults->{controller}->new(%$c); |
187
|
114
|
|
|
|
|
1184
|
@errors = $self->_validator->validate_request($c, $op_spec, \%input); |
188
|
|
|
|
|
|
|
|
189
|
114
|
|
|
|
|
45038
|
_io_error($c, Input => \@errors) if IO_LOGGING and @errors; |
190
|
114
|
100
|
|
|
|
515
|
return $c->render_swagger({errors => \@errors}, {}, 400) if @errors; |
191
|
|
|
|
|
|
|
return $c->delay( |
192
|
|
|
|
|
|
|
sub { |
193
|
95
|
|
|
|
|
37551
|
my $action = $defaults->{action}; |
194
|
95
|
|
|
|
|
396
|
$c->app->log->debug( |
195
|
|
|
|
|
|
|
qq(Swagger2 routing to controller "$defaults->{controller}" and action "$action")); |
196
|
95
|
|
|
|
|
2527
|
$c->$action(\%input, shift->begin); |
197
|
|
|
|
|
|
|
}, |
198
|
|
|
|
|
|
|
sub { |
199
|
94
|
|
|
|
|
9480
|
my $delay = shift; |
200
|
94
|
|
|
|
|
182
|
my $data = shift; |
201
|
94
|
|
100
|
|
|
314
|
my $status = shift || 200; |
202
|
94
|
|
|
|
|
394
|
my @errors = $self->_validator->validate_response($c, $op_spec, $status, $data); |
203
|
|
|
|
|
|
|
|
204
|
94
|
100
|
|
|
|
14146
|
return $c->render_swagger({}, $data, $status) unless @errors; |
205
|
14
|
|
|
|
|
23
|
_io_error($c, Output => \@errors) if IO_LOGGING and @errors; |
206
|
14
|
|
|
|
|
141
|
$c->render_swagger({errors => \@errors}, $data, 500); |
207
|
|
|
|
|
|
|
}, |
208
|
95
|
|
|
|
|
1539
|
); |
209
|
83
|
|
|
|
|
561
|
}; |
210
|
|
|
|
|
|
|
|
211
|
83
|
100
|
|
|
|
111
|
for my $p (@{$op_spec->{parameters} || []}) { |
|
83
|
|
|
|
|
340
|
|
212
|
63
|
100
|
100
|
|
|
325
|
$defaults->{$p->{name}} = $p->{default} if $p->{in} eq 'path' and defined $p->{default}; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
83
|
100
|
|
|
|
218
|
if (my $around_action = $op_spec->{'x-mojo-around-action'}) { |
216
|
3
|
|
|
|
|
4
|
my $next = $handler; |
217
|
|
|
|
|
|
|
$handler = sub { |
218
|
6
|
|
|
6
|
|
45077
|
my $c = shift; |
219
|
6
|
|
33
|
|
|
59
|
my $around = $c->can($around_action) || $around_action; |
220
|
6
|
|
|
|
|
21
|
$around->($next, $c, $op_spec); |
221
|
3
|
|
|
|
|
10
|
}; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
83
|
|
|
|
|
265
|
$self->{route_defaults}{$op_spec->{operationId}} = $defaults; |
225
|
83
|
|
|
|
|
389
|
return $defaults, $handler; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub _on_route_added { |
229
|
83
|
|
|
83
|
|
15849
|
my ($self, $r) = @_; |
230
|
83
|
|
|
|
|
216
|
my $op_spec = $r->pattern->defaults->{swagger_operation_spec}; |
231
|
83
|
|
|
|
|
475
|
my $controller = $op_spec->{'x-mojo-controller'}; |
232
|
83
|
|
|
|
|
85
|
my $route_name; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
$route_name = $controller |
235
|
158
|
|
|
|
|
580
|
? decamelize join '::', map { ucfirst $_ } $controller, $op_spec->{operationId} |
236
|
83
|
100
|
|
|
|
268
|
: decamelize $op_spec->{operationId}; |
237
|
|
|
|
|
|
|
|
238
|
83
|
|
|
|
|
2592
|
$route_name =~ s/\W+/_/g; |
239
|
83
|
|
|
|
|
284
|
$r->name($route_name); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
sub _render_spec { |
243
|
4
|
|
|
4
|
|
11
|
my ($c, $swagger) = @_; |
244
|
4
|
|
100
|
|
|
16
|
my $format = $c->stash('format') || 'json'; |
245
|
4
|
|
|
|
|
70
|
my $spec = $swagger->api_spec->data; |
246
|
4
|
|
|
|
|
49
|
my $url = $c->req->url->to_abs; |
247
|
|
|
|
|
|
|
|
248
|
4
|
|
|
|
|
748
|
local $spec->{id}; |
249
|
4
|
|
|
|
|
10
|
delete $spec->{id}; |
250
|
4
|
|
|
|
|
15
|
local $spec->{host} = $url->host_port; |
251
|
4
|
|
|
|
|
94
|
$swagger->base_url->host($url->host)->port($url->port); |
252
|
|
|
|
|
|
|
|
253
|
4
|
100
|
|
|
|
73
|
if ($format eq 'text') { |
|
|
100
|
|
|
|
|
|
254
|
1
|
|
|
|
|
7
|
$c->render(text => $swagger->pod->to_string); |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
elsif ($format eq 'html') { |
257
|
1
|
|
|
|
|
12
|
$c->render( |
258
|
|
|
|
|
|
|
handler => 'ep', |
259
|
|
|
|
|
|
|
inline => $LAYOUT, |
260
|
|
|
|
|
|
|
pod => $c->pod_to_html($swagger->pod->to_string) |
261
|
|
|
|
|
|
|
); |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
else { |
264
|
2
|
|
|
|
|
10
|
$c->render(json => $spec); |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
sub _error { |
269
|
5
|
|
|
5
|
|
47
|
return {errors => [{message => $_[0], path => '/'}]}; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub _find_action { |
273
|
131
|
100
|
|
131
|
|
9786
|
return if $_[1]->{controller}; # cached |
274
|
77
|
|
|
|
|
124
|
my ($c, $defaults) = @_; |
275
|
77
|
50
|
|
|
|
316
|
my $op = $defaults->{swagger_operation_spec}{operationId} or return 'operationId is missing.'; |
276
|
|
|
|
|
|
|
my $can = sub { |
277
|
|
|
|
|
|
|
$defaults->{controller}->can($defaults->{action}) |
278
|
76
|
100
|
|
76
|
|
852
|
? '' |
279
|
|
|
|
|
|
|
: qq(Method "$defaults->{action}" not implemented.); |
280
|
77
|
|
|
|
|
329
|
}; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# specify controller manually |
283
|
|
|
|
|
|
|
@$defaults{qw(action controller)} |
284
|
77
|
|
|
|
|
340
|
= _load($c, $op, $defaults->{swagger_operation_spec}{'x-mojo-controller'}); |
285
|
77
|
100
|
|
|
|
325
|
return $can->() if $defaults->{controller}; |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
# "createFileInFileSystem" = ("createFile", "FileSystem") |
288
|
16
|
|
|
|
|
128
|
@$defaults{qw(action controller)} = _load($c, split $SKIP_OP_RE, $op); |
289
|
16
|
100
|
|
|
|
47
|
return $can->() if $defaults->{controller}; |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
# "showPetById" = "showPet" |
292
|
6
|
|
|
|
|
146
|
$op =~ s!$SKIP_OP_RE.*$!!; |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# "show_fooPet" = ("show_foo", "Pet") |
295
|
6
|
|
|
|
|
44
|
@$defaults{qw(action controller)} = _load($c, $op =~ /^([a-z_]+)([A-Z]\w+)$/); |
296
|
6
|
100
|
|
|
|
24
|
return $can->() if $defaults->{controller}; |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
return |
299
|
1
|
|
|
|
|
13
|
qq(Controller from operationId "$defaults->{swagger_operation_spec}{operationId}" could not be loaded.); |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
sub _io_error { |
303
|
0
|
|
|
0
|
|
0
|
my $c = shift; |
304
|
0
|
|
|
|
|
0
|
my $level = IO_LOGGING; |
305
|
0
|
|
|
|
|
0
|
$c->app->log->$level(sprintf '%s error: %s', shift, Mojo::JSON::encode_json(shift)); |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
sub _load { |
309
|
99
|
|
|
99
|
|
202
|
my ($c, $action, $controller) = @_; |
310
|
99
|
|
|
|
|
103
|
my (@classes, %uniq); |
311
|
|
|
|
|
|
|
|
312
|
99
|
100
|
|
|
|
269
|
return unless $controller; |
313
|
81
|
|
|
|
|
432
|
$action = decamelize ucfirst $action; |
314
|
|
|
|
|
|
|
|
315
|
81
|
100
|
|
|
|
2083
|
if ($controller =~ /::/) { |
316
|
62
|
|
|
|
|
154
|
push @classes, $controller; |
317
|
|
|
|
|
|
|
} |
318
|
|
|
|
|
|
|
else { |
319
|
19
|
|
|
|
|
58
|
my $singular = $controller; |
320
|
19
|
|
|
|
|
32
|
$singular =~ s!s$!!; # "showPets" = "showPet" |
321
|
|
|
|
|
|
|
push @classes, |
322
|
36
|
|
|
|
|
79
|
grep { !$uniq{$_}++ } |
323
|
19
|
|
|
|
|
44
|
map { ("${_}::$controller", "${_}::$singular") } @{$c->app->routes->namespaces}; |
|
18
|
|
|
|
|
172
|
|
|
19
|
|
|
|
|
47
|
|
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
|
326
|
81
|
|
|
|
|
284
|
while ($controller = shift @classes) { |
327
|
83
|
|
|
|
|
414
|
my $e = Mojo::Loader::load_class($controller); |
328
|
83
|
|
|
|
|
17595
|
warn |
329
|
|
|
|
|
|
|
qq([Swagger2] Load "$controller": @{[ref $e ? $e : $e ? "Can't locate class" : "Success"]}.\n) |
330
|
|
|
|
|
|
|
if DEBUG; |
331
|
83
|
100
|
|
|
|
782
|
return ($action, $controller) if $controller->can('new'); |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
5
|
|
|
|
|
17
|
return; |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
1; |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=encoding utf8 |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
=head1 NAME |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
Mojolicious::Plugin::Swagger2 - Mojolicious plugin for Swagger2 |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
=head1 DEPRECATION WARNING |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
See L. |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
=head1 SYNOPSIS |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
package MyApp; |
352
|
|
|
|
|
|
|
use Mojo::Base "Mojolicious"; |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
sub startup { |
355
|
|
|
|
|
|
|
my $app = shift; |
356
|
|
|
|
|
|
|
$app->plugin(Swagger2 => {url => "data://MyApp/petstore.json"}); |
357
|
|
|
|
|
|
|
} |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
__DATA__ |
360
|
|
|
|
|
|
|
@@ petstore.json |
361
|
|
|
|
|
|
|
{ |
362
|
|
|
|
|
|
|
"swagger": "2.0", |
363
|
|
|
|
|
|
|
"info": {...}, |
364
|
|
|
|
|
|
|
"host": "petstore.swagger.wordnik.com", |
365
|
|
|
|
|
|
|
"basePath": "/api", |
366
|
|
|
|
|
|
|
"paths": { |
367
|
|
|
|
|
|
|
"/pets": { |
368
|
|
|
|
|
|
|
"get": {...} |
369
|
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
} |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
=head1 DESCRIPTION |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
L is L that add routes and |
376
|
|
|
|
|
|
|
input/output validation to your L application. |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=head1 HOOKS |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
=head2 swagger_route_added |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
This hook will be DEPRECATED! See L. |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
=head1 STASH VARIABLES |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
=head2 swagger |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
The L object used to generate the routes is available |
389
|
|
|
|
|
|
|
as C from L. Example code: |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
sub documentation { |
392
|
|
|
|
|
|
|
my ($c, $args, $cb); |
393
|
|
|
|
|
|
|
$c->$cb($c->stash('swagger')->pod->to_string, 200); |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=head2 swagger_operation_spec |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
The Swagger specification for the current |
399
|
|
|
|
|
|
|
L |
400
|
|
|
|
|
|
|
is stored in the "swagger_operation_spec" stash variable. |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
sub list_pets { |
403
|
|
|
|
|
|
|
my ($c, $args, $cb); |
404
|
|
|
|
|
|
|
$c->app->log->info($c->stash("swagger_operation_spec")->{operationId}); |
405
|
|
|
|
|
|
|
... |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
=head2 url |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
Holds the URL to the swagger specification file. |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
=head1 HELPERS |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
=head2 dispatch_to_swagger |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
$bool = $c->dispatch_to_swagger(\%data); |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
This helper can be used to handle WebSocket requests with swagger. |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
This helper is EXPERIMENTAL. |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
=head2 render_swagger |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
$c->render_swagger(\%err, \%data, $status); |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
This method is used to render C<%data> from the controller method. The C<%err> |
429
|
|
|
|
|
|
|
hash will be empty on success, but can contain input/output validation errors. |
430
|
|
|
|
|
|
|
C<$status> is used to set a proper HTTP status code such as 200, 400 or 500. |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
=head1 METHODS |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
=head2 register |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
$self->register($app, \%config); |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
This method is called when this plugin is registered in the L |
439
|
|
|
|
|
|
|
application. |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
C<%config> can contain these parameters: |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
=over 4 |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
=item * coerce |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
This argument will be passed on to L. |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
=item * ensure_swagger_response |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
$app->plugin(swagger2 => { |
452
|
|
|
|
|
|
|
ensure_swagger_response => { |
453
|
|
|
|
|
|
|
exception => "Internal server error.", |
454
|
|
|
|
|
|
|
not_found => "Not found.", |
455
|
|
|
|
|
|
|
} |
456
|
|
|
|
|
|
|
}); |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
Adds a L hook which will make sure |
459
|
|
|
|
|
|
|
"exception" and "not_found" responses are in JSON format. There is no need to |
460
|
|
|
|
|
|
|
specify "exception" and "not_found" if you are happy with the defaults. |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
=item * route |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
Need to hold a Mojolicious route object. See L for an example. |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
This parameter is optional. |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
=item * spec_path |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
Holds the location for where the specifiation can be served from. The default |
471
|
|
|
|
|
|
|
value is "/", relative to "basePath" in the specification. Can be disabled |
472
|
|
|
|
|
|
|
by setting this value to empty string. |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
=item * validate |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
A boolean value (default is true) that will cause your schema to be validated. |
477
|
|
|
|
|
|
|
This plugin will abort loading if the schema include errors |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
=item * swagger |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
A C object. This can be useful if you want to keep use the |
482
|
|
|
|
|
|
|
specification to other things in your application. Example: |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
use Swagger2; |
485
|
|
|
|
|
|
|
my $swagger = Swagger2->new->load($url); |
486
|
|
|
|
|
|
|
plugin Swagger2 => {swagger => $swagger2}; |
487
|
|
|
|
|
|
|
app->defaults(swagger_spec => $swagger->api_spec); |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
Either this parameter or C need to be present. |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
=item * url |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
This will be used to construct a new L object. The C |
494
|
|
|
|
|
|
|
can be anything that L can handle. |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
Either this parameter or C need to be present. |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
=back |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
Copyright (C) 2014-2015, Jan Henning Thorsen |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
505
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
=head1 AUTHOR |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
=cut |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
__DATA__ |