Dancer2-Plugin-OpenAPI

 view release on metacpan or  search on metacpan

lib/Dancer2/Plugin/OpenAPI.pm  view on Meta::CPAN

# TODO: add 'validate_schema'
# TODO: add 'strict_schema'
# TODO: make /swagger.json configurable

package Dancer2::Plugin::OpenAPI;
our $AUTHORITY = 'cpan:YANICK';
# ABSTRACT: create OpenAPI documentation of your application
$Dancer2::Plugin::OpenAPI::VERSION = '1.0.2';
use strict;
use warnings;

use Dancer2::Plugin;
use PerlX::Maybe;
use Scalar::Util qw/ blessed /;

use Dancer2::Plugin::OpenAPI::Path;

use MooseX::MungeHas 'is_ro';

use Path::Tiny;
use File::ShareDir::Tarball;

has doc => (
    is => 'ro',
    lazy => 1,
    default => sub { 
        my $self = shift;

        my $doc = {
            openapi => '3.1.0',
            paths => {},
        };

        $doc->{info}{$_} = '' for qw/ title description version /; 

        $doc->{info}{title} = $self->main_api_module if $self->main_api_module;

        if( my( $desc) = $self->main_api_module_content =~ /
                ^(?:\s* \# \s* ABSTRACT: \s* |=head1 \s+ NAME \s+ (?:\w+) \s+ - \s+  ) ([^\n]+) 
                /xm
        ) {
            $doc->{info}{description} = $desc;
        }

        $doc->{info}{version} = eval {
            $self->main_api_module->VERSION
        } // '0.0.0';

        $doc;
        
    },
);

our $FIRST_LOADED = caller(1);

has main_api_module => (
    is => 'ro',
    lazy => 1,
    from_config => 1,
    default => sub { 
        return if $] lt '5.036000';

        $Dancer2::Plugin::OpenAPI::FIRST_LOADED //= caller;
    },
);

has main_api_module_content => (
    is => 'ro',
    lazy => 1,
    default => sub { 
        my $mod = $_[0]->main_api_module or return '';

        $mod =~ s#::#/#g;
        $mod .= '.pm';
        my $path = $INC{$mod} or return '';

        return Path::Tiny::path($path)->slurp;
    }
);

has show_ui => (
    is => 'ro',
    lazy => 1,
    from_config => sub { 1 },
);

has ui_url => (
    is => 'ro',
    lazy => 1,
    from_config => sub { '/doc' },
);

has ui_dir => (
    is => 'ro',
    lazy => 1,
    from_config => sub { 
        Path::Tiny::path(
            File::ShareDir::Tarball::dist_dir('Dancer2-Plugin-OpenAPI')
        )
    },
);

has auto_discover_skip => (
    is => 'ro',
    lazy => 1,
    default => sub { [
            map { /^qr/ ? eval $_ : $_ }
        @{ $_[0]->config->{auto_discover_skip} || [
            '/openapi.json', ( 'qr!' . $_[0]->ui_url . '!' ) x $_[0]->show_ui
        ] }
    ];
    },
);

has validate_response => ( from_config => 1 );
has strict_validation => ( from_config => 1 );


sub BUILD {
    my $self = shift;



( run in 0.933 second using v1.01-cache-2.11-cpan-302cb4679cc )