Mojolicious::Plugin::OpenAPI - OpenAPI / Swagger plugin for Mojolicious
use Mojolicious::Lite;
# Will be moved under "basePath", resulting in "POST /api/echo"
post "/echo" => sub {
# Validate input request or return an error document
my $c = shift->openapi->valid_input or return;
# Generate some data
my $data = {body => $c->req->json};
# Validate the output response and render it to the user agent
# using a custom "openapi" handler.
$c->render(openapi => $data);
}, "echo";
# Load specification and start web server
# Use "v3" instead of "v2" for "schema" if you are using OpenAPI v3
# The plugin must be loaded *after* defining the routes in a Lite app
plugin OpenAPI => {url => "data:///spec.json", schema => "v2"};
app->start;
__DATA__
@@ spec.json
{
"swagger" : "2.0",
"info" : { "version": "0.8", "title" : "Echo Service" },
"schemes" : [ "http" ],
"basePath" : "/api",
"paths" : {
"/echo" : {
"post" : {
"x-mojo-name" : "echo",
"parameters" : [
{ "in": "body", "name": "body", "schema": { "type" : "object" } }
],
"responses" : {
"200": {
"description": "Echo response",
"schema": { "type": "object" }
}
}
}
}
}
}
See Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2 or Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv3 for tutorials on how to write a "full" app with application class and controllers.
Mojolicious::Plugin::OpenAPI is Mojolicious::Plugin that add routes and input/output validation to your Mojolicious application based on a OpenAPI (Swagger) specification. This plugin supports both version 2.0 and 3.x, though 3.x might have some missing features.
Have a look at the "SEE ALSO" for references to more documentation.
Please report in issues or open pull requests to enhance the 3.0 support.
$hash = $c->openapi->spec($json_pointer)
$hash = $c->openapi->spec("/info/title")
$hash = $c->openapi->spec;
Returns the OpenAPI specification. A JSON Pointer can be used to extract a
given section of the specification. The default value of $json_pointer
will
be relative to the current operation. Example:
{
"paths": {
"/pets": {
"get": {
// This datastructure is returned by default
}
}
}
}
@errors = $c->openapi->validate;
Used to validate a request. @errors
holds a list of
JSON::Validator::Error objects or empty list on valid input.
Note that this helper is only for customization. You probably want "openapi.valid_input" in most cases.
$c = $c->openapi->valid_input;
Returns the Mojolicious::Controller object if the input is valid or automatically render an error document if not and return false. See "SYNOPSIS" for example usage.
Mojolicious::Plugin::OpenAPI will emit the following hooks on the application object.
Emitted after all routes have been added by this plugin.
$app->hook(openapi_routes_added => sub {
my ($openapi, $routes) = @_;
for my $route (@$routes) {
...
}
});
This hook is EXPERIMENTAL and subject for change.
This plugin register a new handler called openapi
. The special thing about
this handler is that it will validate the data before sending it back to the
user agent. Examples:
$c->render(json => {foo => 123}); # without validation
$c->render(openapi => {foo => 123}); # with validation
This handler will also use "renderer" to format the output data. The code below shows the default "renderer" which generates JSON data:
$app->plugin(
OpenAPI => {
renderer => sub {
my ($c, $data) = @_;
return Mojo::JSON::encode_json($data);
}
}
);
$route = $openapi->route;
The parent Mojolicious::Routes::Route object for all the OpenAPI endpoints.
$jv = $openapi->validator;
Holds a JSON::Validator::OpenAPI::Mojolicious object.
$openapi = $openapi->register($app, \%config);
$openapi = $app->plugin(OpenAPI => \%config);
Loads the OpenAPI specification, validates it and add routes to $app. It will also set up "HELPERS" and adds a before_render hook for auto-rendering of error documents. The return value is the object instance, which allow you to access the "ATTRIBUTES" after you load the plugin.
%config
can have:
The OpenAPI specification does not allow "$ref" at every level, but setting this flag to a true value will ignore the $ref check.
Note that setting this attribute is discourage.
See "coerce" in JSON::Validator for possible values that coerce
can take.
Default: booleans,numbers,strings
The default value will include "defaults" in the future, once that is stable enough.
A list of response codes that will get a "$ref"
pointing to
"#/definitions/DefaultResponse", unless already defined in the spec.
"DefaultResponse" can be altered by setting "default_response_name".
The default response code list is the following:
400 | Bad Request | Invalid input from client / user agent
401 | Unauthorized | Used by Mojolicious::Plugin::OpenAPI::Security
404 | Not Found | Route is not defined
500 | Internal Server Error | Internal error or failed output validation
501 | Not Implemented | Route exists, but the action is not implemented
Note that more default codes might be added in the future if required by the plugin.
The name of the "definition" in the spec that will be used for "default_response_codes". The default value is "DefaultResponse". See "Default response schema" in Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2 for more details.
log_level
is used when logging invalid request/response error messages.
Default: "warn".
A list of OpenAPI classes to extend the functionality. Default is: Mojolicious::Plugin::OpenAPI::Cors, Mojolicious::Plugin::OpenAPI::SpecRenderer and Mojolicious::Plugin::OpenAPI::Security.
$app->plugin(OpenAPI => {plugins => [qw(+Cors +SpecRenderer +Security)]});
You can load your own plugins by doing:
$app->plugin(OpenAPI => {plugins => [qw(+SpecRenderer My::Cool::OpenAPI::Plugin)]});
See "RENDERER".
route
can be specified in case you want to have a protected API. Example:
$app->plugin(OpenAPI => {
route => $app->routes->under("/api")->to("user#auth"),
url => $app->home->rel_file("cool.api"),
});
Can be used to set a different schema, than the default OpenAPI 2.0 spec. Example values: "http://swagger.io/v2/schema.json", "v2" or "v3".
See also Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2 and Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv3.
Name of the route that handles the "basePath" part of the specification and serves the specification. Defaults to "x-mojo-name" in the specification at the top level.
See "schema" in JSON::Validator for the different url
formats that is
accepted.
spec
is an alias for "url", which might make more sense if your
specification is written in perl, instead of JSON or YAML.
Can be used to overridden /info/version
in the API specification, from the
return value from the VERSION()
method in version_from_class
.
This will only have an effect if "version" is "0".
Defaults to the current $app
.
Henrik Andersen
Ilya Rassadin
Jan Henning Thorsen
Joel Berger
Copyright (C) Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
-
Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv2
Guide for how to use this plugin with OpenAPI version 2.0 spec.
-
Mojolicious::Plugin::OpenAPI::Guides::OpenAPIv3
Guide for how to use this plugin with OpenAPI version 3.0 spec.
-
Mojolicious::Plugin::OpenAPI::Cors
Plugin to add Cross-Origin Resource Sharing (CORS).
-
Mojolicious::Plugin::OpenAPI::Security
Plugin for handling security definitions in your schema.
-
Mojolicious::Plugin::OpenAPI::SpecRenderer
Plugin for exposing your spec in human readble or JSON format.
-
Official OpenAPI website.