Yancy/Plugin/Editor.pm

filter: Statementstmt 67.9% 125 / 184 Branchbran 58.1% 57 / 98 Conditioncond 48.1% 38 / 79 MC/DCmcdc 14.6% 6 / 41 Subroutinesub 47.6% 10 / 21 Podpod 100% 1 / 1 totaltotal 55.9% 237 / 424 CC 88 SCAR 39.1
CC
88
Cov
61%
register
183.8
__ANON__
72.0
_openapi_find_schema_name
29.4
CRAP 50.1
« Yancy/Plugin/Auth/Password.pm ↑ Summary Yancy/Plugin/Auth/Token.pm »
1 package Yancy::Plugin::Editor;
2 20 our $VERSION = '1.078';
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 Mojo::JSON qw( true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
184 20 ▶use Mojo::Util qw( url_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
185 20 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
186 20 ▶use Yancy::Util qw( derp currym json_validator );
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 20 has moniker => 'editor';
189 20 has route =>;
190 0 ▶has includes => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
191 0 ▶has menu_items => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
192 20 has backend =>;
193 20 has schema =>;
194 20 has app => undef, weak => 1;
195  
196 sub _helper_name {
197 0 ▶ my ( $self, $name ) = @_;
Subroutine _helper_name: not called CC=1 SCAR=6.9
198 0 return join '.', 'yancy', $self->moniker, $name;
199 }
200  
201 sub register {
202 19 ▶ my ( $self, $app, $config ) = @_;
Subroutine register: called CC=19 SCAR=52.1
Pod documented
203  
204 19 ▶ $config->{title} //= $app->l( 'Yancy' );
Condition33%1/3
$config->{"title"} //= $app->l("Yancy")
l!l&&r!l&&!r
0190
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
205 19 ▶ $config->{return_label} //= $app->l( 'Back to Application' );
Condition33%1/3
$config->{"return_label"} //= $app->l("Back to Application")
l!l&&r!l&&!r
0190
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
206 19 $self->backend( $config->{backend} );
207 19 $self->schema( my $schema = $config->{schema} );
208 19 $self->app( $app );
209  
210 19 for my $key ( grep exists $config->{ $_ }, qw( moniker ) ) {
211 0 $self->$key( $config->{ $key } );
212 }
213 19 ▶ $config->{default_controller} //= $config->{api_controller} // 'Yancy';
Condition40%2/5
$config->{"default_controller"} //= $config->{"api_controller"} // "Yancy"
l!l&&r!l&&!r
0190
$config->{"api_controller"} // "Yancy"
l!l
019
MC/DC0%0/2
$config->{"default_controller"} //= $config->{"api_controller"} // "Yancy"
$config->{"default_controller"} $config->{"api_controller"}
Truth table0%0/3
A //= B
A= $config->{"default_controller"}B= $config->{"api_controller"}
ABresult
1X1
011
000
214 19 ▶ if ( $config->{api_controller} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"api_controller"}) 0 19 19
215 0 derp 'api_controller configuration is deprecated. Use editor.default_controller instead';
216 }
217  
218 # XXX: Throw an error if there is already a route here
219 19 my $route = $app->yancy->routify( $config->{route}, '/yancy' );
220 19 ▶ $route->to( return_to => $config->{return_to} // '/' );
Condition50%1/2
$config->{"return_to"} // "/"
l!l
019
MC/DC0%0/1
$config->{"return_to"} // "/"
$config->{"return_to"}
Truth table50%1/2
A
A= $config->{"return_to"}
Aresult
11
00
221  
222 # Create authentication for editor. We need to delay fetching this
223 # callback until after startup is complete so that any auth plugin
224 # can be added.
225 my $auth_under = sub {
226 0 ▶ my ( $c ) = @_;
Subroutine __ANON__: not called CC=8 SCAR=42.8
227 state $auth_cb = $c->yancy->can( 'auth' )
228 && $c->yancy->auth->can( 'require_user' )
229 0 ▶ && $c->yancy->auth->require_user( $config->{require_user} || () );
Condition0%0/9
$c->yancy->can('auth') && $c->yancy->auth->can('require_user') && $c->yancy->auth->require_user($config->{'require_user'} || ())
!ll&&!rl&&r
000
$c->yancy->can("auth") and $c->yancy->auth->can("require_user")
!ll&&!rl&&r
000
$config->{'require_user'} || ()
l!l&&r!l&&!r
000
MC/DC0%0/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 table0%0/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 table0%0/3
A || B
A= $config->{'require_user'}B= ()
ABresult
1X1
011
000
230 0 ▶ 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"}) 0 0 0
Condition0%0/6
$auth_cb or exists $config->{"require_user"} or defined $config->{"route"}
l!l&&r!l&&!r
000
$auth_cb or exists $config->{"require_user"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$auth_cb or exists $config->{"require_user"} or defined $config->{"route"}
$auth_cb exists $config->{"require_user"} defined $config->{"route"}
Truth table0%0/4
A or B or C
A= $auth_cbB= exists $config->{"require_user"}C= defined $config->{"route"}
ABCresult
1XX1
01X1
0011
0000
231 0 $app->log->warn(
232 qq{*** Cannot verify that admin editor is behind authentication.\n}
233 . qq{Add a Yancy Auth plugin, add a `route` to the Yancy plugin config,\n}
234 . qq{or set `editor.require_user => undef` to silence this warning\n}
235 );
236 }
237 0 ▶ return $auth_cb ? $auth_cb->( $c ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$auth_cb ? : 0 0 0
238 19 };
239 19 $route = $route->under( $auth_under );
240  
241 # First create the OpenAPI schema and API URL
242 19 my $spec;
243 19 ▶ if ( $config->{openapi} && keys %{ $config->{openapi} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'openapi'} and keys %{$$config{'openapi'};}) { } 0 19 19
Condition33%1/3
$config->{"openapi"} and keys %{$$config{"openapi"};}
!ll&&!rl&&r
1900
MC/DC0%0/2
$config->{"openapi"} and keys %{$$config{"openapi"};}
$config->{"openapi"} keys %{$$config{"openapi"};}
Truth table33%1/3
A and B
A= $config->{"openapi"}B= keys %{$$config{"openapi"};}
ABresult
0X0
100
111
244 0 $spec = $config->{openapi};
245 }
246 else {
247 # Add OpenAPI spec
248 19 $spec = $self->_openapi_spec_from_schema( $config );
249 }
250 19 $self->_openapi_spec_add_mojo( $spec, $config );
251  
252 19 my $openapi = $app->plugin( OpenAPI => {
253 route => $route->any( '/api' )->to( backend => $self->backend )->name( 'yancy.api' ),
254 spec => $spec,
255 default_response_name => '_Error',
256 validator => json_validator(),
257 } );
258 0 $_->to(format => 'json') for (@{$openapi->route->children});
259 $app->helper( 'yancy.openapi' => sub {
260 0 ▶ derp 'yancy.openapi helper is deprecated. Use yancy.editor.openapi instead';
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 return $openapi;
262 0 } );
263 0 ▶ $app->helper( $self->_helper_name( 'openapi' ) => sub { $openapi } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
264  
265 # Do some sanity checks on the config to make sure nothing bad
266 # happens
267 0 for my $schema_name ( keys %$schema ) {
268 0 ▶ if ( my $view = $schema->{ $schema_name }{ 'x-view' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $view = $schema->{$schema_name}{"x-view"}) 0 0 0
269 0 $schema_name = $view->{schema};
270 }
271 0 ▶ next if $schema->{ $schema_name }{ 'x-view' };
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{$schema_name}{"x-view"} 0 0 0
272 0 ▶ if ( my $list_cols = $schema->{ $schema_name }{ 'x-list-columns' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $list_cols = $schema->{$schema_name}{"x-list-columns"}) 0 0 0
273 0 for my $col ( @$list_cols ) {
274 0 ▶ if ( ref $col eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col eq 'HASH') { } 0 0 0
275 # Check template for columns
276 0 my @cols = $col->{template} =~ m'\{([^{]+)\}'g;
277 0 ▶ 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)) 0 0 0
278 0 die sprintf q{Column "%s" in x-list-columns template does not exist in schema "%s"},
279 $col, $schema_name;
280 }
281 }
282 else {
283 0 ▶ if ( !exists $schema->{ $schema_name }{ properties }{ $col } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $schema->{$schema_name}{"properties"}{$col}) 0 0 0
284 0 die sprintf q{Column "%s" in x-list-columns does not exist in schema "%s"},
285 $col, $schema_name;
286 }
287 }
288 }
289 }
290 }
291  
292 # Now create the routes and helpers the editor needs
293 $route->get( '/' )->name( 'yancy.index' )
294 ->to(
295 template => 'yancy/index',
296 controller => $config->{default_controller},
297 action => 'index',
298 api_url => $openapi->route->render,
299 title => $config->{title},
300 return_label => $config->{return_label},
301 0 );
302 $route->post( '/upload' )->name( 'yancy.editor.upload' )
303 ->to( cb => sub {
304 0 ▶ my ( $c ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 my $upload = $c->param( 'upload' );
306 0 my $path = $c->yancy->file->write( $upload );
307 0 $c->res->headers->location( $path );
308 0 $c->render( status => 201, text => $path );
309 0 } );
310  
311 0 $app->helper( $self->_helper_name( 'menu' ), currym( $self, '_helper_menu' ) );
312 0 $app->helper( $self->_helper_name( 'include' ), currym( $self, '_helper_include' ) );
313 0 ▶ $app->helper( $self->_helper_name( 'route' ), sub { $route } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 $app->helper( 'yancy.route', sub {
315 0 ▶ derp 'yancy.route helper is deprecated. Use yancy.editor.route instead';
Subroutine __ANON__: not called CC=1 SCAR=6.9
316 0 return $self->route;
317 0 } );
318 0 $self->route( $route );
319  
320  
321 }
322  
323 sub _helper_include {
324 0 ▶ my ( $self, $c, @includes ) = @_;
Subroutine _helper_include: not called CC=2 SCAR=17.9
325 0 ▶ if ( @includes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@includes) 0 0 0
326 0 push @{ $self->includes }, @includes;
327 }
328 0 return $self->includes;
329 }
330  
331 sub _helper_menu {
332 0 ▶ my ( $self, $c, $category, $title, $config ) = @_;
Subroutine _helper_menu: not called CC=2 SCAR=17.9
333 0 ▶ if ( $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) 0 0 0
334 0 push @{ $self->menu_items->{ $category } }, {
335 %$config,
336 title => $title,
337 };
338 }
339 0 return $self->menu_items;
340 }
341  
342 sub _openapi_find_schema_name {
343 196 ▶ my ( $self, $path, $pathspec ) = @_;
Subroutine _openapi_find_schema_name: called CC=23 SCAR=33.8
344 196 ▶ return $pathspec->{'x-schema'} if $pathspec->{'x-schema'};
Branch: 1 branch
BranchTrueFalseTotal
if $pathspec->{"x-schema"} 0 196 196
345 196 my $schema_name;
346 196 for my $method ( grep !/^(parameters$|x-)/, keys %{ $pathspec } ) {
347 388 my $op_spec = $pathspec->{ $method };
348 388 my $schema;
349 388 ▶ if ( $method eq 'get' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'get') { } 196 192 388
elsif ($method =~ /^(put|post)$/u) { } 128 64 192
350 # d is in case only has "default" response
351 196 my ($response) = grep /^[2d]/, sort keys %{ $op_spec->{responses} };
352 196 my $response_spec = $op_spec->{responses}{$response};
353 196 ▶ next unless $schema = $response_spec->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema = $response_spec->{"schema"} 0 196 196
354 } elsif ( $method =~ /^(put|post)$/ ) {
355 my @body_params = grep 'body' eq ($_->{in} // ''),
356 128 ▶ @{ $op_spec->{parameters} || [] },
Branch: 1 branch
BranchTrueFalseTotal
$op_spec->{"parameters"} or [] 0 128 128
357 128 ▶ @{ $pathspec->{parameters} || [] },
Branch: 1 branch
BranchTrueFalseTotal
$pathspec->{"parameters"} or [] 64 64 128
Condition50%1/2
$_->{"in"} // ""
l!l
1950
MC/DC0%0/1
$_->{"in"} // ""
$_->{"in"}
Truth table50%1/2
A
A= $_->{"in"}
Aresult
11
00
358 ;
359 128 ▶ die "No more than 1 'body' parameter allowed" if @body_params > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @body_params > 1 0 128 128
360 128 ▶ next unless $schema = $body_params[0]->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema = $body_params[0]{"schema"} 0 128 128
361 }
362 next unless my $this_ref =
363 $schema->{'$ref'} ||
364 ( $schema->{items} && $schema->{items}{'$ref'} ) ||
365 388 ▶ ( $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"} 162 226 388
Condition60%9/15
$schema->{'$ref'} || $schema->{'items'} && $schema->{'items'}{'$ref'} || $schema->{'properties'} && $schema->{'properties'}{'items'} && $schema->{'properties'}{'items'}{'$ref'}
l!l&&r!l&&!r
2260162
$schema->{"\$ref"} or $schema->{"items"} and $schema->{"items"}{"\$ref"}
l!l&&r!l&&!r
2260162
$schema->{"items"} and $schema->{"items"}{"\$ref"}
!ll&&!rl&&r
16200
$schema->{"properties"} and $schema->{"properties"}{"items"} and $schema->{"properties"}{"items"}{"\$ref"}
!ll&&!rl&&r
64980
$schema->{"properties"} and $schema->{"properties"}{"items"}
!ll&&!rl&&r
64098
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
366 226 ▶ next unless $this_ref =~ s:^#/definitions/::;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_ref =~ s[^#/definitions/][]u 0 226 226
367 226 ▶ 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 226 226
Condition66%4/6
$this_ref and $schema_name and $this_ref ne $schema_name
!ll&&!rl&&r
162640
$this_ref and $schema_name
!ll&&!rl&&r
016264
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
368 if $this_ref and $schema_name and $this_ref ne $schema_name;
369 226 $schema_name = $this_ref;
370 }
371 196 ▶ if ( !$schema_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_name) 34 162 196
372 34 ($schema_name) = $path =~ m#^/([^/]+)#;
373 34 ▶ die "No schema found in '$path'" if !$schema_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_name 0 34 34
374 }
375 196 $schema_name;
376 }
377  
378 # mutates $spec
379 sub _openapi_spec_add_mojo {
380 19 ▶ my ( $self, $spec, $config ) = @_;
Subroutine _openapi_spec_add_mojo: called CC=10 SCAR=23.8
381 19 for my $path ( keys %{ $spec->{paths} } ) {
382 196 my $pathspec = $spec->{paths}{ $path };
383 196 my $schema = $self->_openapi_find_schema_name( $path, $pathspec );
384 die "Path '$path' had non-existent schema '$schema'"
385 196 ▶ if !$spec->{definitions}{$schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"definitions"}{$schema} 0 196 196
386 196 for my $method ( grep !/^(parameters$|x-)/, keys %{ $pathspec } ) {
387 388 my $op_spec = $pathspec->{ $method };
388 388 my $mojo = $self->_openapi_spec_infer_mojo( $path, $pathspec, $method, $op_spec );
389 # XXX Allow overriding controller on a per-schema basis
390 # This gives more control over how a certain schema's items
391 # are written/read from the database
392 388 $mojo->{controller} = $config->{default_controller};
393 388 $mojo->{schema} = $schema;
394 my @filters = (
395 388 ▶ @{ $pathspec->{ 'x-filter' } || [] },
Branch: 1 branch
BranchTrueFalseTotal
$pathspec->{"x-filter"} or [] 388 0 388
396 388 ▶ @{ $op_spec->{ 'x-filter' } || [] },
Branch: 1 branch
BranchTrueFalseTotal
$op_spec->{"x-filter"} or [] 388 0 388
397 );
398 388 ▶ $mojo->{filters} = \@filters if @filters;
Branch: 1 branch
BranchTrueFalseTotal
if @filters 0 388 388
399 my @filters_out = (
400 388 ▶ @{ $pathspec->{ 'x-filter-output' } || [] },
Branch: 1 branch
BranchTrueFalseTotal
$pathspec->{"x-filter-output"} or [] 388 0 388
401 388 ▶ @{ $op_spec->{ 'x-filter-output' } || [] },
Branch: 1 branch
BranchTrueFalseTotal
$op_spec->{"x-filter-output"} or [] 388 0 388
402 );
403 388 ▶ $mojo->{filters_out} = \@filters_out if @filters_out;
Branch: 1 branch
BranchTrueFalseTotal
if @filters_out 0 388 388
404 388 $op_spec->{ 'x-mojo-to' } = $mojo;
405 }
406 }
407 }
408  
409 # for a given OpenAPI operation, figures out right values for 'x-mojo-to'
410 # to hook it up to the correct CRUD operation
411 sub _openapi_spec_infer_mojo {
412 388 ▶ my ( $self, $path, $pathspec, $method, $op_spec ) = @_;
Subroutine _openapi_spec_infer_mojo: called CC=12 SCAR=25.1
413 my @path_params = grep 'path' eq ($_->{in} // ''),
414 388 ▶ @{ $pathspec->{parameters} || [] },
Branch: 1 branch
BranchTrueFalseTotal
$pathspec->{"parameters"} or [] 162 226 388
415 388 ▶ @{ $op_spec->{parameters} || [] },
Branch: 1 branch
BranchTrueFalseTotal
$op_spec->{"parameters"} or [] 162 226 388
Condition100%2/2
$_->{"in"} // ""
l!l
1038392
MC/DC100%1/1
$_->{"in"} // ""
$_->{"in"}
Truth table100%2/2
A
A= $_->{"in"}
Aresult
11
00
416 ;
417 my ($id_field) = grep defined,
418 (map $_->{'x-id-field'}, $op_spec, $pathspec),
419 388 ▶ (@path_params && $path_params[-1]{name});
Condition66%2/3
@path_params && $path_params[-1]{'name'}
!ll&&!rl&&r
1620226
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
420 388 ▶ if ( $method eq 'get' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($method eq 'get') { } 196 192 388
elsif ($method eq 'post') { } 64 128 192
elsif ($method eq 'put') { } 64 64 128
elsif ($method eq 'delete') { } 64 0 64
421 # heuristic: is per-item if have a param in path
422 196 ▶ if ( $id_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_field) { } 98 98 196
423 # per-item - GET = "read"
424 return {
425 98 action => 'get',
426 format => 'json',
427 };
428 }
429 else {
430 # per-schema - GET = "list"
431 return {
432 98 action => 'list',
433 format => 'json',
434 };
435 }
436 } elsif ( $method eq 'post' ) {
437 return {
438 64 action => 'set',
439 format => 'json',
440 };
441 } elsif ( $method eq 'put' ) {
442 64 ▶ die "'$method' $path needs id_field" if !$id_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_field 0 64 64
443 return {
444 64 action => 'set',
445 format => 'json',
446 };
447 } elsif ( $method eq 'delete' ) {
448 64 ▶ die "'$method' $path needs id_field" if !$id_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $id_field 0 64 64
449 return {
450 64 action => 'delete',
451 format => 'json',
452 };
453 }
454 else {
455 0 die "Unknown method '$method'";
456 }
457 }
458  
459 sub _openapi_spec_from_schema {
460 19 ▶ my ( $self, $config ) = @_;
Subroutine _openapi_spec_from_schema: called CC=19 SCAR=29.8
461 19 my ( %definitions, %paths );
462 19 my %parameters = (
463 '$limit' => {
464 name => '$limit',
465 type => 'integer',
466 in => 'query',
467 description => $self->app->l( 'OpenAPI $limit description' ),
468 },
469 '$offset' => {
470 name => '$offset',
471 type => 'integer',
472 in => 'query',
473 description => $self->app->l( 'OpenAPI $offset description' ),
474 },
475 '$order_by' => {
476 name => '$order_by',
477 type => 'string',
478 in => 'query',
479 pattern => '^(?:asc|desc):[^:,]+$',
480 description => $self->app->l( 'OpenAPI $order_by description' ),
481 },
482 '$match' => {
483 name => '$match',
484 type => 'string',
485 enum => [qw( any all )],
486 default => 'all',
487 in => 'query',
488 description => $self->app->l( 'OpenAPI $match description' ),
489 },
490 );
491 19 for my $schema_name ( keys %{ $config->{schema} } ) {
492 # Set some defaults so users don't have to type as much
493 117 my $schema = $config->{schema}{ $schema_name };
494 117 ▶ next if $schema->{ 'x-ignore' };
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"x-ignore"} 19 98 117
495 98 ▶ my $id_field = $schema->{ 'x-id-field' } // 'id';
Condition100%2/2
$schema->{"x-id-field"} // "id"
l!l
6038
MC/DC100%1/1
$schema->{"x-id-field"} // "id"
$schema->{"x-id-field"}
Truth table100%2/2
A
A= $schema->{"x-id-field"}
Aresult
11
00
496 98 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 3 95 98
497 98 ▶ my $real_schema_name = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
34640
$schema->{'x-view'} || {}
l!l
3464
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
498 my $props = $schema->{properties}
499 98 ▶ || $config->{schema}{ $real_schema_name }{properties};
Condition66%2/3
$schema->{'properties'} || $config->{'schema'}{$real_schema_name}{'properties'}
l!l&&r!l&&!r
81170
MC/DC0%0/2
$schema->{'properties'} || $config->{'schema'}{$real_schema_name}{'properties'}
$schema->{'properties'} $config->{'schema'}{$real_schema_name}{'properties'}
Truth table66%2/3
A || B
A= $schema->{'properties'}B= $config->{'schema'}{$real_schema_name}{'properties'}
ABresult
1X1
011
000
500 98 my %props = %$props;
501  
502 98 $definitions{ $schema_name } = $schema;
503  
504 98 for my $prop ( keys %props ) {
505 675 ▶ $props{ $prop }{ type } ||= 'string';
Condition50%1/2
$props{$prop}{"type"} ||= "string"
l!l
6750
MC/DC0%0/1
$props{$prop}{"type"} ||= "string"
$props{$prop}{"type"}
Truth table50%1/2
A
A= $props{$prop}{"type"}
Aresult
11
00
506 }
507  
508 $paths{ '/' . $schema_name } = {
509 get => {
510 description => $self->app->l( 'OpenAPI list description' ),
511 parameters => [
512 { '$ref' => '#/parameters/%24limit' },
513 { '$ref' => '#/parameters/%24offset' },
514 { '$ref' => '#/parameters/%24order_by' },
515 { '$ref' => '#/parameters/%24match' },
516 map {
517 675 my $name = $_;
518 675 ▶ my $type = ref $props{ $_ }{type} eq 'ARRAY' ? $props{ $_ }{type}[0] : $props{ $_ }{type};
Branch: 1 branch
BranchTrueFalseTotal
ref $props{$_}{'type'} eq 'ARRAY' ? : 192 483 675
519 675 ▶ my $description = $self->app->l(
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'number' || $type eq 'integer' ? : 156 519 675
$type eq 'boolean' ? : 38 481 519
$type eq 'array' ? : 0 481 481
Condition100%3/3
$type eq "number" or $type eq "integer"
l!l&&r!l&&!r
2154519
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
520 $type eq 'number' || $type eq 'integer' ? 'OpenAPI filter number description'
521 : $type eq 'boolean' ? 'OpenAPI filter boolean description'
522 : $type eq 'array' ? 'OpenAPI filter array description'
523 : 'OpenAPI filter string description'
524 );
525 {
526 675 ▶ name => $name,
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'x-view'} ? : 34 64 98
527 in => 'query',
528 type => $type,
529 description => $self->app->l( 'OpenAPI filter description', $name ) . $description,
530 } } grep !exists( $props{ $_ }{'$ref'} ), sort keys %props,
531 ],
532 responses => {
533 200 => {
534 description => $self->app->l( 'OpenAPI list response' ),
535 schema => {
536 type => 'object',
537 required => [qw( items total )],
538 properties => {
539 total => {
540 type => 'integer',
541 description => $self->app->l( 'OpenAPI list total description' ),
542 },
543 items => {
544 type => 'array',
545 description => $self->app->l( 'OpenAPI list items description' ),
546 items => { '$ref' => "#/definitions/" . url_escape $schema_name },
547 },
548 offset => {
549 type => 'integer',
550 description => $self->app->l( 'OpenAPI list offset description' ),
551 },
552 },
553 },
554 },
555 default => {
556 description => $self->app->l( 'Unexpected error' ),
557 schema => { '$ref' => '#/definitions/_Error' },
558 },
559 },
560 },
561 $schema->{'x-view'} ? () : (post => {
562 parameters => [
563 {
564 name => "newItem",
565 in => "body",
566 required => true,
567 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
568 },
569 ],
570 responses => {
571 201 => {
572 description => $self->app->l( 'OpenAPI create response' ),
573 schema => {
574 98 '$ref' => '#' . join '/', map { url_escape $_ } 'definitions',
575 $schema_name, 'properties', @id_fields,
576 },
577 },
578 default => {
579 description => $self->app->l( "Unexpected error" ),
580 schema => { '$ref' => "#/definitions/_Error" },
581 },
582 },
583 }),
584 };
585  
586 $paths{ sprintf '/%s/{%s}', $schema_name, $id_field } = {
587 parameters => [
588 map +{
589 name => $_,
590 in => 'path',
591 required => true,
592 type => 'string',
593 'x-mojo-placeholder' => '*',
594 }, @id_fields
595 ],
596  
597 get => {
598 description => $self->app->l( 'OpenAPI get description' ),
599 responses => {
600 200 => {
601 description => $self->app->l( 'OpenAPI get response' ),
602 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
603 },
604 default => {
605 description => $self->app->l( "Unexpected error" ),
606 schema => { '$ref' => '#/definitions/_Error' },
607 }
608 }
609 },
610  
611 98 ▶ $schema->{'x-view'} ? () : (put => {
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'x-view'} ? : 34 64 98
612 description => $self->app->l( 'OpenAPI update description' ),
613 parameters => [
614 {
615 name => "newItem",
616 in => "body",
617 required => true,
618 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
619 }
620 ],
621 responses => {
622 200 => {
623 description => $self->app->l( 'OpenAPI update response' ),
624 schema => { '$ref' => "#/definitions/" . url_escape $schema_name },
625 },
626 default => {
627 description => $self->app->l( "Unexpected error" ),
628 schema => { '$ref' => "#/definitions/_Error" },
629 }
630 }
631 },
632  
633 delete => {
634 description => $self->app->l( 'OpenAPI delete description' ),
635 responses => {
636 204 => {
637 description => $self->app->l( 'OpenAPI delete response' ),
638 },
639 default => {
640 description => $self->app->l( "Unexpected error" ),
641 schema => { '$ref' => '#/definitions/_Error' },
642 },
643 },
644 }),
645 };
646 }
647  
648 return {
649 info => $config->{info} || { title => $config->{title}, version => "1" },
650 swagger => '2.0',
651 19 ▶ host => $config->{host} // hostname(),
Condition40%2/5
$config->{'info'} || {'title', $config->{'title'}, 'version', '1'}
l!l
019
$config->{"host"} // hostname()
l!l&&r!l&&!r
0190
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
652 basePath => '/api',
653 schemes => [qw( http )],
654 consumes => [qw( application/json )],
655 produces => [qw( application/json )],
656 definitions => {
657 _Error => {
658 title => $self->app->l( 'OpenAPI error object' ),
659 type => 'object',
660 properties => {
661 errors => {
662 type => "array",
663 items => {
664 required => [qw( message )],
665 properties => {
666 message => {
667 type => "string",
668 description => $self->app->l( 'OpenAPI error message' ),
669 },
670 path => {
671 type => "string",
672 description => $self->app->l( 'OpenAPI error path' ),
673 }
674 }
675 }
676 }
677 }
678 },
679 %definitions,
680 },
681 paths => \%paths,
682 parameters => \%parameters,
683 };
684 }
685  
686  
687 20 1;
688  
689 __END__
« Yancy/Plugin/Auth/Password.pm ↑ Summary Yancy/Plugin/Auth/Token.pm »