package Mojolicious::Plugin::OpenAPI;
use Mojo::Base 'Mojolicious::Plugin';
use JSON::Validator;
use Mojo::JSON;
use Mojo::Util;
use Mojolicious::Plugin::OpenAPI::Parameters;
use constant DEBUG => $
ute => sub {undef};
has validator => sub { JSON::Validator::Schema->new; };
sub register {
my ($self, $app, $config) = @_;
$self->validator(JSON::Validator->new->schema($config->{url} || $co
c = shift;
my $path = shift // 'for_current';
my $self = _self($c);
# Get spec by valid JSON pointer
return $self->validator->get($path) if ref $path or $path =~ m!^/! or !length $path;
pecification>
and generate routes and input/output rules from it. See L<JSON::Validator> for
L<supported schema file formats|JSON::Validator/Supported schema formats>.
{
"swagger": "2.0",
"
from the keys
under C</pets>.
The different parts of the specification can also be retrieved as JSON using
the "OPTIONS" HTTP method. Example:
OPTIONS /api/pets
OPTIONS /api/pets?method=get
No
h
The C<basePath> will also be used to add a route that renders back the
specification either as JSON or HTML. Examples:
=over 2
=item * http://example.com/api.html
Retrieve the expanded version o
jolicious::Plugin::OpenAPI::SpecRenderer;
use Mojo::Base 'Mojolicious::Plugin';
use JSON::Validator;
use Mojo::JSON;
use Scalar::Util qw(blessed);
use constant DEBUG => $ENV{MOJO_OPENAPI_DEBUG} |
fication} // 1) {
my $spec_route = $openapi->route->get(
'/',
[format => [qw(html json)]],
{format => undef},
sub { shift->openapi->render_spec(@_) }
);
my $name =
>render(json => {errors => [{message => 'No spec defined.'}]}, status => 404)
unless my $schema = $validator->get([paths => $path, $method ? ($method) : ()]);
return $c->render(
json => {
startup {
my $app = shift;
$app->plugin(OpenAPI => {
url => "data:///security.json",
security => {
dummy => sub {
my ($c, $definition, $scopes, $cb) = @_;
message => "Some error!", path => "/Whatever"}];
return [{message => "Some error!"}];
return [JSON::Validator::Error->new];
=back
On success, the following headers will be set, unless already se
:Parameters;
use Mojo::Base 'Mojolicious::Plugin';
use JSON::Validator::Util qw(is_bool schema_type);
use Mojo::JSON qw(encode_json decode_json);
sub register {
my ($self, $app, $config) = @_;
o::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 =
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;
ecification>
and generates routes and input/output rules from it. See L<JSON::Validator> for
L<supported schema file formats|JSON::Validator/Supported schema formats>.
{
"openapi": "3.0.2",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
"description": "Pet response",
"content": {
"application/json": {
"schema": {
"type": "object",
"prop
# Load your specification
$self->plugin("OpenAPI" => {url => $app->home->rel_file("myapi.json")});
$self->hook(around_action => sub {
my ($next, $c, $action, $last) = @_;
#
pecification>
and generate routes and input/output rules from it. See L<JSON::Validator> for
L<supported schema file formats|JSON::Validator/Supported schema formats>.
{
"swagger": "2.0",
"
from the keys
under C</pets>.
The different parts of the specification can also be retrieved as JSON using
the "OPTIONS" HTTP method. Example:
OPTIONS /api/pets
OPTIONS /api/pets?method=get
No
h
The C<basePath> will also be used to add a route that renders back the
specification either as JSON or HTML. Examples:
=over 2
=item * http://example.com/api.html
Retrieve the expanded version o