-
Notifications
You must be signed in to change notification settings - Fork 44
/
Parameters.pm
211 lines (157 loc) · 6.37 KB
/
Parameters.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package Mojolicious::Plugin::OpenAPI::Parameters;
use Mojo::Base 'Mojolicious::Plugin';
use JSON::Validator::Util qw(is_type);
use Mojo::JSON qw(encode_json);
sub register {
my ($self, $app, $config) = @_;
$app->helper('openapi.build_response_body' => $config->{renderer}
|| \&_helper_build_response_body);
$app->helper('openapi.build_schema_request' => \&_helper_build_schema_request);
$app->helper('openapi.build_schema_response' => \&_helper_build_schema_response);
$app->helper('openapi.coerce_request_parameters' => \&_helper_coerce_request_parameters);
$app->helper('openapi.coerce_response_parameters' => \&_helper_coerce_response_parameters);
$app->helper('openapi.parse_request_body' => \&_helper_parse_request_body);
}
sub _bool {
return map { !is_type($_, 'BOOL') ? $_ : $_ ? 'true' : 'false' } @_;
}
sub _helper_build_response_body {
my $c = shift;
return $_[0]->slurp if UNIVERSAL::isa($_[0], 'Mojo::Asset');
$c->res->headers->content_type('application/json;charset=UTF-8')
unless $c->res->headers->content_type;
return encode_json($_[0]);
}
sub _helper_build_schema_request {
my $c = shift;
my $req = $c->req;
$c->stash->{'openapi.evaluated_request_parameters'} = \my @evaluated;
return {
body => sub {
$evaluated[@evaluated] = $c->openapi->parse_request_body($_[1]);
},
formData => sub {
my $name = shift;
my $value = $req->body_params->every_param($name);
return $evaluated[@evaluated] = {exists => 1, value => $value} if @$value;
$value = $req->upload($name);
return $evaluated[@evaluated] = {exists => !!$value, value => $value && $value->size};
},
header => sub {
my $name = shift;
my $value = $req->headers->every_header($name);
return $evaluated[@evaluated] = {exists => !!@$value, value => $value};
},
path => sub {
my $name = shift;
my $stash = $c->match->stack->[-1];
return $evaluated[@evaluated] = {exists => exists $stash->{$name}, value => $stash->{$name}};
},
query => sub {
return $evaluated[@evaluated] = {exists => 1, value => $req->url->query->to_hash}
unless my $name = shift;
my $value = $req->url->query->every_param($name);
return $evaluated[@evaluated] = {exists => !!@$value, value => $value};
},
};
}
sub _helper_build_schema_response {
my $c = shift;
my $res = $c->res;
$c->stash->{'openapi.evaluated_response_parameters'} = \my @evaluated;
return {
body => sub {
my $res = $c->stash('openapi');
return $evaluated[@evaluated]
= {accept => $c->req->headers->accept, exists => !!$res, value => $res};
},
header => sub {
my $name = shift;
my $value = $res->headers->every_header($name);
return $evaluated[@evaluated] = {exists => !!@$value, value => $value};
},
};
}
sub _helper_coerce_request_parameters {
my ($c, $evaluated) = @_;
my $output = $c->validation->output;
my $req = $c->req;
for my $i (@$evaluated) {
next unless $i->{valid};
$output->{$i->{name}} = $i->{value};
$c->stash(@$i{qw(name value)}) if $i->{in} eq 'path';
$req->headers->header($i->{name}, ref $i->{value} eq 'ARRAY' ? @{$i->{value}} : $i->{value})
if $i->{in} eq 'header';
$req->url->query->merge(@$i{qw(name value)}) if $i->{in} eq 'query';
$req->params->merge(@$i{qw(name value)}) if $i->{in} eq 'query';
$req->params->merge(@$i{qw(name value)}) if $i->{in} eq 'formData';
$req->body_params->merge(@$i{qw(name value)}) if $i->{in} eq 'formData';
}
}
sub _helper_coerce_response_parameters {
my ($c, $evaluated) = @_;
my $res = $c->res;
for my $i (@$evaluated) {
next unless $i->{valid};
$c->stash(openapi_negotiated_content_type => $i->{content_type}) if $i->{in} eq 'body';
$res->headers->header($i->{name},
_bool(ref $i->{value} eq 'ARRAY' ? @{$i->{value}} : $i->{value}))
if $i->{in} eq 'header';
}
}
sub _helper_parse_request_body {
my ($c, $param) = @_;
my $content_type = $c->req->headers->content_type || '';
my $res = {content_type => $content_type, exists => !!$c->req->body_size};
if (grep { $content_type eq $_ } qw(application/x-www-form-urlencoded multipart/form-data)) {
$res->{value} = $c->req->body_params->to_hash;
}
else {
$res->{value} = $c->req->json;
}
return $res;
}
1;
=encoding utf8
=head1 NAME
Mojolicious::Plugin::OpenAPI::Parameters - Methods for transforming data from/to JSON::Validator::Schema
=head1 DESCRIPTION
L<Mojolicious::Plugin::OpenAPI::Parameters> adds helpers to your L<Mojolicious>
application, required by L<Mojolicious::Plugin::OpenAPI>. These helpers can be
redefined in case you have special needs.
=head1 HELPERS
=head2 openapi.build_response_body
$bytes = $c->openapi->build_response_body(Mojo::Asset->new);
$bytes = $c->openapi->build_response_body($data);
Takes validated data and turns it into bytes that will be used as HTTP response
body. This method is useful to override, in case you want to render some other
structure than JSON.
=head2 openapi.build_schema_request
$hash_ref = $c->openapi->build_schema_request;
Builds input data for L<JSON::Validator::Schema::OpenAPIv2/validate_request>.
=head2 openapi.build_schema_response
$hash_ref = $c->openapi->build_schema_response;
Builds input data for L<JSON::Validator::Schema::OpenAPIv2/validate_response>.
=head2 openapi.coerce_request_parameters
$c->openapi->coerce_request_parameters(\@evaluated_parameters);
Used by L<Mojolicious::Plugin::OpenAPI> to write the validated data back to
L<Mojolicious::Controller/req> and
L<Mojolicious::Validator::Validation/output>.
=head2 openapi.coerce_response_parameters
$c->openapi->coerce_response_parameters(\@evaluated_parameters);
Used by L<Mojolicious::Plugin::OpenAPI> to write the validated data to
L<Mojolicious::Controller/res>.
=head2 openapi.parse_request_body
$hash_ref = $c->openapi->parse_request_body;
Returns a structure representing the request body. The default is to parse the
input as JSON:
{content_type => "application/json", exists => !!$c->req->body_size, value => $c->req->json};
This method is useful to override, in case you want to parse some other
structure than JSON.
=head1 METHODS
=head2 register
$self->register($app, \%config);
This method will add the L</HELPERS> to your L<Mojolicious> C<$app>.
=head1 SEE ALSO
L<Mojolicious::Plugin::OpenAPI>.
=cut