| 1 | # TODO: add responses | ||||||||||||||||||||||||||||
| 2 | # TODO: add examples | ||||||||||||||||||||||||||||
| 3 | # TODO: then add the template for different responses values | ||||||||||||||||||||||||||||
| 4 | # TODO: override send_error ? | ||||||||||||||||||||||||||||
| 5 | # TODO: add 'validate_schema' | ||||||||||||||||||||||||||||
| 6 | # TODO: add 'strict_schema' | ||||||||||||||||||||||||||||
| 7 | # TODO: make /swagger.json configurable | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | package Dancer::Plugin::Swagger; | ||||||||||||||||||||||||||||
| 10 | 3 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||
| 11 | # ABSTRACT: create Swagger documentation of the app REST interface | ||||||||||||||||||||||||||||
| 12 | 3 | $Dancer::Plugin::Swagger::VERSION = '0.2.1'; | |||||||||||||||||||||||||||
| 13 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Dancer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Dancer::Plugin::REST; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use PerlX::Maybe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use Dancer::Plugin::Swagger::Path; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | with 'MooX::Singleton'; | |||||||||||||||||||||||||||
| 26 | 3 | ▶ | use MooseX::MungeHas 'is_ro'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | use Class::Load qw/ load_class /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 3 | ▶ | use File::ShareDir::Tarball; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub import { | ||||||||||||||||||||||||||||
| 33 | 2 | ▶ | $Dancer::Plugin::Swagger::FIRST_LOADED ||= caller; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=8.1
Condition33%1/3
$Dancer::Plugin::Swagger::FIRST_LOADED ||= caller
MC/DC0%0/2
$Dancer::Plugin::Swagger::FIRST_LOADED ||= caller
$Dancer::Plugin::Swagger::FIRST_LOADED
caller
Truth table33%1/3
A ||= B
A= $Dancer::Plugin::Swagger::FIRST_LOADEDB= caller
| |||||||||||||||||||||||||||||
| 34 | 2 | goto &Exporter::import; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has doc => ( | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | lazy => 1, | ||||||||||||||||||||||||||||
| 40 | default => sub { | ||||||||||||||||||||||||||||
| 41 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | my $doc = { | |||||||||||||||||||||||||||
| 44 | swagger => '2.0', | ||||||||||||||||||||||||||||
| 45 | paths => {}, | ||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | $doc->{info}{$_} = '' for qw/ title description version /; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | $doc->{info}{title} = $self->main_api_module if $self->main_api_module; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | ▶ | if( my( $desc) = $self->main_api_module_content =~ / | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | ^(?:\s* \# \s* ABSTRACT: \s* |=head1 \s+ NAME \s+ (?:\w+) \s+ - \s+ ) ([^\n]+) | ||||||||||||||||||||||||||||
| 54 | /xm | ||||||||||||||||||||||||||||
| 55 | ) { | ||||||||||||||||||||||||||||
| 56 | 0 | $doc->{info}{description} = $desc; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | $doc->{info}{version} = eval { | ||||||||||||||||||||||||||
|
Condition50%1/2
eval { // "0.0.0"
MC/DC0%0/1
eval {
do {
$self->main_api_module->VERSION
}
} // "0.0.0"
eval {
do {
$self->main_api_module->VERSION
}
}
Truth table50%1/2
A
A= eval {
do {
$self->main_api_module->VERSION
}
}
| |||||||||||||||||||||||||||||
| 60 | 2 | $self->main_api_module->VERSION | |||||||||||||||||||||||||||
| 61 | } // '0.0.0'; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | $doc; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||
| 66 | 3 | ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | has main_api_module => ( | ||||||||||||||||||||||||||||
| 69 | is => 'ro', | ||||||||||||||||||||||||||||
| 70 | lazy => 1, | ||||||||||||||||||||||||||||
| 71 | default => sub { | ||||||||||||||||||||||||||||
| 72 | plugin_setting->{main_api_module} | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | || $Dancer::Plugin::Swagger::FIRST_LOADED; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||
| 75 | 3 | ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | has main_api_module_content => ( | ||||||||||||||||||||||||||||
| 78 | is => 'ro', | ||||||||||||||||||||||||||||
| 79 | lazy => 1, | ||||||||||||||||||||||||||||
| 80 | default => sub { | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $mod = $_[0]->main_api_module or return ''; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $mod =~ s#::#/#g; | |||||||||||||||||||||||||||
| 83 | 0 | $mod .= '.pm'; | |||||||||||||||||||||||||||
| 84 | 0 | Path::Tiny::path( $INC{$mod} )->slurp; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 3 | ); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | has show_ui => ( | ||||||||||||||||||||||||||||
| 89 | is => 'ro', | ||||||||||||||||||||||||||||
| 90 | lazy => 1, | ||||||||||||||||||||||||||||
| 91 | 3 | ▶ | default => sub { plugin_setting->{show_ui} // 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
plugin_setting()->{"show_ui"} // 1
MC/DC0%0/1
plugin_setting()->{"show_ui"} // 1
plugin_setting()->{"show_ui"}
Truth table50%1/2
A
A= plugin_setting()->{"show_ui"}
| |||||||||||||||||||||||||||||
| 92 | 3 | ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | has ui_url => ( | ||||||||||||||||||||||||||||
| 95 | is => 'ro', | ||||||||||||||||||||||||||||
| 96 | lazy => 1, | ||||||||||||||||||||||||||||
| 97 | 3 | ▶ | default => sub { plugin_setting->{ui_url} // '/doc' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
plugin_setting()->{"ui_url"} // "/doc"
MC/DC0%0/1
plugin_setting()->{"ui_url"} // "/doc"
plugin_setting()->{"ui_url"}
Truth table50%1/2
A
A= plugin_setting()->{"ui_url"}
| |||||||||||||||||||||||||||||
| 98 | 3 | ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | has ui_dir => ( | ||||||||||||||||||||||||||||
| 101 | is => 'ro', | ||||||||||||||||||||||||||||
| 102 | lazy => 1, | ||||||||||||||||||||||||||||
| 103 | default => sub { | ||||||||||||||||||||||||||||
| 104 | Path::Tiny::path( | ||||||||||||||||||||||||||||
| 105 | plugin_setting->{ui_dir} || | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | File::ShareDir::Tarball::dist_dir('Dancer-Plugin-Swagger') | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
plugin_setting()->{'ui_dir'} || File::ShareDir::Tarball::dist_dir('Dancer-Plugin-Swagger')
MC/DC0%0/2
plugin_setting()->{'ui_dir'} || File::ShareDir::Tarball::dist_dir('Dancer-Plugin-Swagger')
plugin_setting()->{'ui_dir'}
File::ShareDir::Tarball::dist_dir('Dancer-Plugin-Swagger')
Truth table0%0/3
A || B
A= plugin_setting()->{'ui_dir'}B= File::ShareDir::Tarball::dist_dir('Dancer-Plugin-Swagger')
| |||||||||||||||||||||||||||||
| 107 | ) | ||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | 3 | ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | has auto_discover_skip => ( | ||||||||||||||||||||||||||||
| 112 | is => 'ro', | ||||||||||||||||||||||||||||
| 113 | lazy => 1, | ||||||||||||||||||||||||||||
| 114 | default => sub { [ | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | map { /^qr/ ? eval $_ : $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | @{ plugin_setting->{auto_discover_skip} || [ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | '/swagger.json', ( 'qr!' . $_[0]->ui_url . '!' ) x $_[0]->show_ui | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | ] } | ||||||||||||||||||||||||||||
| 119 | ]; | ||||||||||||||||||||||||||||
| 120 | }, | ||||||||||||||||||||||||||||
| 121 | 3 | ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | has validate_response => sub { plugin_setting->{validate_response} }; | ||||||||||||||||||||||||||
|
Subroutine
_build_validate_response: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | has strict_validation => sub { plugin_setting->{strict_validation} }; | ||||||||||||||||||||||||||
|
Subroutine
_build_strict_validation: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | my $plugin = __PACKAGE__->instance; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | if ( $plugin->show_ui ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 3 | my $base_url = $plugin->ui_url; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | get $base_url => sub { redirect $base_url . '/?url=/swagger.json' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | get $base_url . '/' => sub { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my $file = $plugin->ui_dir->child('index.html'); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | send_error "file not found", 404 unless -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | return $file->slurp; | |||||||||||||||||||||||||||
| 139 | 3 | }; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | get $base_url.'/**' => sub { | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $file = $plugin->ui_dir->child( @{ (splat())[0] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | send_error "file not found", 404 unless -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | send_file $file, system_path => 1; | |||||||||||||||||||||||||||
| 147 | 3 | }; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # TODO make the doc url configurable | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | get '/swagger.json' => sub { | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $plugin->doc | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 3 | }; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | register swagger_auto_discover => sub { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | $args{skip} ||= $plugin->auto_discover_skip; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{"skip"} ||= $plugin->auto_discover_skip
MC/DC0%0/2
$args{"skip"} ||= $plugin->auto_discover_skip
$args{"skip"}
$plugin->auto_discover_skip
Truth table0%0/3
A ||= B
A= $args{"skip"}B= $plugin->auto_discover_skip
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my $routes = Dancer::App->current->registry->routes; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | my $doc = $plugin->doc->{paths}; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | for my $method ( qw/ get post put delete / ) { | |||||||||||||||||||||||||||
| 167 | 0 | for my $r ( @{ $routes->{$method} } ) { | |||||||||||||||||||||||||||
| 168 | 0 | my $pattern = $r->pattern; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | next if ref $pattern eq 'Regexp'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | next if grep { ref $_ ? $pattern =~ $_ : $pattern eq $_ } @{ $args{skip} }; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $path = Dancer::Plugin::Swagger::Path->new( route => $r ); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | warn "adding $path"; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | $path->add_to_doc($plugin->doc); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 3 | }; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | register swagger_path => sub { | ||||||||||||||||||||||||||||
| 185 | 8 | ▶ | my @routes; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||
| 186 | 8 | ▶ | push @routes, pop @_ while eval { $_[-1]->isa('Dancer::Route') }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # we don't process HEAD | ||||||||||||||||||||||||||||
| 189 | 8 | @routes = grep { $_->method ne 'head' } @routes; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 8 | my $description; | |||||||||||||||||||||||||||
| 192 | 8 | ▶ | if( @_ and not ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and not ref $_[0]
MC/DC100%2/2
@_ and not ref $_[0]
@_
not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0]
| |||||||||||||||||||||||||||||
| 193 | 1 | $description = shift; | |||||||||||||||||||||||||||
| 194 | 1 | $description =~ s/^\s*\n//; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 1 | ▶ | $description =~ s/^$1//mg | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | if $description =~ /^(\s+)/; | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 8 | ▶ | my $arg = shift @_ || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift @_ || {}
MC/DC100%1/1
shift @_ || {}
shift @_
Truth table100%2/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 8 | ▶ | $arg->{description} = $description if $description; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # groom the parameters | ||||||||||||||||||||||||||||
| 205 | 8 | ▶ | if ( my $p = $arg->{parameters} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 4 | ▶ | if( ref $p eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 2 | $_ = { description => $_ } for grep { ! ref } values %$p; | |||||||||||||||||||||||||||
| 208 | 2 | $p = [ map { +{ name => $_, %{$p->{$_}} } } sort keys %$p ]; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # deal with named parameters | ||||||||||||||||||||||||||||
| 212 | 4 | my @p; | |||||||||||||||||||||||||||
| 213 | 4 | ▶ | while( my $k = shift @$p ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 9 | ▶ | unless( ref $k ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 2 | my $value = shift @$p; | |||||||||||||||||||||||||||
| 216 | 2 | ▶ | $value = { description => $value } unless ref $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 2 | $value->{name} = $k; | |||||||||||||||||||||||||||
| 218 | 2 | $k = $value; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 9 | ▶ | if (!exists($k->{schema}) && !exists($k->{type})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $k->{"schema"} or exists $k->{"type"}
MC/DC50%1/2
exists $k->{"schema"} or exists $k->{"type"}
exists $k->{"schema"}
exists $k->{"type"}
Truth table66%2/3
A or B
A= exists $k->{"schema"}B= exists $k->{"type"}
| |||||||||||||||||||||||||||||
| 221 | # set a default type iff it's missing and this doesn't reference a schema | ||||||||||||||||||||||||||||
| 222 | 3 | $k->{type} = 'string'; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 9 | push @p, $k; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | 4 | $p = \@p; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # set defaults | ||||||||||||||||||||||||||||
| 229 | 4 | $p = [ map { +{ in => 'query', %$_ } } @$p ]; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 4 | $arg->{parameters} = $p; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 8 | for my $route ( @routes ) { | |||||||||||||||||||||||||||
| 236 | 8 | my $path = Dancer::Plugin::Swagger::Path->new(%$arg, route => $route); | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 8 | $path->add_to_doc( $plugin->doc ); | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 8 | my $code = $route->code; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | $route->code(sub { | ||||||||||||||||||||||||||||
| 243 | 5 | ▶ | local $Dancer::Plugin::Swagger::THIS_ACTION = $path; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 244 | 5 | $code->(); | |||||||||||||||||||||||||||
| 245 | 8 | }); | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 3 | }; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | register swagger_template => sub { | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | my $vars = pop; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my $status = shift || Dancer::status(); | ||||||||||||||||||||||||||
|
Condition0%0/3
shift() || Dancer::status()
MC/DC0%0/2
shift() || Dancer::status()
shift()
Dancer::status()
Truth table0%0/3
A || B
A= shift()B= Dancer::status()
| |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | my $template = $Dancer::Plugin::Swagger::THIS_ACTION->{responses}{$status}{template}; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | ▶ | Dancer::status( $status ) if $status =~ /^\d{3}$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 0 | ▶ | return swagger_response( $status, $template ? $template->($vars) : $vars ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 3 | }; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | sub swagger_response { | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | my $data = pop; | ||||||||||||||||||||||||||
|
Subroutine
swagger_response: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | my $status = Dancer::status(@_); | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $Dancer::Plugin::Swagger::THIS_ACTION->validate_response( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | $status => $data, $plugin->strict_validation | ||||||||||||||||||||||||||||
| 268 | ) if $plugin->validate_response; | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | $data; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 3 | register swagger_response => \&swagger_response; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | register swagger_definition => sub { | ||||||||||||||||||||||||||||
| 276 | 1 | ▶ | my( $name, $def ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 1 | ▶ | $plugin->doc->{definitions} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$plugin->doc->{"definitions"} ||= {}
MC/DC0%0/1
$plugin->doc->{"definitions"} ||= {}
$plugin->doc->{"definitions"}
Truth table50%1/2
A
A= $plugin->doc->{"definitions"}
| |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 1 | $plugin->doc->{definitions}{$name} = $def; | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 1 | return { '$ref', => '#/definitions/'.$name }; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 3 | }; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 3 | register_plugin; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 3 | 1; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | __END__ | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =pod | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head1 NAME | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Dancer::Plugin::Swagger - create Swagger documentation of the app REST interface | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head1 VERSION | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | version 0.2.1 | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | package MyApp; | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | use Dancer; | ||||||||||||||||||||||||||||
| 309 | use Dancer::Plugin::Swagger; | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | our $VERSION = "0.1"; | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | get '/choreograph/:name' => sub { ... }; | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 1; | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | This plugin provides tools to create and access a L<Swagger|http://swagger.io/> specification file for a | ||||||||||||||||||||||||||||
| 320 | Dancer REST web service. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | Overview of C<Dancer::Plugin::Swagger>'s features: | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =over | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =item Can create a F</swagger.json> REST specification file. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =item Can auto-discover routes and add them to the swagger file. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =item Can provide a Swagger UI version of the swagger documentation. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =back | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | plugins: | ||||||||||||||||||||||||||||
| 337 | Swagger: | ||||||||||||||||||||||||||||
| 338 | main_api_module: MyApp | ||||||||||||||||||||||||||||
| 339 | show_ui: 1 | ||||||||||||||||||||||||||||
| 340 | ui_url: /doc | ||||||||||||||||||||||||||||
| 341 | ui_dir: /path/to/files | ||||||||||||||||||||||||||||
| 342 | auto_discover_skip: | ||||||||||||||||||||||||||||
| 343 | - /swagger.json | ||||||||||||||||||||||||||||
| 344 | - qr#^/doc/# | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =head2 main_api_module | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | If not provided explicitly, the Swagger document's title and version will be set | ||||||||||||||||||||||||||||
| 349 | to the abstract and version of this module. | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Defaults to the first | ||||||||||||||||||||||||||||
| 352 | module to import L<Dancer::Plugin::Swagger>. | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =head2 show_ui | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | If C<true>, a route will be created for the Swagger UI (see L<http://swagger.io/swagger-ui/>). | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Defaults to C<true>. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =head2 ui_url | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Path of the swagger ui route. Will also be the prefix for all the CSS/JS dependencies of the page. | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | Defaults to C</doc>. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =head2 ui_dir | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | Filesystem path to the directory holding the assets for the Swagger UI page. | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Defaults to a copy of the Swagger UI code bundled with the L<Dancer::Plugin::Swagger> distribution. | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =head2 auto_discover_skip | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | List of urls that should not be added to the Swagger document by C<swagger_auto_discover>. | ||||||||||||||||||||||||||||
| 375 | If an url begins with C<qr>, it will be compiled as a regular expression. | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | Defauls to C</swagger.json> and, if C<show_ui> is C<true>, all the urls under C<ui_url>. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =head2 validate_response | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | If set to C<true>, calls to C<swagger_response> will verify if a schema is defined | ||||||||||||||||||||||||||||
| 382 | for the response, and if so validate against it. L<JSON::Schema::AsType> is used for the | ||||||||||||||||||||||||||||
| 383 | validation (and this required if this option is used). | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | Defaults to C<false>. | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =head2 strict_validation | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | If set to C<true>, dies if a call to C<swagger_response> doesn't find a schema for its response. | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | Defaults to C<false>. | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =head1 PLUGIN KEYWORDS | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head2 swagger_path $description, \%args, $route | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | swagger_path { | ||||||||||||||||||||||||||||
| 398 | description => 'Returns info about a judge', | ||||||||||||||||||||||||||||
| 399 | }, | ||||||||||||||||||||||||||||
| 400 | get '/judge/:judge_name' => sub { | ||||||||||||||||||||||||||||
| 401 | ...; | ||||||||||||||||||||||||||||
| 402 | }; | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | Registers a route as a swagger path item in the swagger document. | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | C<%args> is optional. | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | The C<$description> is optional as well, and can also be defined as part of the | ||||||||||||||||||||||||||||
| 409 | C<%args>. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | # equivalent to the main example | ||||||||||||||||||||||||||||
| 412 | swagger_path 'Returns info about a judge', | ||||||||||||||||||||||||||||
| 413 | get '/judge/:judge_name' => sub { | ||||||||||||||||||||||||||||
| 414 | ...; | ||||||||||||||||||||||||||||
| 415 | }; | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | If the C<$description> spans many lines, it will be left-trimmed. | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | swagger_path q{ | ||||||||||||||||||||||||||||
| 420 | Returns info about a judge. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | Some more documentation can go here. | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | And this will be seen as a performatted block | ||||||||||||||||||||||||||||
| 425 | by swagger. | ||||||||||||||||||||||||||||
| 426 | }, | ||||||||||||||||||||||||||||
| 427 | get '/judge/:judge_name' => sub { | ||||||||||||||||||||||||||||
| 428 | ...; | ||||||||||||||||||||||||||||
| 429 | }; | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =head3 Supported arguments | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =over | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =item method | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | The HTTP method (GET, POST, etc) for the path item. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Defaults to the route's method. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =item path | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | The url for the path item. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | Defaults to the route's path. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =item description | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | The path item's description. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =item tags | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Optional arrayref of tags assigned to the path. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =item parameters | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | List of parameters for the path item. Must be an arrayref or a hashref. | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | Route parameters are automatically populated. E.g., | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | swagger_path | ||||||||||||||||||||||||||||
| 462 | get '/judge/:judge_name' => { ... }; | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | is equivalent to | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | swagger_path { | ||||||||||||||||||||||||||||
| 467 | parameters => [ | ||||||||||||||||||||||||||||
| 468 | { name => 'judge_name', in => 'path', required => 1, type => 'string' }, | ||||||||||||||||||||||||||||
| 469 | ] | ||||||||||||||||||||||||||||
| 470 | }, | ||||||||||||||||||||||||||||
| 471 | get '/judge/:judge_name' => { ... }; | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | If the parameters are passed as a hashref, the keys are the names of the parameters, and they will | ||||||||||||||||||||||||||||
| 474 | appear in the swagger document following their alphabetical order. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | If the parameters are passed as an arrayref, they will appear in the document in the order | ||||||||||||||||||||||||||||
| 477 | in which they are passed. Additionally, each parameter can be given as a hashref, or can be a | ||||||||||||||||||||||||||||
| 478 | C<< name => arguments >> pair. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | In both format, for the key/value pairs, a string value is considered to be the | ||||||||||||||||||||||||||||
| 481 | C<description> of the parameter. | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | Finally, if not specified explicitly, the C<in> argument of a parameter defaults to C<query>, | ||||||||||||||||||||||||||||
| 484 | and its type to C<string>. | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | parameters => [ | ||||||||||||||||||||||||||||
| 487 | { name => 'bar', in => 'path', required => 1, type => 'string' }, | ||||||||||||||||||||||||||||
| 488 | { name => 'foo', in => 'query', type => 'string', description => 'yadah' }, | ||||||||||||||||||||||||||||
| 489 | ], | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | # equivalent arrayref with mixed pairs/non-pairs | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | parameters => [ | ||||||||||||||||||||||||||||
| 494 | { name => 'bar', in => 'path', required => 1, type => 'string' }, | ||||||||||||||||||||||||||||
| 495 | foo => { in => 'query', type => 'string', description => 'yadah' }, | ||||||||||||||||||||||||||||
| 496 | ], | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | # equivalent hashref format | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | parameters => { | ||||||||||||||||||||||||||||
| 501 | bar => { in => 'path', required => 1, type => 'string' }, | ||||||||||||||||||||||||||||
| 502 | foo => { in => 'query', type => 'string', description => 'yadah' }, | ||||||||||||||||||||||||||||
| 503 | }, | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | # equivalent, using defaults | ||||||||||||||||||||||||||||
| 506 | parameters => { | ||||||||||||||||||||||||||||
| 507 | bar => { in => 'path', required => 1 }, | ||||||||||||||||||||||||||||
| 508 | foo => 'yadah', | ||||||||||||||||||||||||||||
| 509 | }, | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | =item responses | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | Possible responses from the path. Must be a hashref. | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | swagger_path { | ||||||||||||||||||||||||||||
| 516 | responses => { | ||||||||||||||||||||||||||||
| 517 | default => { description => 'The judge information' } | ||||||||||||||||||||||||||||
| 518 | }, | ||||||||||||||||||||||||||||
| 519 | }, | ||||||||||||||||||||||||||||
| 520 | get '/judge/:judge_name' => { ... }; | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | If the key C<example> is given (instead of C<examples> as defined by the Swagger specs), | ||||||||||||||||||||||||||||
| 523 | and the serializer used by the application is L<Dancer::Serializer::JSON> or L<Dancer::Serializer::YAML>, | ||||||||||||||||||||||||||||
| 524 | the example will be expanded to have the right content-type key. | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | swagger_path { | ||||||||||||||||||||||||||||
| 527 | responses => { | ||||||||||||||||||||||||||||
| 528 | default => { example => { fullname => 'Mary Ann Murphy' } } | ||||||||||||||||||||||||||||
| 529 | }, | ||||||||||||||||||||||||||||
| 530 | }, | ||||||||||||||||||||||||||||
| 531 | get '/judge/:judge_name' => { ... }; | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | # equivalent to | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | swagger_path { | ||||||||||||||||||||||||||||
| 536 | responses => { | ||||||||||||||||||||||||||||
| 537 | default => { examples => { 'application/json' => { fullname => 'Mary Ann Murphy' } } } | ||||||||||||||||||||||||||||
| 538 | }, | ||||||||||||||||||||||||||||
| 539 | }, | ||||||||||||||||||||||||||||
| 540 | get '/judge/:judge_name' => { ... }; | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | The special key C<template> will not appear in the Swagger doc, but will be | ||||||||||||||||||||||||||||
| 543 | used by the C<swagger_template> plugin keyword. | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | =back | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | =head2 swagger_template $code, $args | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | swagger_path { | ||||||||||||||||||||||||||||
| 550 | responses => { | ||||||||||||||||||||||||||||
| 551 | 404 => { template => sub { +{ error => "judge '$_[0]' not found" } } | ||||||||||||||||||||||||||||
| 552 | }, | ||||||||||||||||||||||||||||
| 553 | }, | ||||||||||||||||||||||||||||
| 554 | get '/judge/:judge_name' => { | ||||||||||||||||||||||||||||
| 555 | my $name = param('judge_name'); | ||||||||||||||||||||||||||||
| 556 | return swagger_template 404, $name unless in_db($name); | ||||||||||||||||||||||||||||
| 557 | ...; | ||||||||||||||||||||||||||||
| 558 | }; | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | Calls the template for the C<$code> response, passing it C<$args>. If C<$code> is numerical, also set | ||||||||||||||||||||||||||||
| 561 | the response's status to that value. | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | =head2 swagger_auto_discover skip => \@list | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | Populates the Swagger document with information of all | ||||||||||||||||||||||||||||
| 566 | the routes of the application. | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | Accepts an optional C<skip> parameter that takes an arrayref of | ||||||||||||||||||||||||||||
| 569 | routes that shouldn't be added to the Swagger document. The routes | ||||||||||||||||||||||||||||
| 570 | can be specified as-is, or via regular expressions. If no skip list is given, defaults to | ||||||||||||||||||||||||||||
| 571 | the c<auto_discover_skip> configuration value. | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | swagger_auto_discover skip => [ '/swagger.json', qr#^/doc/# ]; | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | The information of a route won't be altered if it's | ||||||||||||||||||||||||||||
| 576 | already present in the document. | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | If a route has path parameters, they will be automatically | ||||||||||||||||||||||||||||
| 579 | added as such in the C<parameters> section. | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | Routes defined as regexes are skipped, as there is no clean way | ||||||||||||||||||||||||||||
| 582 | to automatically make them look nice. | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | # will be picked up | ||||||||||||||||||||||||||||
| 585 | get '/user' => ...; | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | # ditto, as '/user/{user_id}' | ||||||||||||||||||||||||||||
| 588 | get '/user/:user_id => ...; | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | # won't be picked up | ||||||||||||||||||||||||||||
| 591 | get qr#/user/(\d+)# => ...; | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | Note that routes defined after C<swagger_auto_discover> has been called won't | ||||||||||||||||||||||||||||
| 594 | be added to the Swagger document. Typically, you'll want C<swagger_auto_discover> | ||||||||||||||||||||||||||||
| 595 | to be called at the very end of your module. Alternatively, C<swagger_auto_discover> | ||||||||||||||||||||||||||||
| 596 | can be called more than once safely -- which can be useful if an application creates | ||||||||||||||||||||||||||||
| 597 | routes dynamically. | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | =head2 swagger_definition $name => $definition, ... | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | Adds a schema (or more) to the definition section of the Swagger document. | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | swagger_definition 'Judge' => { | ||||||||||||||||||||||||||||
| 604 | type => 'object', | ||||||||||||||||||||||||||||
| 605 | required => [ 'fullname' ], | ||||||||||||||||||||||||||||
| 606 | properties => { | ||||||||||||||||||||||||||||
| 607 | fullname => { type => 'string' }, | ||||||||||||||||||||||||||||
| 608 | seasons => { type => 'array', items => { type => 'integer' } }, | ||||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||
| 610 | }; | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | The function returns the reference to the definition that can be then used where | ||||||||||||||||||||||||||||
| 613 | schemas are used. | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | my $Judge = swagger_definition 'Judge' => { ... }; | ||||||||||||||||||||||||||||
| 616 | # $Judge is now the hashref '{ '$ref' => '#/definitions/Judge' }' | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | # later on... | ||||||||||||||||||||||||||||
| 619 | swagger_path { | ||||||||||||||||||||||||||||
| 620 | responses => { | ||||||||||||||||||||||||||||
| 621 | default => { schema => $Judge }, | ||||||||||||||||||||||||||||
| 622 | }, | ||||||||||||||||||||||||||||
| 623 | }, | ||||||||||||||||||||||||||||
| 624 | get '/judge/:name' => sub { ... }; | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | =head1 EXAMPLES | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | See the F<examples/> directory of the distribution for a working example. | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | =over | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | =item L<http://swagger.io/|Swagger> | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | =back | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | This software is copyright (c) 2021, 2016, 2015 by Yanick Champoux. | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 647 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | =cut | ||||||||||||||||||||||||||||