Yancy/Plugin/Editor.pm

filter: Statementstmt 96.9% 190 / 196 Branchbran 82.6% 76 / 92 Conditioncond 64.4% 58 / 90 MC/DCmcdc 31.9% 15 / 47 Subroutinesub 90.9% 20 / 22 Podpod 100% 1 / 1 totaltotal 80.3% 360 / 448 CC 91 SCAR 28.5
CC
91
Cov
86%
_openapi_find_schema_name
29.4
register
25.9
_openapi_spec_from_schema
18.3
CRAP 17.3
« Yancy/Controller/Yancy.pm ↑ Summary Yancy/Plugin/Form/Bootstrap4.pm »
1 package Yancy::Plugin::Editor;
2 20 our $VERSION = '1.088';
3 # ABSTRACT: Yancy content editor, admin, and management application
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use Mojolicious::Lite;
8 #pod # The default editor at /yancy
9 #pod plugin Yancy => {
10 #pod backend => 'sqlite://myapp.db',
11 #pod read_schema => 1,
12 #pod editor => {
13 #pod require_user => { can_edit => 1 },
14 #pod },
15 #pod };
16 #pod
17 #pod # Enable another editor for blog users
18 #pod app->plugin->yancy( Editor => {
19 #pod moniker => 'blog_editor',
20 #pod backend => app->yancy->backend,
21 #pod schema => { blog_posts => app->yancy->schema( 'blog_posts' ) },
22 #pod route => app->routes->any( '/blog/editor' ),
23 #pod require_user => { can_blog => 1 },
24 #pod } );
25 #pod
26 #pod =head1 DESCRIPTION
27 #pod
28 #pod This plugin contains the Yancy editor application which allows editing
29 #pod the data in a L<Yancy::Backend>.
30 #pod
31 #pod =head1 CONFIGURATION
32 #pod
33 #pod This plugin has the following configuration options.
34 #pod
35 #pod =head2 backend
36 #pod
37 #pod The backend to use for this editor. Defaults to the default backend
38 #pod configured in the L<main Yancy plugin|Mojolicious::Plugin::Yancy>.
39 #pod
40 #pod =head2 schema
41 #pod
42 #pod The schema to use to build the editor application. This may not
43 #pod necessarily be the full and exact schema supported by the backend: You
44 #pod can remove certain fields from this editor instance to protect them, for
45 #pod example.
46 #pod
47 #pod If not given, will use L<Yancy::Backend/read_schema> to read the schema
48 #pod from the backend.
49 #pod
50 #pod =head2 openapi
51 #pod
52 #pod Instead of L</schema>, you can pass a full OpenAPI spec to this editor.
53 #pod This is deprecated; see L<Yancy::Guides::Upgrading>.
54 #pod
55 #pod =head2 default_controller
56 #pod
57 #pod The default controller for API routes. Defaults to
58 #pod L<Yancy::Controller::Yancy>. Building a custom controller can allow for
59 #pod customizing how the editor works and what content it displays to which
60 #pod users.
61 #pod
62 #pod =head2 moniker
63 #pod
64 #pod The name of this editor instance. Used to build helper names and route
65 #pod names. Defaults to C<editor>. Other plugins may rely on there being
66 #pod a default editor named C<editor>. Additional instances should have
67 #pod different monikers.
68 #pod
69 #pod =head2 route
70 #pod
71 #pod A base route to add the editor to. This allows you to customize the URL
72 #pod and add authentication or authorization. Defaults to allowing access to
73 #pod the Yancy web application under C</yancy>, and the REST API under
74 #pod C</yancy/api>.
75 #pod
76 #pod This can be a string or a L<Mojolicious::Routes::Route> object.
77 #pod
78 #pod =head2 return_to
79 #pod
80 #pod The URL to use for the "Back to Application" link. Defaults to C</>.
81 #pod
82 #pod =head2 title
83 #pod
84 #pod The title of the page, shown in the title bar and the page header. Defaults to C<Yancy>.
85 #pod
86 #pod =head2 host
87 #pod
88 #pod The host to use for the generated OpenAPI spec. Defaults to the current system's hostname
89 #pod (via L<Sys::Hostname>).
90 #pod
91 #pod =head2 info
92 #pod
93 #pod An OpenAPI info object, as a Perl hashref. See L<the OpenAPI 2.0
94 #pod spec|https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#infoObject>
95 #pod for what keys are allowed in this hashref.
96 #pod
97 #pod =head1 HELPERS
98 #pod
99 #pod =head2 yancy.editor.include
100 #pod
101 #pod $app->yancy->editor->include( $template_name );
102 #pod
103 #pod Include a template in the editor, before the rest of the editor. Use this
104 #pod to add your own L<Vue.JS|http://vuejs.org> components to the editor.
105 #pod
106 #pod =head2 yancy.editor.menu
107 #pod
108 #pod $app->yancy->editor->menu( $category, $title, $config );
109 #pod
110 #pod Add a menu item to the editor. The C<$category> is the title of the category
111 #pod in the sidebar. C<$title> is the title of the menu item. C<$config> is a
112 #pod hash reference with the following keys:
113 #pod
114 #pod =over
115 #pod
116 #pod =item component
117 #pod
118 #pod The name of a Vue.JS component to display for this menu item. The
119 #pod component will take up the entire main area of the application, and will
120 #pod be kept active even after another menu item is selected.
121 #pod
122 #pod =back
123 #pod
124 #pod Yancy plugins should use the category C<Plugins>. Other categories are
125 #pod available for custom applications.
126 #pod
127 #pod app->yancy->editor->include( 'plugin/editor/custom_element' );
128 #pod app->yancy->editor->menu(
129 #pod 'Plugins', 'Custom Item',
130 #pod {
131 #pod component => 'custom-element',
132 #pod },
133 #pod );
134 #pod __END__
135 #pod @@ plugin/editor/custom_element.html.ep
136 #pod <template id="custom-element-template">
137 #pod <div :id="'custom-element'">
138 #pod Hello, world!
139 #pod </div>
140 #pod </template>
141 #pod %= javascript begin
142 #pod Vue.component( 'custom-element', {
143 #pod template: '#custom-element-template',
144 #pod } );
145 #pod % end
146 #pod
147 #pod =for html
148 #pod <img alt="screenshot of yancy editor showing custom element"
149 #pod src="https://raw.github.com/preaction/Yancy/master/eg/doc-site/public/screenshot-custom-element.png?raw=true"
150 #pod style="width: 600px; max-width: 100%;" width="600" />
151 #pod
152 #pod =head2 yancy.editor.route
153 #pod
154 #pod Get the route where the editor will appear.
155 #pod
156 #pod =head2 yancy.editor.openapi
157 #pod
158 #pod my $openapi = $c->yancy->openapi;
159 #pod
160 #pod Get the L<Mojolicious::Plugin::OpenAPI> object containing the OpenAPI
161 #pod interface for this Yancy API.
162 #pod
163 #pod =head1 TEMPLATES
164 #pod
165 #pod To override these templates, add your own at the designated path inside
166 #pod your app's C<templates/> directory.
167 #pod
168 #pod =head2 yancy/editor.html.ep
169 #pod
170 #pod This is the main Yancy web application. You should not override this.
171 #pod Instead, use the L</yancy.editor.include> helper to add new components.
172 #pod If there is something you can't do using the include helper, consider
173 #pod L<asking how on the Github issues board|https://github.com/preaction/Yancy/issues>
174 #pod or L<filing a bug report or feature request|https://github.com/preaction/Yancy/issues>.
175 #pod
176 #pod =head1 SEE ALSO
177 #pod
178 #pod L<Yancy::Guides::Schema>, L<Mojolicious::Plugin::Yancy>, L<Yancy>
179 #pod
180 #pod =cut
181  
182 20 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
183 20 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
184 20 ▶use Mojo::JSON qw( true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
185 20 ▶use Mojo::Util qw( url_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
186 20 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
187 20 ▶use Yancy::Util qw( derp currym json_validator load_backend );
Subroutine BEGIN: called CC=1 SCAR=0.0
188  
189 20 has moniker => 'editor';
190 20 has route =>;
191 5 ▶has includes => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
192 5 ▶has menu_items => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
193 20 has model =>;
194 20 has schema =>;
195 20 has app => undef, weak => 1;
196  
197 198 ▶sub _helper_name {
Subroutine _helper_name: called CC=1 SCAR=0.0
198 198 my ( $self, $name ) = @_;
199 198 return join '.', 'yancy', $self->moniker, $name;
200 }
201  
202 53 ▶sub register {
Subroutine register: called CC=23 SCAR=32.5
Pod documented
203 53 my ( $self, $app, $config ) = @_;
204  
205 53 ▶ $config->{title} //= $app->l( 'Yancy' );
Condition33%1/3
$config->{"title"} //= $app->l("Yancy")
l!l&&r!l&&!r
0530
MC/DC0%0/2
$config->{"title"} //= $app->l("Yancy")
$config->{"title"} $app->l("Yancy")
Truth table33%1/3
A //= B
A= $config->{"title"}B= $app->l("Yancy")
ABresult
1X1
011
000
206 53 ▶ $config->{return_label} //= $app->l( 'Back to Application' );
Condition33%1/3
$config->{"return_label"} //= $app->l("Back to Application")
l!l&&r!l&&!r
0530
MC/DC0%0/2
$config->{"return_label"} //= $app->l("Back to Application")
$config->{"return_label"} $app->l("Back to Application")
Truth table33%1/3
A //= B
A= $config->{"return_label"}B= $app->l("Back to Application")
ABresult
1X1
011
000
207  
208 53 ▶ if ( $config->{backend} || $config->{schema} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'backend'} or $config->{'schema'}) { } 53 0 53
Condition66%2/3
$config->{"backend"} or $config->{"schema"}
l!l&&r!l&&!r
1520
MC/DC0%0/2
$config->{"backend"} or $config->{"schema"}
$config->{"backend"} $config->{"schema"}
Truth table66%2/3
A or B
A= $config->{"backend"}B= $config->{"schema"}
ABresult
1X1
011
000
209 my $backend = $config->{backend} ? (
210 blessed $config->{backend}
211 ? $config->{backend}
212 : load_backend( $config->{backend} )
213 53 ▶ ) : $app->yancy->backend;
Branch: 2 branches
BranchTrueFalseTotal
$config->{'backend'} ? : 1 52 53
builtin::blessed($config->{'backend'}) ? : 1 0 1
214 my $model = Yancy::Model->new(
215 backend => $backend,
216 ( schema => $config->{schema} )x!!$config->{schema},
217 ( read_schema => $config->{read_schema} )x!!exists $config->{read_schema},
218 53 );
219 53 $self->model( $model );
220 }
221 else {
222 0 $self->model( $app->yancy->model );
223 }
224 53 $self->schema( my $schema = $config->{schema} );
225 53 $self->app( $app );
226  
227 53 for my $key ( grep exists $config->{ $_ }, qw( moniker ) ) {
228 1 $self->$key( $config->{ $key } );
229 }
230 53 ▶ $config->{default_controller} //= $config->{api_controller} // 'Yancy';
Condition50%2/4
$config->{"default_controller"} //= $config->{"api_controller"} // "Yancy"
l!l
053
$config->{"api_controller"} // "Yancy"
l!l
053
MC/DC0%0/1
$config->{"default_controller"} //= $config->{"api_controller"} // "Yancy"
$config->{"default_controller"}
Truth table50%1/2
A
A= $config->{"default_controller"}
Aresult
11
00
231 53 ▶ if ( $config->{api_controller} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"api_controller"}) 0 53 53
232 0 derp 'api_controller configuration is deprecated. Use editor.default_controller instead';
233 }
234  
235 # XXX: Throw an error if there is already a route here
236 53 my $route = $app->yancy->routify( $config->{route}, '/yancy' );
237 53 ▶ $route->to( return_to => $config->{return_to} // '/' );
Condition50%1/2
$config->{"return_to"} // "/"
l!l
053
MC/DC0%0/1
$config->{"return_to"} // "/"
$config->{"return_to"}
Truth table50%1/2
A
A= $config->{"return_to"}
Aresult
11
00
238  
239 # Create authentication for editor. We need to delay fetching this
240 # callback until after startup is complete so that any auth plugin
241 # can be added.
242 95 ▶ my $auth_under = sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
243 95 my ( $c ) = @_;
244 state $auth_cb = $c->yancy->can( 'auth' )
245 && $c->yancy->auth->can( 'require_user' )
246 95 ▶ && $c->yancy->auth->require_user( $config->{require_user} || () );
Condition66%6/9
$c->yancy->can('auth') && $c->yancy->auth->can('require_user') && $c->yancy->auth->require_user($config->{'require_user'} || ())
!ll&&!rl&&r
1101
$c->yancy->can("auth") and $c->yancy->auth->can("require_user")
!ll&&!rl&&r
921
$config->{'require_user'} || ()
l!l&&r!l&&!r
010
MC/DC66%2/3
$c->yancy->can('auth') && $c->yancy->auth->can('require_user') && $c->yancy->auth->require_user($config->{'require_user'} || ())
$c->yancy->can("auth") $c->yancy->auth->can("require_user") $c->yancy->auth->require_user($config->{'require_user'} || ())
MC/DC0%0/2
$config->{'require_user'} || ()
$config->{'require_user'} ()
Truth table75%3/4
A and B && C
A= $c->yancy->can("auth")B= $c->yancy->auth->can("require_user")C= $c->yancy->auth->require_user($config->{'require_user'} || ())
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A || B
A= $config->{'require_user'}B= ()
ABresult
1X1
011
000
247 95 ▶ if ( !$auth_cb && !exists $config->{require_user} && !defined $config->{route} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth_cb or exists $config->{"require_user"} or defined $config->{"route"}) 4 91 95
Condition83%5/6
$auth_cb or exists $config->{"require_user"} or defined $config->{"route"}
l!l&&r!l&&!r
9104
$auth_cb or exists $config->{"require_user"}
l!l&&r!l&&!r
2894
MC/DC66%2/3
$auth_cb or exists $config->{"require_user"} or defined $config->{"route"}
$auth_cb exists $config->{"require_user"} defined $config->{"route"}
Truth table75%3/4
A or B or C
A= $auth_cbB= exists $config->{"require_user"}C= defined $config->{"route"}
ABCresult
1XX1
01X1
0011
0000
248 4 $app->log->warn(
249 qq{*** Cannot verify that admin editor is behind authentication.\n}
250 . qq{Add a Yancy Auth plugin, add a `route` to the Yancy plugin config,\n}
251 . qq{or set `editor.require_user => undef` to silence this warning\n}
252 );
253 }
254 95 ▶ return $auth_cb ? $auth_cb->( $c ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$auth_cb ? : 2 93 95
255 53 };
256 53 $route = $route->under( $auth_under );
257  
258 # First create the OpenAPI schema and API URL
259 53 my $spec;
260 53 ▶ if ( $config->{openapi} && keys %{ $config->{openapi} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'openapi'} and keys %{$$config{'openapi'};}) { } 4 49 53
Condition100%3/3
$config->{"openapi"} and keys %{$$config{"openapi"};}
!ll&&!rl&&r
4364
MC/DC100%2/2
$config->{"openapi"} and keys %{$$config{"openapi"};}
$config->{"openapi"} keys %{$$config{"openapi"};}
Truth table100%3/3
A and B
A= $config->{"openapi"}B= keys %{$$config{"openapi"};}
ABresult
0X0
100
111
261 4 $spec = $config->{openapi};
262 }
263 else {
264 # Add OpenAPI spec
265 49 $spec = $self->_openapi_spec_from_schema( $config );
266 }
267 53 $self->_openapi_spec_add_mojo( $spec, $config );
268  
269 51 my $openapi = $app->plugin( OpenAPI => {
270 route => $route->any( '/api' )->to( model => $self->model )->name( 'yancy.api' ),
271 spec => $spec,
272 default_response_name => '_Error',
273 validator => json_validator(),
274 } );
275 51 $_->to(format => 'json') for (@{$openapi->route->children});
276 0 ▶ $app->helper( 'yancy.openapi' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
277 0 derp 'yancy.openapi helper is deprecated. Use yancy.editor.openapi instead';
278 0 return $openapi;
279 51 } );
280 51 ▶ $app->helper( $self->_helper_name( 'openapi' ) => sub { $openapi } );
Subroutine __ANON__: called CC=1 SCAR=0.0
281  
282 # Do some sanity checks on the config to make sure nothing bad
283 # happens
284 51 for my $schema_name ( keys %$schema ) {
285 335 ▶ if ( my $view = $schema->{ $schema_name }{ 'x-view' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $view = $schema->{$schema_name}{"x-view"}) 76 259 335
286 76 $schema_name = $view->{schema};
287 }
288 335 ▶ next if $schema->{ $schema_name }{ 'x-view' };
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{$schema_name}{"x-view"} 0 335 335
289 335 ▶ if ( my $list_cols = $schema->{ $schema_name }{ 'x-list-columns' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $list_cols = $schema->{$schema_name}{"x-list-columns"}) 167 168 335
290 167 for my $col ( @$list_cols ) {
291 327 ▶ if ( ref $col eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col eq 'HASH') { } 8 319 327
292 # Check template for columns
293 8 my @cols = $col->{template} =~ m'\{([^{]+)\}'g;
294 8 ▶ if ( my ( $col ) = grep { !exists $schema->{ $schema_name }{ properties }{ $_ } } @cols ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($col) = grep({not exists $schema->{$schema_name}{"properties"}{$_};} @cols)) 1 7 8
295 1 die sprintf q{Column "%s" in x-list-columns template does not exist in schema "%s"},
296 $col, $schema_name;
297 }
298 }
299 else {
300 319 ▶ if ( !exists $schema->{ $schema_name }{ properties }{ $col } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $schema->{$schema_name}{"properties"}{$col}) 1 318 319
301 1 die sprintf q{Column "%s" in x-list-columns does not exist in schema "%s"},
302 $col, $schema_name;
303 }
304 }
305 }
306 }
307 }
308  
309 # Now create the routes and helpers the editor needs
310 $route->get( '/' )->name( 'yancy.index' )
311 ->to(
312 template => 'yancy/index',
313 controller => $config->{default_controller},
314 action => 'index',
315 api_url => $openapi->route->render,
316 title => $config->{title},
317 return_label => $config->{return_label},
318 49 );
319 $route->post( '/upload' )->name( 'yancy.editor.upload' )
320 1 ▶ ->to( cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 1 my ( $c ) = @_;
322 1 my $upload = $c->param( 'upload' );
323 1 my $path = $c->yancy->file->write( $upload );
324 1 $c->res->headers->location( $path );
325 1 $c->render( status => 201, text => $path );
326 49 } );
327  
328 49 $app->helper( $self->_helper_name( 'menu' ), currym( $self, '_helper_menu' ) );
329 49 $app->helper( $self->_helper_name( 'include' ), currym( $self, '_helper_include' ) );
330 49 ▶ $app->helper( $self->_helper_name( 'route' ), sub { $route } );
Subroutine __ANON__: called CC=1 SCAR=0.0
331 0 ▶ $app->helper( 'yancy.route', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
332 0 derp 'yancy.route helper is deprecated. Use yancy.editor.route instead';
333 0 return $self->route;
334 49 } );
335 49 $self->route( $route );
336  
337  
338 }
339  
340 6 ▶sub _helper_include {
Subroutine _helper_include: called CC=2 SCAR=6.9
341 6 my ( $self, $c, @includes ) = @_;
342 6 ▶ if ( @includes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@includes) 1 5 6
343 1 push @{ $self->includes }, @includes;
344 }
345 6 return $self->includes;
346 }
347  
348 6 ▶sub _helper_menu {
Subroutine _helper_menu: called CC=2 SCAR=6.9
349 6 my ( $self, $c, $category, $title, $config ) = @_;
350 6 ▶ if ( $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) 1 5 6
351 1 push @{ $self->menu_items->{ $category } }, {
352 %$config,
353 title => $title,
354 };
355 }
356 6 return $self->menu_items;
357 }
358  
359 581 ▶sub _openapi_find_schema_name {
Subroutine _openapi_find_schema_name: called CC=23 SCAR=33.8
360 581 my ( $self, $path, $pathspec ) = @_;
361 581 ▶ return $pathspec->{'x-schema'} if $pathspec->{'x-schema'};
Branch: 1 branch
BranchTrueFalseTotal
if $pathspec->{"x-schema"} 0 581 581
362 581 my $schema_name;
363 581 for my $method ( grep !/^(parameters$|x-)/, keys %{ $pathspec } ) {
364 1214 my $op_spec = $pathspec->{ $method };
365 1214 my $schema;
366 1214 ▶ if ( $method eq 'get' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'get') { } 579 635 1214
elsif ($method =~ /^(put|post)$/u) { } 423 212 635
367 # d is in case only has "default" response
368 579 my ($response) = grep /^[2d]/, sort keys %{ $op_spec->{responses} };
369 579 my $response_spec = $op_spec->{responses}{$response};
370 579 ▶ next unless $schema = $response_spec->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema = $response_spec->{"schema"} 0 579 579
371 } elsif ( $method =~ /^(put|post)$/ ) {
372 my @body_params = grep 'body' eq ($_->{in} // ''),
373 423 ▶ @{ $op_spec->{parameters} || [] },
Condition50%1/2
$op_spec->{'parameters'} || []
l!l
4230
MC/DC0%0/1
$op_spec->{'parameters'} || []
$op_spec->{'parameters'}
Truth table50%1/2
A
A= $op_spec->{'parameters'}
Aresult
11
00
374 423 ▶ @{ $pathspec->{parameters} || [] },
Condition75%3/4
$_->{"in"} // ""
l!l
6500
$pathspec->{'parameters'} || []
l!l
211212
MC/DC0%0/1
$_->{"in"} // ""
$_->{"in"}
MC/DC100%1/1
$pathspec->{'parameters'} || []
$pathspec->{'parameters'}
Truth table50%1/2
A
A= $_->{"in"}
Aresult
11
00
Truth table100%2/2
A
A= $pathspec->{'parameters'}
Aresult
11
00
375 ;
376 423 ▶ die "No more than 1 'body' parameter allowed" if @body_params > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @body_params > 1 0 423 423
377 423 ▶ next unless $schema = $body_params[0]->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema = $body_params[0]{"schema"} 0 423 423
378 }
379 next unless my $this_ref =
380 $schema->{'$ref'} ||
381 ( $schema->{items} && $schema->{items}{'$ref'} ) ||
382 1214 ▶ ( $schema->{properties} && $schema->{properties}{items} && $schema->{properties}{items}{'$ref'} );
Branch: 1 branch
BranchTrueFalseTotal
unless my $this_ref = $schema->{"\$ref"} || $schema->{"items"} && $schema->{"items"}{"\$ref"} || $schema->{"properties"} && $schema->{"properties"}{"items"} && $schema->{"properties"}{"items"}{"\$ref"} 502 712 1214
Condition60%9/15
$schema->{'$ref'} || $schema->{'items'} && $schema->{'items'}{'$ref'} || $schema->{'properties'} && $schema->{'properties'}{'items'} && $schema->{'properties'}{'items'}{'$ref'}
l!l&&r!l&&!r
7120502
$schema->{"\$ref"} or $schema->{"items"} and $schema->{"items"}{"\$ref"}
l!l&&r!l&&!r
7120502
$schema->{"items"} and $schema->{"items"}{"\$ref"}
!ll&&!rl&&r
50200
$schema->{"properties"} and $schema->{"properties"}{"items"} and $schema->{"properties"}{"items"}{"\$ref"}
!ll&&!rl&&r
2122900
$schema->{"properties"} and $schema->{"properties"}{"items"}
!ll&&!rl&&r
2120290
MC/DC16%1/6
$schema->{'$ref'} || $schema->{'items'} && $schema->{'items'}{'$ref'} || $schema->{'properties'} && $schema->{'properties'}{'items'} && $schema->{'properties'}{'items'}{'$ref'}
$schema->{"\$ref"} $schema->{"items"} $schema->{"items"}{"\$ref"} $schema->{"properties"} $schema->{"properties"}{"items"} $schema->{"properties"}{"items"}{"\$ref"}
Truth table30%3/10
A or B and C || D and E and F
A= $schema->{"\$ref"}B= $schema->{"items"}C= $schema->{"items"}{"\$ref"}D= $schema->{"properties"}E= $schema->{"properties"}{"items"}F= $schema->{"properties"}{"items"}{"\$ref"}
ABCDEFresult
1XXXXX1
011XXX1
00X1111
0101111
00X0XX0
00X10X0
00X1100
0100XX0
01010X0
0101100
383 712 ▶ next unless $this_ref =~ s:^#/definitions/::;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_ref =~ s[^#/definitions/][]u 0 712 712
384 712 ▶ die "$method '$path' = $this_ref but also '$schema_name'"
Branch: 1 branch
BranchTrueFalseTotal
if $this_ref and $schema_name and $this_ref ne $schema_name 0 712 712
Condition66%4/6
$this_ref and $schema_name and $this_ref ne $schema_name
!ll&&!rl&&r
5012110
$this_ref and $schema_name
!ll&&!rl&&r
0501211
MC/DC0%0/3
$this_ref and $schema_name and $this_ref ne $schema_name
$this_ref $schema_name $this_ref ne $schema_name
Truth table50%2/4
A and B and C
A= $this_refB= $schema_nameC= $this_ref ne $schema_name
ABCresult
0XX0
10X0
1100
1111
385 if $this_ref and $schema_name and $this_ref ne $schema_name;
386 712 $schema_name = $this_ref;
387 }
388 581 ▶ if ( !$schema_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_name) 80 501 581
389 80 ($schema_name) = $path =~ m#^/([^/]+)#;
390 80 ▶ die "No schema found in '$path'" if !$schema_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_name 0 80 80
391 }
392 581 $schema_name;
393 }
394  
395 # mutates $spec
396 53 ▶sub _openapi_spec_add_mojo {
Subroutine _openapi_spec_add_mojo: called CC=10 SCAR=23.5
397 53 my ( $self, $spec, $config ) = @_;
398 53 for my $path ( keys %{ $spec->{paths} } ) {
399 581 my $pathspec = $spec->{paths}{ $path };
400 581 my $schema = $self->_openapi_find_schema_name( $path, $pathspec );
401 die "Path '$path' had non-existent schema '$schema'"
402 581 ▶ if !$spec->{definitions}{$schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"definitions"}{$schema} 1 580 581
403 580 for my $method ( grep !/^(parameters$|x-)/, keys %{ $pathspec } ) {
404 1213 my $op_spec = $pathspec->{ $method };
405 1213 my $mojo = $self->_openapi_spec_infer_mojo( $path, $pathspec, $method, $op_spec );
406 # XXX Allow overriding controller on a per-schema basis
407 # This gives more control over how a certain schema's items
408 # are written/read from the database
409 1212 $mojo->{controller} = $config->{default_controller};
410 1212 $mojo->{schema} = $schema;
411 my @filters = (
412 1212 ▶ @{ $pathspec->{ 'x-filter' } || [] },
Condition50%1/2
$pathspec->{'x-filter'} || []
l!l
01212
MC/DC0%0/1
$pathspec->{'x-filter'} || []
$pathspec->{'x-filter'}
Truth table50%1/2
A
A= $pathspec->{'x-filter'}
Aresult
11
00
413 1212 ▶ @{ $op_spec->{ 'x-filter' } || [] },
Condition50%1/2
$op_spec->{'x-filter'} || []
l!l
01212
MC/DC0%0/1
$op_spec->{'x-filter'} || []
$op_spec->{'x-filter'}
Truth table50%1/2
A
A= $op_spec->{'x-filter'}
Aresult
11
00
414 );
415 1212 ▶ $mojo->{filters} = \@filters if @filters;
Branch: 1 branch
BranchTrueFalseTotal
if @filters 0 1212 1212
416 my @filters_out = (
417 1212 ▶ @{ $pathspec->{ 'x-filter-output' } || [] },
Condition50%1/2
$pathspec->{'x-filter-output'} || []
l!l
01212
MC/DC0%0/1
$pathspec->{'x-filter-output'} || []
$pathspec->{'x-filter-output'}
Truth table50%1/2
A
A= $pathspec->{'x-filter-output'}
Aresult
11
00
418 1212 ▶ @{ $op_spec->{ 'x-filter-output' } || [] },
Condition50%1/2
$op_spec->{'x-filter-output'} || []
l!l
01212
MC/DC0%0/1
$op_spec->{'x-filter-output'} || []
$op_spec->{'x-filter-output'}
Truth table50%1/2
A
A= $op_spec->{'x-filter-output'}
Aresult
11
00
419 );
420 1212 ▶ $mojo->{filters_out} = \@filters_out if @filters_out;
Branch: 1 branch
BranchTrueFalseTotal
if @filters_out 0 1212 1212
421 1212 $op_spec->{ 'x-mojo-to' } = $mojo;
422 }
423 }
424 }
425  
426 # for a given OpenAPI operation, figures out right values for 'x-mojo-to'
427 # to hook it up to the correct CRUD operation
428 1213 ▶sub _openapi_spec_infer_mojo {
Subroutine _openapi_spec_infer_mojo: called CC=12 SCAR=24.9
429 1213 my ( $self, $path, $pathspec, $method, $op_spec ) = @_;
430 my @path_params = grep 'path' eq ($_->{in} // ''),
431 1213 ▶ @{ $pathspec->{parameters} || [] },
Condition100%2/2
$pathspec->{'parameters'} || []
l!l
711502
MC/DC100%1/1
$pathspec->{'parameters'} || []
$pathspec->{'parameters'}
Truth table100%2/2
A
A= $pathspec->{'parameters'}
Aresult
11
00
432 1213 ▶ @{ $op_spec->{parameters} || [] },
Condition100%4/4
$_->{"in"} // ""
l!l
30551143
$op_spec->{'parameters'} || []
l!l
712501
MC/DC100%1/1
$_->{"in"} // ""
$_->{"in"}
MC/DC100%1/1
$op_spec->{'parameters'} || []
$op_spec->{'parameters'}
Truth table100%2/2
A
A= $_->{"in"}
Aresult
11
00
Truth table100%2/2
A
A= $op_spec->{'parameters'}
Aresult
11
00
433 ;
434 my ($id_field) = grep defined,
435 (map $_->{'x-id-field'}, $op_spec, $pathspec),
436 1213 ▶ (@path_params && $path_params[-1]{name});
Condition66%2/3
@path_params && $path_params[-1]{'name'}
!ll&&!rl&&r
5020711
MC/DC50%1/2
@path_params && $path_params[-1]{'name'}
@path_params $path_params[-1]{'name'}
Truth table66%2/3
A && B
A= @path_paramsB= $path_params[-1]{'name'}
ABresult
0X0
100
111
437 1213 ▶ if ( $method eq 'get' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($method eq 'get') { } 578 635 1213
elsif ($method eq 'post') { } 212 423 635
elsif ($method eq 'put') { } 211 212 423
elsif ($method eq 'delete') { } 211 1 212
438 # heuristic: is per-item if have a param in path
439 578 ▶ if ( $id_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_field) { } 289 289 578
440 # per-item - GET = "read"
441 return {
442 289 action => 'get',
443 format => 'json',
444 };
445 }
446 else {
447 # per-schema - GET = "list"
448 return {
449 289 action => 'list',
450 format => 'json',
451 };
452 }
453 } elsif ( $method eq 'post' ) {
454 return {
455 212 action => 'set',
456 format => 'json',
457 };
458 } elsif ( $method eq 'put' ) {
459 211 ▶ die "'$method' $path needs id_field" if !$id_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_field 0 211 211
460 return {
461 211 action => 'set',
462 format => 'json',
463 };
464 } elsif ( $method eq 'delete' ) {
465 211 ▶ die "'$method' $path needs id_field" if !$id_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_field 0 211 211
466 return {
467 211 action => 'delete',
468 format => 'json',
469 };
470 }
471 else {
472 1 die "Unknown method '$method'";
473 }
474 }
475  
476 49 ▶sub _openapi_spec_from_schema {
Subroutine _openapi_spec_from_schema: called CC=18 SCAR=29.1
477 49 my ( $self, $config ) = @_;
478 49 my ( %definitions, %paths );
479 49 my $app = $self->app;
480 49 my %parameters = (
481 '$limit' => {
482 name => '$limit',
483 type => 'integer',
484 in => 'query',
485 description => $app->l( 'OpenAPI $limit description' ),
486 },
487 '$offset' => {
488 name => '$offset',
489 type => 'integer',
490 in => 'query',
491 description => $app->l( 'OpenAPI $offset description' ),
492 },
493 '$order_by' => {
494 name => '$order_by',
495 type => 'string',
496 in => 'query',
497 pattern => '^(?:asc|desc):[^:,]+$',
498 description => $app->l( 'OpenAPI $order_by description' ),
499 },
500 '$match' => {
501 name => '$match',
502 type => 'string',
503 enum => [qw( any all )],
504 default => 'all',
505 in => 'query',
506 description => $app->l( 'OpenAPI $match description' ),
507 },
508 );
509 49 my $model = $self->model;
510 49 for my $schema_name ( $model->schema_names ) {
511 # Set some defaults so users don't have to type as much
512 276 my $schema = $model->schema( $schema_name );
513 276 my $json_schema = $schema->json_schema;
514 276 ▶ next if $json_schema->{ 'x-ignore' };
Branch: 1 branch
BranchTrueFalseTotal
if $json_schema->{"x-ignore"} 0 276 276
515 276 my $id_field = $schema->id_field;
516 276 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 16 260 276
517 276 my $props = $json_schema->{properties};
518 276 ▶ if ( !$props && $json_schema->{'x-view'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $props and $json_schema->{"x-view"}) 36 240 276
Condition66%2/3
not $props and $json_schema->{"x-view"}
!ll&&!rl&&r
240036
MC/DC50%1/2
not $props and $json_schema->{"x-view"}
not $props $json_schema->{"x-view"}
Truth table66%2/3
not(A) and B
A= not $propsB= $json_schema->{"x-view"}
ABresult
0X0
100
111
519 36 my $real_schema_name = $json_schema->{'x-view'}{schema};
520 36 $props = $model->schema( $real_schema_name )->json_schema->{properties};
521 }
522 276 my %props = %$props;
523  
524 276 $definitions{ $schema_name } = $json_schema;
525  
526 276 for my $prop ( keys %props ) {
527 1780 ▶ $props{ $prop }{ type } ||= 'string';
Condition50%1/2
$props{$prop}{"type"} ||= "string"
l!l
17800
MC/DC0%0/1
$props{$prop}{"type"} ||= "string"
$props{$prop}{"type"}
Truth table50%1/2
A
A= $props{$prop}{"type"}
Aresult
11
00
528 }
529  
530 $paths{ '/' . $schema_name } = {
531 get => {
532 description => $app->l( 'OpenAPI list description' ),
533 parameters => [
534 { '$ref' => '#/parameters/%24limit' },
535 { '$ref' => '#/parameters/%24offset' },
536 { '$ref' => '#/parameters/%24order_by' },
537 { '$ref' => '#/parameters/%24match' },
538 map {
539 1780 my $name = $_;
540 1780 ▶ my $type = ref $props{ $_ }{type} eq 'ARRAY' ? $props{ $_ }{type}[0] : $props{ $_ }{type};
Branch: 1 branch
BranchTrueFalseTotal
ref $props{$_}{'type'} eq 'ARRAY' ? : 483 1297 1780
541 1780 ▶ my $description = $app->l(
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'number' || $type eq 'integer' ? : 451 1329 1780
$type eq 'boolean' ? : 95 1234 1329
$type eq 'array' ? : 1 1233 1234
Condition100%3/3
$type eq "number" or $type eq "integer"
l!l&&r!l&&!r
124391329
MC/DC100%2/2
$type eq "number" or $type eq "integer"
$type eq "number" $type eq "integer"
Truth table100%3/3
A or B
A= $type eq "number"B= $type eq "integer"
ABresult
1X1
011
000
542 $type eq 'number' || $type eq 'integer' ? 'OpenAPI filter number description'
543 : $type eq 'boolean' ? 'OpenAPI filter boolean description'
544 : $type eq 'array' ? 'OpenAPI filter array description'
545 : 'OpenAPI filter string description'
546 );
547 {
548 1780 ▶ name => $name,
Branch: 2 branches
BranchTrueFalseTotal
$json_schema->{'x-view'} ? : 72 204 276
@id_fields > 1 ? : 16 188 204
549 in => 'query',
550 type => $type,
551 description => $app->l( 'OpenAPI filter description', $name ) . $description,
552 } } grep !exists( $props{ $_ }{'$ref'} ), sort keys %props,
553 ],
554 responses => {
555 200 => {
556 description => $app->l( 'OpenAPI list response' ),
557 schema => {
558 type => 'object',
559 required => [qw( items total )],
560 properties => {
561 total => {
562 type => 'integer',
563 description => $app->l( 'OpenAPI list total description' ),
564 },
565 items => {
566 type => 'array',
567 description => $app->l( 'OpenAPI list items description' ),
568 items => { '$ref' => "#/definitions/" . url_escape $schema_name },
569 },
570 offset => {
571 type => 'integer',
572 description => $app->l( 'OpenAPI list offset description' ),
573 },
574 },
575 },
576 },
577 default => {
578 description => $app->l( 'Unexpected error' ),
579 schema => { '$ref' => '#/definitions/_Error' },
580 },
581 },
582 },
583 $json_schema->{'x-view'} ? () : (post => {
584 parameters => [
585 {
586 name => "newItem",
587 in => "body",
588 required => true,
589 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
590 },
591 ],
592 responses => {
593 201 => {
594 description => $app->l( 'OpenAPI create response' ),
595 schema => {
596 @id_fields > 1
597 ? (
598 type => 'array',
599 items => [
600 map +{
601 128 '$ref' => '#/' . join '/', map { url_escape $_ }
602 'definitions', $schema_name, 'properties', $_,
603 }, @id_fields,
604 ],
605 )
606 : (
607 276 '$ref' => '#/' . join '/', map { url_escape $_ }
608 'definitions', $schema_name, 'properties', $id_fields[0],
609 ),
610 },
611 },
612 default => {
613 description => $app->l( "Unexpected error" ),
614 schema => { '$ref' => "#/definitions/_Error" },
615 },
616 },
617 }),
618 };
619  
620 $paths{ sprintf '/%s/{%s}', $schema_name, $id_field } = {
621 parameters => [
622 map +{
623 name => $_,
624 in => 'path',
625 required => true,
626 type => 'string',
627 'x-mojo-placeholder' => '*',
628 }, @id_fields
629 ],
630  
631 get => {
632 description => $app->l( 'OpenAPI get description' ),
633 responses => {
634 200 => {
635 description => $app->l( 'OpenAPI get response' ),
636 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
637 },
638 default => {
639 description => $app->l( "Unexpected error" ),
640 schema => { '$ref' => '#/definitions/_Error' },
641 }
642 }
643 },
644  
645 276 ▶ $json_schema->{'x-view'} ? () : (put => {
Branch: 1 branch
BranchTrueFalseTotal
$json_schema->{'x-view'} ? : 72 204 276
646 description => $app->l( 'OpenAPI update description' ),
647 parameters => [
648 {
649 name => "newItem",
650 in => "body",
651 required => true,
652 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
653 }
654 ],
655 responses => {
656 200 => {
657 description => $app->l( 'OpenAPI update response' ),
658 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
659 },
660 default => {
661 description => $app->l( "Unexpected error" ),
662 schema => { '$ref' => "#/definitions/_Error" },
663 }
664 }
665 },
666  
667 delete => {
668 description => $app->l( 'OpenAPI delete description' ),
669 responses => {
670 204 => {
671 description => $app->l( 'OpenAPI delete response' ),
672 },
673 default => {
674 description => $app->l( "Unexpected error" ),
675 schema => { '$ref' => '#/definitions/_Error' },
676 },
677 },
678 }),
679 };
680 }
681  
682 return {
683 info => $config->{info} || { title => $config->{title}, version => "1" },
684 swagger => '2.0',
685 49 ▶ host => $config->{host} // hostname(),
Condition40%2/5
$config->{'info'} || {'title', $config->{'title'}, 'version', '1'}
l!l
049
$config->{"host"} // hostname()
l!l&&r!l&&!r
0490
MC/DC0%0/1
$config->{'info'} || {'title', $config->{'title'}, 'version', '1'}
$config->{'info'}
MC/DC0%0/2
$config->{"host"} // hostname()
$config->{"host"} hostname()
Truth table50%1/2
A
A= $config->{'info'}
Aresult
11
00
Truth table33%1/3
A // B
A= $config->{"host"}B= hostname()
ABresult
1X1
011
000
686 basePath => '/api',
687 schemes => [qw( http )],
688 consumes => [qw( application/json )],
689 produces => [qw( application/json )],
690 definitions => {
691 _Error => {
692 'x-ignore' => 1, # In case we get round-tripped into a Yancy::Model
693 title => $app->l( 'OpenAPI error object' ),
694 type => 'object',
695 properties => {
696 errors => {
697 type => "array",
698 items => {
699 required => [qw( message )],
700 properties => {
701 message => {
702 type => "string",
703 description => $app->l( 'OpenAPI error message' ),
704 },
705 path => {
706 type => "string",
707 description => $app->l( 'OpenAPI error path' ),
708 }
709 }
710 }
711 }
712 }
713 },
714 %definitions,
715 },
716 paths => \%paths,
717 parameters => \%parameters,
718 };
719 }
720  
721  
722 20 1;
723  
724 __END__
« Yancy/Controller/Yancy.pm ↑ Summary Yancy/Plugin/Form/Bootstrap4.pm »