Mojolicious-Plugin-OpenAPI
view release on metacpan or search on metacpan
lib/Mojolicious/Plugin/OpenAPI/Parameters.pm view on Meta::CPAN
$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};
eval {
$res->{value} //= $c->req->body_params->to_hash
if grep { $content_type eq $_ } qw(application/x-www-form-urlencoded multipart/form-data);
# Trying to use the already parsed json() or fallback to manually decoding the request
# since it will make the eval {} fail on invalid json.
$res->{value} //= $c->req->json // decode_json $c->req->body;
1;
} or do {
$res->{value} = $c->req->body;
};
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
( run in 0.674 second using v1.01-cache-2.11-cpan-39bf76dae61 )