File Coverage

blib/lib/Swagger2.pm
Criterion Covered Total %
statement 81 81 100.0
branch 25 28 89.2
condition 25 27 92.5
subroutine 20 20 100.0
pod 11 11 100.0
total 162 167 97.0


line stmt bran cond sub pod time code
1             package Swagger2;
2 38     38   998208 use Mojo::Base -base;
  38         53  
  38         226  
3 38     38   6005 use Mojo::Asset::File;
  38         89863  
  38         381  
4 38     38   1976 use Mojo::JSON;
  38         34788  
  38         1191  
5 38     38   1546 use Mojo::JSON::Pointer;
  38         1894  
  38         346  
6 38     38   2116 use Mojo::URL;
  38         17697  
  38         240  
7 38     38   745 use File::Basename ();
  38         56  
  38         564  
8 38     38   126 use File::Spec;
  38         51  
  38         885  
9 38     38   16963 use JSON::Validator::OpenAPI;
  38         446228  
  38         44280  
10              
11             our $VERSION = '0.89';
12              
13             # Should be considered internal
14             our $JS_CLIENT
15             = File::Spec->catfile(File::Basename::dirname(__FILE__), 'Swagger2', 'swagger2-client.js');
16              
17             has api_spec => sub {
18             my $self = shift;
19             return $self->_validator->_load_schema($self->url) if '' . $self->url;
20             return Mojo::JSON::Pointer->new({});
21             };
22              
23             has base_url => sub {
24             my $self = shift;
25             my $url = Mojo::URL->new;
26             my ($schemes, $v);
27              
28             $self->load if !$self->{api_spec} and '' . $self->url;
29             $schemes = $self->api_spec->get('/schemes') || [];
30             $url->host($self->api_spec->get('/host') || 'example.com');
31             $url->path($self->api_spec->get('/basePath') || '/');
32             $url->scheme($schemes->[0] || 'http');
33              
34             return $url;
35             };
36              
37             has _specification => sub { shift->_validator->schema('http://swagger.io/v2/schema.json')->schema };
38              
39             has _validator => sub { JSON::Validator::OpenAPI->new };
40              
41 1     1 1 349 sub ua { shift->_validator->ua(@_) }
42 162     162 1 98701 sub url { shift->{url} }
43              
44             sub expand {
45 93     93 1 389 my $self = shift;
46 93         549 my $class = Scalar::Util::blessed($self);
47 93         286 my $schema = $self->_validator->schema($self->api_spec->data)->schema;
48 93         179822 $class->new(%$self)->api_spec($schema);
49             }
50              
51             sub find_operations {
52 7     7 1 3801 my ($self, $needle) = @_;
53 7         17 my $paths = $self->api_spec->get('/paths');
54 7         142 my $operations = [];
55              
56 7   100     18 $needle ||= {};
57 7 100       16 $needle = {operationId => $needle} unless ref $needle;
58              
59 7         15 for my $path (keys %$paths) {
60 21 50       31 next if $path =~ /^x-/;
61 21 100 100     46 next if $needle->{path} and $needle->{path} ne $path;
62 17         12 for my $method (keys %{$paths->{$path}}) {
  17         32  
63 31         29 my $object = $paths->{$path}{$method};
64 31 100       51 next if $method =~ /^x-/;
65 24 100 100     36 next if $needle->{tag} and !grep { $needle->{tag} eq $_ } @{$object->{tags} || []};
  6 100       20  
  8         24  
66 17 100 100     29 next if $needle->{method} and $needle->{method} ne $method;
67 16 100 100     40 next if $needle->{operationId} and $needle->{operationId} ne $object->{operationId};
68 10         14 push @$operations, $object;
69             }
70             }
71              
72 7         24 return $operations;
73             }
74              
75 1     1 1 13 sub javascript_client { Mojo::Asset::File->new(path => $JS_CLIENT) }
76              
77             sub load {
78 49     49 1 84 my $self = shift;
79 49         77 delete $self->{base_url};
80 49 100       261 $self->{url} = Mojo::URL->new(shift) if @_;
81 49         3819 $self->{api_spec} = $self->api_spec;
82 49         79110 $self;
83             }
84              
85             sub new {
86 156     156 1 1000194 my $class = shift;
87 156 100       580 my $url = @_ % 2 ? shift : '';
88 156         625 my $self = $class->SUPER::new(@_);
89              
90 156         887 $url =~ s!^file://!!;
91 156   100     860 $self->{url} ||= $url;
92 156 100       1219 $self->{url} = Mojo::URL->new($self->{url}) unless ref $self->{url};
93 156         19622 $self;
94             }
95              
96             sub parse {
97 4     4 1 552 my ($self, $doc, $namespace) = @_;
98 4         10 delete $self->{base_url};
99 4   100     21 $namespace ||= 'http://127.0.0.1/#';
100 4         13 $self->{url} = Mojo::URL->new($namespace);
101 4         357 $self->{api_spec} = Mojo::JSON::Pointer->new($self->_validator->_load_schema_from_text($doc));
102 4         8385 $self;
103             }
104              
105             sub pod {
106 12     12 1 8108 my $self = shift;
107 12         49 my $resolved = $self->_validator->schema($self->api_spec->data)->schema;
108 12         48244 require Swagger2::POD;
109 12         58 Swagger2::POD->new(base_url => $self->base_url, api_spec => $resolved);
110             }
111              
112             sub to_string {
113 2     2 1 1711 my $self = shift;
114 2   100     10 my $format = shift || 'json';
115              
116 2 50       5 return DumpYAML($self->api_spec->data) if $format eq 'yaml';
117 2         6 return Mojo::JSON::encode_json($self->api_spec->data);
118             }
119              
120             sub validate {
121 47     47 1 46889 my $self = shift;
122 47         195 $self->_validator->validate($self->expand->api_spec->data, $self->_specification->data);
123             }
124              
125             sub _is_true {
126 120 50 66 120   332 return $_[0] if ref $_[0] and !Scalar::Util::blessed($_[0]);
127 120 100 66     479 return 0 if !$_[0] or $_[0] =~ /^(n|false|off)/i;
128 110         598 return 1;
129             }
130              
131             1;
132              
133             =encoding utf8
134              
135             =head1 NAME
136              
137             Swagger2 - Swagger RESTful API Documentation
138              
139             =head1 VERSION
140              
141             0.89
142              
143             =head1 DEPRECATION WARNING
144              
145             The L distribution is no longer actively maintained. Only severe bug
146             fixes and pull requests will move this code forward. The reason behind this is
147             that the code is too complex and hard to maintain.
148              
149             So what should you use instead?
150              
151             =over 2
152              
153             =item * L
154              
155             L is either not very useful or replaced by L.
156              
157             =item * L
158              
159             No alternatives. The issue with this module is that it does not understand if
160             you have parameters with the same name. There might be a L at
161             some point, but it is currently no plans to write it.
162              
163             =item * L
164              
165             No alternatives.
166              
167             =item * L
168              
169             L is not very good and also very hard to maintain.
170             L has a HTML renderer which makes documentation
171             that is much easier to read and always in sync with the application.
172              
173             When that is said: The renderer in L need
174             refinement.
175              
176             =item * L
177              
178             L has the validator built in. For other purposes,
179             use L or L instead.
180              
181             =item * L
182              
183             No alternatives.
184              
185             =item * L
186              
187             Use L instead. L
188             plays much nicer together with the L framework.
189              
190             =back
191              
192             =head1 DESCRIPTION
193              
194             L is a module for generating, parsing and transforming
195             L API specification. It has support for reading
196             swagger specification in JSON notation and as well YAML format.
197              
198             Please read L
199             for an introduction to Swagger and reasons for why you would to use it.
200              
201             =head2 Mojolicious server side code generator
202              
203             This distribution comes with a L plugin,
204             L, which can set up routes and perform input
205             and output validation.
206              
207             =head2 Mojolicious client side code generator
208              
209             Swagger2 also comes with a L generator, which converts the client
210             spec to perl code in memory.
211              
212             =head1 RECOMMENDED MODULES
213              
214             =over 4
215              
216             =item * YAML parser
217              
218             A L parser is required if you want to read/write spec written in
219             the YAML format. Supported modules are L, L, L
220             and L.
221              
222             =back
223              
224             =head1 SYNOPSIS
225              
226             use Swagger2;
227             my $swagger = Swagger2->new("/path/to/api-spec.yaml");
228              
229             # Access the raw specification values
230             print $swagger->api_spec->get("/swagger");
231              
232             # Returns the specification as a POD document
233             print $swagger->pod->to_string;
234              
235             =head1 ATTRIBUTES
236              
237             =head2 api_spec
238              
239             $pointer = $self->api_spec;
240             $self = $self->api_spec(Mojo::JSON::Pointer->new({}));
241              
242             Holds a L object containing your API specification.
243              
244             =head2 base_url
245              
246             $mojo_url = $self->base_url;
247              
248             L object that holds the location to the API endpoint.
249             Note: This might also just be a dummy URL to L.
250              
251             =head2 ua
252              
253             $ua = $self->ua;
254             $self = $self->ua(Mojo::UserAgent->new);
255              
256             A L used to fetch remote documentation.
257              
258             =head2 url
259              
260             $mojo_url = $self->url;
261              
262             L object that holds the location to the documentation file.
263             This can be both a location on disk or an URL to a server. A remote
264             resource will be fetched using L.
265              
266             =head1 METHODS
267              
268             =head2 expand
269              
270             $swagger = $self->expand;
271              
272             This method returns a new C object, where all the
273             L
274             are resolved.
275              
276             =head2 find_operations
277              
278             $operations = $self->find_operations(\%q);
279              
280             Used to find a list of L
281             from the specification. C<%q> can be:
282              
283             $all = $self->find_operations;
284             $operations = $self->find_operations($operationId);
285             $operations = $self->find_operations({operationId => "listPets"});
286             $operations = $self->find_operations({method => "post", path => "/pets"});
287             $operations = $self->find_operations({tag => "pets"});
288              
289             =head2 javascript_client
290              
291             $file = $self->javascript_client;
292              
293             Returns a L object which points to a file containing a
294             custom JavaScript file which can communicate with
295             L.
296              
297             See L
298             for source code.
299              
300             C is currently EXPERIMENTAL!
301              
302             =head2 load
303              
304             $self = $self->load;
305             $self = $self->load($url);
306              
307             Used to load the content from C<$url> or L. This method will try to
308             guess the content type (JSON or YAML) by looking at the content of the C<$url>.
309              
310             =head2 new
311              
312             $self = Swagger2->new($url);
313             $self = Swagger2->new(%attributes);
314             $self = Swagger2->new(\%attributes);
315              
316             Object constructor.
317              
318             =head2 parse
319              
320             $self = $self->parse($text);
321              
322             Used to parse C<$text> instead of L data from L.
323              
324             The type of input text can be either JSON or YAML. It will default to YAML,
325             but parse the text as JSON if it starts with "{".
326              
327             =head2 pod
328              
329             $pod_object = $self->pod;
330              
331             Returns a L object.
332              
333             =head2 to_string
334              
335             $json = $self->to_string;
336             $json = $self->to_string("json");
337             $yaml = $self->to_string("yaml");
338              
339             This method can transform this object into Swagger spec.
340              
341             =head2 validate
342              
343             @errors = $self->validate;
344              
345             Will validate L against
346             L,
347             and return a list with all the errors found. See also L.
348              
349             =head1 COPYRIGHT AND LICENSE
350              
351             Copyright (C) 2014-2015, Jan Henning Thorsen
352              
353             This program is free software, you can redistribute it and/or modify it under
354             the terms of the Artistic License version 2.0.
355              
356             =head1 AUTHOR
357              
358             Jan Henning Thorsen - C
359              
360             =cut