NAME

Dancer2::Plugin::OpenAPI - create OpenAPI documentation of your application

VERSION

version 1.0.2

SYNOPSIS

package MyApp;

use Dancer;
use Dancer2::Plugin::OpenAPI;

our $VERSION = "0.1";

get '/choreograph/:name' => sub { ... };

1;

DESCRIPTION

This plugin provides tools to create and access a OpenApi specification file for a Dancer REST web service. Originally was Dancer::Plugin::Swagger.

Overview of Dancer2::Plugin::OpenAPI's features:

CONFIGURATION

plugins:
    OpenApi
       main_api_module: MyApp
       show_ui: 1
       ui_url: /doc
       ui_dir: /path/to/files
       auto_discover_skip:
        - /openapi.json
        - qr#^/doc/#

main_api_module

If not provided explicitly, the OpenApi document's title and version will be set to the abstract and version of this module.

For Perl >= 5.36.0, defaults to the first module to import Dancer2::Plugin::OpenAPI.

show_ui

If true, a route will be created for the Swagger UI (see http://swagger.io/swagger-ui/).

Defaults to true.

ui_url

Path of the swagger ui route. Will also be the prefix for all the CSS/JS dependencies of the page.

Defaults to /doc.

ui_dir

Filesystem path to the directory holding the assets for the Swagger UI page.

Defaults to a copy of the Swagger UI code bundled with the Dancer2::Plugin::OpenAPI distribution.

auto_discover_skip

List of urls that should not be added to the OpenApi document by openapi_auto_discover. If an url begins with qr, it will be compiled as a regular expression.

Defauls to /openapi.json and, if show_ui is true, all the urls under ui_url.

validate_response

If set to true, calls to openapi_response will verify if a schema is defined for the response, and if so validate against it. JSON::Schema::AsType is used for the validation (and this required if this option is used).

Defaults to false.

strict_validation

If set to true, dies if a call to openapi_response doesn't find a schema for its response.

Defaults to false.

PLUGIN KEYWORDS

openapi_path $description, \%args, $route

openapi_path {
    description => 'Returns info about a judge',
},
get '/judge/:judge_name' => sub {
    ...;
};

Registers a route as a OpenAPI path item in the OpenAPI document.

%args is optional.

The $description is optional as well, and can also be defined as part of the %args.

# equivalent to the main example
openapi_path 'Returns info about a judge',
get '/judge/:judge_name' => sub {
    ...;
};

If the $description spans many lines, it will be left-trimmed.

openapi_path q{ 
    Returns info about a judge.

    Some more documentation can go here.

        And this will be seen as a performatted block
        by OpenAPI.
}, 
get '/judge/:judge_name' => sub {
    ...;
};

Supported arguments

openapi_template $code, $args

openapi_path {
    responses => {
        404 => { template => sub { +{ error => "judge '$_[0]' not found" } }  
    },
},
get '/judge/:judge_name' => {  
    my $name = param('judge_name');
    return openapi_template 404, $name unless in_db($name);
    ...;
};

Calls the template for the $code response, passing it $args. If $code is numerical, also set the response's status to that value.

openapi_auto_discover skip => \@list

Populates the OpenAPI document with information of all the routes of the application.

Accepts an optional skip parameter that takes an arrayref of routes that shouldn't be added to the OpenAPI document. The routes can be specified as-is, or via regular expressions. If no skip list is given, defaults to the c<auto_discover_skip> configuration value.

openapi_auto_discover skip => [ '/openapi.json', qr#^/doc/# ];

The information of a route won't be altered if it's already present in the document.

If a route has path parameters, they will be automatically added as such in the parameters section.

Routes defined as regexes are skipped, as there is no clean way to automatically make them look nice.

    # will be picked up
get '/user' => ...;

    # ditto, as '/user/{user_id}'
get '/user/:user_id => ...;

    # won't be picked up
get qr#/user/(\d+)# => ...;

Note that routes defined after openapi_auto_discover has been called won't be added to the OpenAPI document. Typically, you'll want openapi_auto_discover to be called at the very end of your module. Alternatively, openapi_auto_discover can be called more than once safely -- which can be useful if an application creates routes dynamically.

openapi_definition $name => $definition, ...

Adds a schema (or more) to the definition section of the OpenAPI document.

openapi_definition 'Judge' => {
    type => 'object',
    required => [ 'fullname' ],
    properties => {
        fullname => { type => 'string' },
        seasons => { type => 'array', items => { type => 'integer' } },
    }
};

The function returns the reference to the definition that can be then used where schemas are used.

my $Judge = openapi_definition 'Judge' => { ... };
# $Judge is now the hashref '{ '$ref' => '#/definitions/Judge' }'

# later on...
openapi_path {
    responses => {
        default => { schema => $Judge },
    },
},
get '/judge/:name' => sub { ... };

EXAMPLES

See the examples/ directory of the distribution for a working example.

SEE ALSO

AUTHOR

Yanick Champoux yanick@cpan.org endorse

COPYRIGHT AND LICENSE

This software is copyright (c) 2024 by Yanick Champoux.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.