File Coverage

blib/lib/Swagger/Schema/V3.pm
Criterion Covered Total %
statement 144 144 100.0
branch n/a
condition n/a
subroutine 48 48 100.0
pod n/a
total 192 192 100.0


line stmt bran cond sub pod time code
1 1     1   101567 use Moose::Util::TypeConstraints;
  1         289402  
  1         8  
2              
3             subtype 'Swagger::Schema::V3::RefOrSchema', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::Schema';
4             subtype 'Swagger::Schema::V3::RefOrSchemaOrBool', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::Schema|Bool';
5             subtype 'Swagger::Schema::V3::RefOrResponse', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::Response';
6             subtype 'Swagger::Schema::V3::RefOrPath', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::Path';
7             subtype 'Swagger::Schema::V3::RefOrHeader', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::Header';
8             subtype 'Swagger::Schema::V3::RefOrLink', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::Link';
9             subtype 'Swagger::Schema::V3::RefOrRequestBody', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::RequestBody';
10             subtype 'Swagger::Schema::V3::RefOrParameter', as 'Swagger::Schema::V3::Ref|Swagger::Schema::V3::Parameter';
11              
12              
13              
14             coerce 'Swagger::Schema::V3::RefOrSchema',
15             from 'HashRef',
16             via {
17             if ($_->{ '$ref' }) {
18             return Swagger::Schema::V3::Ref->new($_);
19             } else {
20             return Swagger::Schema::V3::Schema->new($_);
21             }
22             };
23              
24             coerce 'Swagger::Schema::V3::RefOrSchemaOrBool',
25             from 'HashRef',
26             via {
27             if (ref($_) && $_->{ '$ref' }) {
28             return Swagger::Schema::V3::Ref->new($_);
29             } elsif (ref($_)) {
30             return Swagger::Schema::V3::Schema->new($_);
31             } else {
32             return $_;
33             }
34             };
35              
36              
37             coerce 'Swagger::Schema::V3::RefOrResponse',
38             from 'HashRef',
39             via {
40             if ($_->{ '$ref' }) {
41             return Swagger::Schema::V3::Ref->new($_);
42             } else {
43             return Swagger::Schema::V3::Response->new($_);
44             }
45             };
46              
47             coerce 'Swagger::Schema::V3::RefOrPath',
48             from 'HashRef',
49             via {
50             if ($_->{ '$ref' }) {
51             return Swagger::Schema::V3::Ref->new($_);
52             } else {
53             return Swagger::Schema::V3::Path->new($_);
54             }
55             };
56              
57             coerce 'Swagger::Schema::V3::RefOrHeader',
58             from 'HashRef',
59             via {
60             if ($_->{ '$ref' }) {
61             return Swagger::Schema::V3::Ref->new($_);
62             } else {
63             return Swagger::Schema::V3::Header->new($_);
64             }
65             };
66              
67             coerce 'Swagger::Schema::V3::RefOrLink',
68             from 'HashRef',
69             via {
70             if ($_->{ '$ref' }) {
71             return Swagger::Schema::V3::Ref->new($_);
72             } else {
73             return Swagger::Schema::V3::Link->new($_);
74             }
75             };
76              
77             coerce 'Swagger::Schema::V3::RefOrRequestBody',
78             from 'HashRef',
79             via {
80             if ($_->{ '$ref' }) {
81             return Swagger::Schema::V3::Ref->new($_);
82             } else {
83             return Swagger::Schema::V3::RequestBody->new($_);
84             }
85             };
86              
87             coerce 'Swagger::Schema::V3::RefOrParameter',
88             from 'HashRef',
89             via {
90             if ($_->{ '$ref' }) {
91             return Swagger::Schema::V3::Ref->new($_);
92             } else {
93             return Swagger::Schema::V3::Parameter->new($_);
94             }
95             };
96              
97             package Swagger::Schema::V3 {
98 1     1   3064 use MooseX::DataModel;
  1         186743  
  1         4  
99 1     1   9273 use Moose::Util::TypeConstraints;
  1         4  
  1         4  
100 1     1   2038 use namespace::autoclean;
  1         7850  
  1         4  
101              
102             key openapi => (isa => 'Str', required => 1);
103             key info => (isa => 'Swagger::Schema::V3::Info', required => 1);
104             array servers => (isa => 'Swagger::Schema::V3::Server');
105             object paths => (isa => 'Swagger::Schema::V3::Path', required => 1);
106             key components => (isa => 'Swagger::Schema::V3::Components');
107             array security => (isa => 'HashRef');
108             array tags => (isa => 'Swagger::Schema::V3::Tag');
109             key externalDocs => (isa => 'Swagger::Schema::V3::ExternalDocumentation');
110             }
111              
112             package Swagger::Schema::V3::Tag {
113 1     1   2783 use MooseX::DataModel;
  1         3  
  1         6  
114 1     1   8396 use namespace::autoclean;
  1         3  
  1         4  
115              
116             key name => (isa => 'Str', required => 1);
117             key description => (isa => 'Str');
118             key externalDocs => (isa => 'Swagger::Schema::V3::ExternalDocumentation');
119             }
120              
121             package Swagger::Schema::V3::Components {
122 1     1   2688 use MooseX::DataModel;
  1         2  
  1         4  
123 1     1   8357 use namespace::autoclean;
  1         2  
  1         5  
124              
125             object schemas => (isa => 'Swagger::Schema::V3::RefOrSchema');
126             object responses => (isa => 'Swagger::Schema::V3::RefOrResponse');
127             object parameters => (isa => 'Swagger::Schema::V3::RefOrParameter');
128             #object examples => (isa => 'Swagger::Schema::V3::RefOr');
129             object requestBodies => (isa => 'Swagger::Schema::V3::RefOrRequestBody');
130             object headers => (isa => 'Swagger::Schema::V3::RefOrHeader');
131             object securitySchemes => (isa => 'Swagger::Schema::V3::SecurityScheme');
132             object links => (isa => 'Swagger::Schema::V3::RefOrLink');
133             #object callbacks => (isa => 'Swagger::Schema::V3::RefOr');
134             }
135              
136             package Swagger::Schema::V3::Server {
137 1     1   2748 use MooseX::DataModel;
  1         8  
  1         4  
138 1     1   8231 use namespace::autoclean;
  1         2  
  1         5  
139              
140             key url => (isa => 'Str', required => 1);
141             key description => (isa => 'Str');
142             object variables => (isa => 'Swagger::Schema::V3::ServerVariable');
143             }
144              
145             package Swagger::Schema::V3::ServerVariable {
146 1     1   2672 use MooseX::DataModel;
  1         2  
  1         3  
147 1     1   8531 use namespace::autoclean;
  1         3  
  1         7  
148              
149             array enum => (isa => 'Str');
150             key default => (isa => 'Str', required => 1);
151             key description => (isa => 'Str');
152             }
153              
154             package Swagger::Schema::V3::Path {
155 1     1   2640 use MooseX::DataModel;
  1         3  
  1         4  
156 1     1   8480 use namespace::autoclean;
  1         4  
  1         5  
157              
158             key summary => (isa => 'Str');
159             key description => (isa => 'Str');
160             key get => (isa => 'Swagger::Schema::V3::Operation');
161             key put => (isa => 'Swagger::Schema::V3::Operation');
162             key post => (isa => 'Swagger::Schema::V3::Operation');
163             key delete => (isa => 'Swagger::Schema::V3::Operation');
164             key options => (isa => 'Swagger::Schema::V3::Operation');
165             key head => (isa => 'Swagger::Schema::V3::Operation');
166             key patch => (isa => 'Swagger::Schema::V3::Operation');
167             key trace => (isa => 'Swagger::Schema::V3::Operation');
168              
169             array servers => (isa => 'Swagger::Schema::V3::Server');
170              
171             array parameters => (isa => 'Swagger::Schema::V3::Parameter');
172             }
173              
174             enum 'Swagger::Schema::V3::ParameterTypes',
175             [qw/string number integer boolean array file object/];
176              
177             enum 'Swagger::Schema::V3::SecurityParameterTypes',
178             [qw/http apiKey openIdConnect oauth2/];
179              
180             package Swagger::Schema::V3::SecurityScheme {
181 1     1   2855 use MooseX::DataModel;
  1         1331  
  1         10  
182 1     1   8516 use namespace::autoclean;
  1         2  
  1         34  
183              
184             key type => (isa => 'Swagger::Schema::V3::SecurityParameterTypes', required => 1);
185             key scheme => (isa => 'Str');
186             }
187              
188              
189             package Swagger::Schema::V3::Schema {
190 1     1   2638 use MooseX::DataModel;
  1         2  
  1         5  
191 1     1   8351 use namespace::autoclean;
  1         2  
  1         4  
192              
193             key title => (isa => 'Str');
194             key multipleOf => (isa => 'Num');
195             key maximum => (isa => 'Int');
196             key exclusiveMaximum => (isa => 'Bool');
197             key minimum => (isa => 'Int');
198             key exclusiveMinumum => (isa => 'Bool');
199             key maxLength => (isa => 'Int');
200             key minLength => (isa => 'Int');
201             key pattern => (isa => 'Str');
202             key maxItems => (isa => 'Int');
203             key minItems => (isa => 'Int');
204             key uniqueItems => (isa => 'Bool');
205             array required => (isa => 'Str');
206             array enum => (isa => 'Any');
207              
208             key type => (isa => 'Swagger::Schema::V3::ParameterTypes');
209             array allOf => (isa => 'Swagger::Schema::V3::RefOrSchema');
210             array oneOf => (isa => 'Swagger::Schema::V3::RefOrSchema');
211             array anyOf => (isa => 'Swagger::Schema::V3::RefOrSchema');
212             key not => (isa => 'Swagger::Schema::V3::RefOrSchema');
213             key items => (isa => 'Swagger::Schema::V3::RefOrSchema');
214             object properties => (isa => 'Swagger::Schema::V3::RefOrSchema');
215             key additionalProperties => (isa => 'Swagger::Schema::V3::RefOrSchemaOrBool');
216             key description => (isa => 'Str');
217             key format => (isa => 'Str');
218              
219             key nullable => (isa => 'Bool');
220             key discriminator => (isa => 'Swagger::Schema::V3::Discriminator');
221             key readOnly => (isa => 'Bool');
222             key writeOnly => (isa => 'Bool');
223             #key xml => (isa => 'Swagger::Schema::V3::XML');
224             key externalDocs => (isa => 'Swagger::Schema::V3::ExternalDocumentation');
225             key example => (isa => 'Any');
226             key deprecated => (isa => 'Bool');
227             #x-^ patterned fields
228             }
229              
230             package Swagger::Schema::V3::Discriminator {
231 1     1   3304 use MooseX::DataModel;
  1         2  
  1         5  
232 1     1   8487 use namespace::autoclean;
  1         3  
  1         4  
233              
234             key propertyName => (isa => 'Str', required => 1);
235             object mapping => (isa => 'Str');
236             }
237              
238             package Swagger::Schema::V3::ParameterBase {
239 1     1   2744 use MooseX::DataModel;
  1         2  
  1         3  
240 1     1   8424 use namespace::autoclean;
  1         2  
  1         13  
241              
242             key description => (isa => 'Str');
243             key required => (isa => 'Bool');
244             key deprecated => (isa => 'Bool');
245             key allowEmptyValue => (isa => 'Bool');
246              
247             key style => (isa => 'Str');
248             key explode => (isa => 'Bool');
249             key allowReserved => (isa => 'Bool');
250              
251             key schema => (isa => 'Swagger::Schema::V3::RefOrSchema');
252              
253             key example => (isa => 'Any');
254             # examples can be Example Object | Reference Object
255             object examples => (isa => 'Any');
256              
257             object content => (isa => 'Swagger::Schema::V3::MediaType');
258             }
259              
260             package Swagger::Schema::V3::Header {
261 1     1   2783 use MooseX::DataModel;
  1         2  
  1         6  
262 1     1   8362 use namespace::autoclean;
  1         3  
  1         4  
263             extends 'Swagger::Schema::V3::ParameterBase';
264             }
265              
266             package Swagger::Schema::V3::Parameter {
267 1     1   2656 use MooseX::DataModel;
  1         3  
  1         3  
268 1     1   8455 use namespace::autoclean;
  1         2  
  1         5  
269             extends 'Swagger::Schema::V3::ParameterBase';
270              
271             key name => (isa => 'Str', required => 1);
272             key in => (isa => 'Str', required => 1);
273             }
274              
275             package Swagger::Schema::V3::MediaType {
276 1     1   2615 use MooseX::DataModel;
  1         2  
  1         4  
277 1     1   8381 use namespace::autoclean;
  1         3  
  1         4  
278              
279             key schema => (isa => 'Swagger::Schema::V3::RefOrSchema');
280             key example => (isa => 'Any');
281             # examples can be Example Object | Reference Object
282             object examples => (isa => 'Any');
283             object encoding => (isa => 'Swagger::Schema::V3::Encoding');
284             }
285              
286             package Swagger::Schema::V3::Encoding {
287 1     1   2768 use MooseX::DataModel;
  1         2  
  1         4  
288 1     1   8394 use namespace::autoclean;
  1         2  
  1         5  
289              
290             key contentType => (isa => 'Str');
291             object headers => (isa => 'Swagger::Schema::V3::Header');
292             key style => (isa => 'Str');
293             key explode => (isa => 'Bool');
294             key allowReserved => (isa => 'Bool');
295             }
296              
297             package Swagger::Schema::V3::Ref {
298 1     1   2714 use MooseX::DataModel;
  1         2  
  1         4  
299 1     1   8312 use namespace::autoclean;
  1         2  
  1         6  
300              
301             key ref => (isa => 'Str', location => '$ref');
302             }
303              
304             package Swagger::Schema::V3::RequestBody {
305 1     1   2661 use MooseX::DataModel;
  1         5  
  1         4  
306 1     1   8458 use namespace::autoclean;
  1         3  
  1         4  
307              
308             key description => (isa => 'Str');
309             object content => (isa => 'Swagger::Schema::V3::MediaType', required => 1);
310             key required => (isa => 'Bool', default => 0);
311             }
312              
313             package Swagger::Schema::V3::Operation {
314 1     1   2646 use MooseX::DataModel;
  1         2  
  1         4  
315 1     1   8728 use namespace::autoclean;
  1         3  
  1         5  
316              
317             array tags => (isa => 'Str');
318             key summary => (isa => 'Str');
319             key description => (isa => 'Str');
320             key externalDocs => (isa => 'Swagger::Schema::V3::ExternalDocumentation');
321             key operationId => (isa => 'Str');
322             array parameters => (isa => 'Swagger::Schema::V3::RefOrParameter');
323              
324             key requestBody => (isa => 'Swagger::Schema::V3::RefOrRequestBody');
325             # TODO: keys for responses can be default or http codes
326             object responses => (isa => 'Swagger::Schema::V3::RefOrResponse', required => 1);
327             object callbacks => (isa => 'Swagger::Schema::V3::RefOrPath');
328             key deprecated => (isa => 'Bool');
329              
330             array security => (isa => 'HashRef');
331             array servers => (isa => 'Swagger::Schema::V3::Server');
332             }
333              
334             package Swagger::Schema::V3::Link {
335 1     1   2803 use MooseX::DataModel;
  1         2  
  1         6  
336 1     1   8367 use namespace::autoclean;
  1         3  
  1         4  
337              
338             key operationRef => (isa => 'Str');
339             key operationId => (isa => 'Str');
340             object parameters => (isa => 'Any');
341             object requestBody => (isa => 'Any');
342             key description => (isa => 'Str');
343             key server => (isa => 'Swagger::Schema::V3::Server');
344             }
345              
346             package Swagger::Schema::V3::Response {
347 1     1   2743 use MooseX::DataModel;
  1         2  
  1         5  
348 1     1   8467 use namespace::autoclean;
  1         4  
  1         6  
349              
350             key description => (isa => 'Str', required => 1);
351             object headers => (isa => 'Swagger::Schema::V3::RefOrHeader');
352             object content => (isa => 'Swagger::Schema::V3::MediaType');
353             object links => (isa => 'Swagger::Schema::V3::RefOrLink');
354             }
355              
356             package Swagger::Schema::V3::ExternalDocumentation {
357 1     1   2677 use MooseX::DataModel;
  1         6  
  1         6  
358 1     1   8807 use namespace::autoclean;
  1         4  
  1         5  
359              
360             key description => (isa => 'Str');
361             key url => (isa => 'Str', required => 1); #Must be in the format of a URL
362             }
363              
364             package Swagger::Schema::V3::Info {
365 1     1   2810 use MooseX::DataModel;
  1         4  
  1         4  
366 1     1   8432 use namespace::autoclean;
  1         2  
  1         5  
367              
368             key title => (isa => 'Str', required => 1);
369             key description => (isa => 'Str'); #Can contain GFM
370             key termsOfService => (isa => 'Str');
371             key contact => (isa => 'Swagger::Schema::V3::Contact');
372             key license => (isa => 'Swagger::Schema::V3::License');
373             key version => (isa => 'Str', required => 1);
374             #TODO: x-^ extensions
375             }
376              
377             package Swagger::Schema::V3::License {
378 1     1   2802 use MooseX::DataModel;
  1         5  
  1         4  
379 1     1   8443 use namespace::autoclean;
  1         3  
  1         5  
380              
381             key name => (isa => 'Str', required => 1);
382             key url => (isa => 'Str'); #Must be in the format of a URL
383             }
384              
385             package Swagger::Schema::V3::Contact {
386 1     1   2712 use MooseX::DataModel;
  1         3  
  1         8  
387 1     1   8755 use namespace::autoclean;
  1         3  
  1         5  
388              
389             key name => (isa => 'Str');
390             key url => (isa => 'Str');
391             key email => (isa => 'Str');
392             }
393              
394             ### main pod documentation begin ###
395              
396             =encoding UTF-8
397              
398             =head1 NAME
399              
400             Swagger::Schema::V3 - Object access to OpenAPI V3 schema files
401              
402             =head1 SYNOPSIS
403              
404             use File::Slurp;
405             my $data = read_file($swagger_file);
406             my $schema = Swagger::Schema::V3->MooseX::DataModel::new_from_json($data);
407             # use the object model
408             say "This API consists of:";
409             foreach my $path (sort keys %{ $schema->paths }){
410             foreach my $http_verb (sort keys %{ $schema->paths->{ $path } }) {
411             say "$http_verb on $path";
412             }
413             }
414              
415             =head1 DESCRIPTION
416              
417             Get programmatic access to an OpenAPI V3 file.
418              
419             If you're trying to parse a V2 file, take a look at L<Swagger::Schema>
420              
421             =head1 OBJECT MODEL
422              
423             The object model is defined with L<MooseX::DataModel>. Take a look at the
424             C<lib/Swagger/Schema/V3.pm> file or the swagger spec
425             L<https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md>
426             to know what you can find inside the objects
427              
428             =head1 SEE ALSO
429              
430             L<https://github.com/OAI/OpenAPI-Specification>
431              
432             L<http://swagger.io>
433              
434             =head1 AUTHOR
435              
436             Jose Luis Martinez
437             CAPSiDE
438             jlmartinez@capside.com
439              
440             =head1 BUGS and SOURCE
441              
442             The source code is located here: https://github.com/pplu/swagger-schema-perl
443              
444             Please report bugs to: https://github.com/pplu/swagger-schema-perl/issues
445              
446             =head1 COPYRIGHT and LICENSE
447              
448             Copyright (c) 2015 by CAPSiDE
449              
450             This code is distributed under the Apache 2 License. The full text of the
451             license can be found in the LICENSE file included with this module.