File Coverage

blib/lib/Swagger/Schema.pm
Criterion Covered Total %
statement 120 120 100.0
branch n/a
condition n/a
subroutine 40 40 100.0
pod n/a
total 160 160 100.0


line stmt bran cond sub pod time code
1 1     1   80542 use Moose::Util::TypeConstraints;
  1         290070  
  1         12  
2              
3             coerce 'Swagger::Schema::Parameter',
4             from 'HashRef',
5             via {
6             if (exists $_->{ in } and $_->{ in } eq 'body') {
7             return Swagger::Schema::BodyParameter->new($_);
8             } elsif ($_->{ '$ref' }) {
9             return Swagger::Schema::RefParameter->new($_);
10             } else {
11             return Swagger::Schema::OtherParameter->new($_);
12             }
13             };
14              
15             package Swagger::Schema {
16             our $VERSION = '1.03';
17             #ABSTRACT: Object model for Swagger schema files
18 1     1   2880 use MooseX::DataModel;
  1         186042  
  1         5  
19 1     1   8863 use Moose::Util::TypeConstraints;
  1         3  
  1         4  
20 1     1   2134 use namespace::autoclean;
  1         8063  
  1         5  
21              
22             key swagger => (isa => enum([ '2.0' ]), required => 1);
23             key info => (isa => 'Swagger::Schema::Info', required => 1);
24             key host => (isa => 'Str'); #MAY contain a port
25             key basePath => (isa => subtype(as 'Str', where { $_ =~ /^\// }));
26             array schemes => (isa => enum([ 'http', 'https', 'ws', 'wss']));
27             array consumes => (isa => 'Str'); #Str must be mime type: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#mimeTypes
28             array produces => (isa => 'Str');
29             object paths => (isa => 'Swagger::Schema::Path', required => 1);
30             object definitions => (isa => 'Swagger::Schema::Schema');
31             object parameters => (isa => 'Swagger::Schema::Parameter');
32             object responses => (isa => 'Swagger::Schema::Response');
33             object securityDefinitions => (isa => 'Swagger::Schema::SecurityScheme');
34             # The below declaration is declared as Any, because there is no way to represent
35             # any array of hashrefs of strings
36             #array security => (isa => 'Swagger::Schema::SecurityRequirement');
37             array security => (isa => 'Any');
38             array tags => (isa => 'Swagger::Schema::Tag');
39             key externalDocs => (isa => 'Swagger::Schema::ExternalDocumentation');
40             }
41              
42             package Swagger::Schema::SecurityScheme {
43 1     1   2941 use MooseX::DataModel;
  1         3  
  1         9  
44 1     1   8953 use namespace::autoclean;
  1         2  
  1         5  
45            
46             }
47              
48             #package Swagger::Schema::SecurityRequirement {
49             # use MooseX::DataModel;
50             #
51             # # See the security attribute in Swagger::Schema
52             # # this object is more like a plain hashref
53             # # it only has a patterned field {name}, which holds an array of strings
54             #
55             # no MooseX::DataModel;
56             #}
57              
58             package Swagger::Schema::Path {
59 1     1   2636 use MooseX::DataModel;
  1         2  
  1         6  
60 1     1   8403 use namespace::autoclean;
  1         8  
  1         5  
61              
62             key get => (isa => 'Swagger::Schema::Operation');
63             key put => (isa => 'Swagger::Schema::Operation');
64             key post => (isa => 'Swagger::Schema::Operation');
65             key delete => (isa => 'Swagger::Schema::Operation');
66             key options => (isa => 'Swagger::Schema::Operation');
67             key head => (isa => 'Swagger::Schema::Operation');
68             key patch => (isa => 'Swagger::Schema::Operation');
69             array parameters => (isa => 'Swagger::Schema::Parameter');
70             }
71              
72             package Swagger::Schema::Tag {
73 1     1   2719 use MooseX::DataModel;
  1         3  
  1         4  
74 1     1   8641 use namespace::autoclean;
  1         3  
  1         5  
75              
76             key name => (isa => 'Str');
77             key description => (isa => 'Str');
78             key externalDocs => (isa => 'Swagger::Schema::ExternalDocumentation');
79             }
80              
81             enum 'Swagger::Schema::ParameterTypes',
82             [qw/string number integer boolean array file object/];
83              
84             package Swagger::Schema::Schema {
85 1     1   2689 use MooseX::DataModel;
  1         2  
  1         5  
86 1     1   8552 use namespace::autoclean;
  1         2  
  1         5  
87              
88             key ref => (isa => 'Str', location => '$ref');
89             key x_ms_client_flatten => (isa => 'Bool', location => 'x-ms-client-flatten');
90              
91             key type => (isa => 'Swagger::Schema::ParameterTypes');
92             key format => (isa => 'Str');
93             key allowEmptyValue => (isa => 'Bool');
94             key collectionFormat => (isa => 'Str');
95             key default => (isa => 'Any');
96             key maximum => (isa => 'Int');
97             key exclusiveMaximum => (isa => 'Bool');
98             key minimum => (isa => 'Int');
99             key exclusiveMinumum => (isa => 'Bool');
100             key maxLength => (isa => 'Int');
101             key minLength => (isa => 'Int');
102             key pattern => (isa => 'Str');
103             key maxItems => (isa => 'Int');
104             key minItems => (isa => 'Int');
105             key uniqueItems => (isa => 'Bool');
106             array enum => (isa => 'Any');
107             key multipleOf => (isa => 'Num');
108             #x-^ patterned fields
109              
110             key items => (isa => 'Swagger::Schema::Schema');
111             array allOf => (isa => 'Swagger::Schema::Schema');
112             object properties => (isa => 'Swagger::Schema::Schema');
113             key additionalProperties => (isa => 'Swagger::Schema::Schema');
114             key readOnly => (isa => 'Bool');
115             #key xml => (isa => 'Swagger::Schema::XML');
116             key externalDocs => (isa => 'Swagger::Schema::ExternalDocumentation');
117             key example => (isa => 'Any');
118             }
119              
120             package Swagger::Schema::Parameter {
121 1     1   2777 use MooseX::DataModel;
  1         2  
  1         4  
122 1     1   8929 use namespace::autoclean;
  1         2  
  1         7  
123              
124             key name => (isa => 'Str');
125             key in => (isa => 'Str');
126             key description => (isa => 'Str');
127             key required => (isa => 'Bool');
128             key x_ms_client_flatten => (isa => 'Bool', location => 'x-ms-client-flatten');
129             key x_ms_skip_url_encoding => (isa => 'Bool', location => 'x-ms-skip-url-encoding');
130             key x_ms_enum => (isa => 'Swagger::Schema::MSX::Enum', location => 'x-ms-enum');
131             key x_ms_parameter_grouping => (isa => 'Swagger::Schema::MSX::ParameterGrouping', location => 'x-ms-parameter-grouping');
132             key x_ms_client_request_id => (isa => 'Bool', location => 'x-ms-client-request-id');
133             key x_ms_client_name => (isa => 'Str', location => 'x-ms-client-name');
134             key x_ms_parameter_location => (isa => 'Str', location => 'x-ms-parameter-location');
135             }
136              
137             package Swagger::Schema::MSX::ParameterGrouping {
138 1     1   2936 use MooseX::DataModel;
  1         2  
  1         6  
139 1     1   9290 use namespace::autoclean;
  1         2  
  1         6  
140              
141             key name => (isa => 'Str');
142             key postfix => (isa => 'Str');
143             }
144              
145             package Swagger::Schema::MSX::Enum {
146 1     1   2693 use MooseX::DataModel;
  1         3  
  1         5  
147 1     1   8835 use namespace::autoclean;
  1         3  
  1         6  
148              
149             key name => (isa => 'Str');
150             key modelAsString => (isa => 'Bool');
151             }
152              
153             package Swagger::Schema::RefParameter {
154 1     1   2627 use MooseX::DataModel;
  1         2  
  1         5  
155 1     1   8535 use namespace::autoclean;
  1         3  
  1         4  
156              
157             extends 'Swagger::Schema::Parameter';
158             key ref => (isa => 'Str', location => '$ref');
159             }
160              
161             package Swagger::Schema::BodyParameter {
162 1     1   2642 use MooseX::DataModel;
  1         4  
  1         4  
163 1     1   8923 use namespace::autoclean;
  1         3  
  1         7  
164              
165             extends 'Swagger::Schema::Parameter';
166             key schema => (isa => 'Swagger::Schema::Schema', required => 1);
167             }
168              
169             package Swagger::Schema::OtherParameter {
170 1     1   2734 use MooseX::DataModel;
  1         2  
  1         6  
171 1     1   8983 use namespace::autoclean;
  1         4  
  1         6  
172              
173             extends 'Swagger::Schema::Parameter';
174              
175             key type => (isa => 'Swagger::Schema::ParameterTypes', required => 1);
176             key format => (isa => 'Str');
177             key allowEmptyValue => (isa => 'Bool');
178             object items => (isa => 'Any');
179             key collectionFormat => (isa => 'Str');
180             key default => (isa => 'Any');
181             key maximum => (isa => 'Int');
182             key exclusiveMaximum => (isa => 'Bool');
183             key minimum => (isa => 'Int');
184             key exclusiveMinumum => (isa => 'Bool');
185             key maxLength => (isa => 'Int');
186             key minLength => (isa => 'Int');
187             key pattern => (isa => 'Str');
188             key maxItems => (isa => 'Int');
189             key minItems => (isa => 'Int');
190             key uniqueItems => (isa => 'Bool');
191             array enum => (isa => 'Any');
192             key multipleOf => (isa => 'Num');
193             #x-^ patterned fields
194             }
195              
196             enum 'Swagger::Schema::CollectionFormat',
197             [qw/csv ssv tsv pipes/];
198              
199             package Swagger::Schema::Item {
200 1     1   2867 use MooseX::DataModel;
  1         2  
  1         6  
201 1     1   8995 use namespace::autoclean;
  1         2  
  1         6  
202              
203             key type => (isa => 'Swagger::Schema::ParameterTypes', required => 1);
204             key format => (isa => 'Str');
205            
206             array items => (isa => 'Swagger::Schema::Item');
207              
208             key collectionFormat => (isa => 'Swagger::Schema::CollectionFormat');
209             key default => (isa => 'Any');
210             key maximum => (isa => 'Num');
211             key exclusiveMaximum => (isa => 'Bool');
212             key minimum => (isa => 'Num');
213             key exclusiveMinimum => (isa => 'Bool');
214             key maxLength => (isa => 'Int');
215             key minLength => (isa => 'Int');
216             key pattern => (isa => 'Str');
217             key maxItems => (isa => 'Int');
218             key minItems => (isa => 'Int');
219             key uniqueItems => (isa => 'Bool');
220             array enum => (isa => 'Any');
221             key multipleOf => (isa => 'Num');
222             #x-^ patterned fields
223             }
224              
225             package Swagger::Schema::Operation {
226 1     1   2800 use MooseX::DataModel;
  1         3  
  1         5  
227 1     1   8504 use namespace::autoclean;
  1         3  
  1         4  
228              
229             array tags => (isa => 'Str');
230             key summary => (isa => 'Str');
231             key description => (isa => 'Str');
232             key externalDocs => (isa => 'Swagger::Schema::ExternalDocumentation');
233             key operationId => (isa => 'Str');
234             array consumes => (isa => 'Str'); #Must be a Mime Type
235             array produces => (isa => 'Str'); #Must be a Mime Type
236             array parameters => (isa => 'Swagger::Schema::Parameter');
237             object responses => (isa => 'Swagger::Schema::Response');
238             array schemes => (isa => 'Str');
239             key deprecated => (isa => 'Bool');
240             key x_ms_long_running_operation => (isa => 'Bool', location => 'x-ms-long-running-operation');
241             #key security => (isa =>
242             #TODO: x-^ fields
243             }
244              
245             package Swagger::Schema::Response {
246 1     1   2807 use MooseX::DataModel;
  1         3  
  1         17  
247 1     1   8424 use namespace::autoclean;
  1         3  
  1         5  
248              
249             key description => (isa => 'Str');
250             key schema => (isa => 'Swagger::Schema::Parameter');
251             object headers => (isa => 'Swagger::Schema::Header');
252             #key examples => (isa => '');
253             #TODO: patterned fields
254             }
255              
256             package Swagger::Schema::Header {
257 1     1   2661 use MooseX::DataModel;
  1         20  
  1         7  
258 1     1   8453 use namespace::autoclean;
  1         5  
  1         5  
259              
260             key description => (isa => 'Str');
261             key type => (isa => 'Str', required => 1);
262             key format => (isa => 'Str');
263             object items => (isa => 'HashRef');
264             key collectionFormat => (isa => 'Str');
265             key default => (isa => 'Any');
266             key maximum => (isa => 'Int');
267             key exclusiveMaximum => (isa => 'Bool');
268             key minimum => (isa => 'Int');
269             key exclusiveMinumum => (isa => 'Bool');
270             key maxLength => (isa => 'Int');
271             key minLength => (isa => 'Int');
272             key pattern => (isa => 'Str');
273             key maxItems => (isa => 'Int');
274             key minItems => (isa => 'Int');
275             key uniqueItems => (isa => 'Bool');
276             array enum => (isa => 'Any');
277             key multipleOf => (isa => 'Num');
278             }
279              
280             package Swagger::Schema::ExternalDocumentation {
281 1     1   4223 use MooseX::DataModel;
  1         4  
  1         5  
282 1     1   8490 use namespace::autoclean;
  1         2  
  1         6  
283              
284             key description => (isa => 'Str');
285             key url => (isa => 'Str', required => 1); #Must be in the format of a URL
286             }
287              
288             package Swagger::Schema::Info {
289 1     1   2670 use MooseX::DataModel;
  1         2  
  1         4  
290 1     1   8520 use namespace::autoclean;
  1         3  
  1         6  
291              
292             key title => (isa => 'Str', required => 1);
293             key description => (isa => 'Str'); #Can contain GFM
294             key termsOfService => (isa => 'Str');
295             key contact => (isa => 'Swagger::Schema::Contact');
296             key license => (isa => 'Swagger::Schema::License');
297             key version => (isa => 'Str', required => 1);
298             #TODO: x-^ extensions
299             }
300              
301             package Swagger::Schema::License {
302 1     1   2705 use MooseX::DataModel;
  1         2  
  1         5  
303 1     1   9015 use namespace::autoclean;
  1         3  
  1         6  
304              
305             key name => (isa => 'Str', required => 1);
306             key url => (isa => 'Str'); #Must be in the format of a URL
307             }
308              
309             package Swagger::Schema::Contact {
310 1     1   2714 use MooseX::DataModel;
  1         3  
  1         7  
311 1     1   9199 use namespace::autoclean;
  1         3  
  1         6  
312              
313             key name => (isa => 'Str');
314             key url => (isa => 'Str');
315             key email => (isa => 'Str');
316             }
317             ### main pod documentation begin ###
318              
319             =encoding UTF-8
320              
321             =head1 NAME
322              
323             Swagger::Schema - Object access to Swagger / OpenAPI schema files
324              
325             =head1 SYNOPSIS
326              
327             use File::Slurp;
328             my $data = read_file($swagger_file);
329             my $schema = Swagger::Schema->MooseX::DataModel::new_from_json($data);
330             # use the object model
331             say "This API consists of:";
332             foreach my $path (sort keys %{ $schema->paths }){
333             foreach my $http_verb (sort keys %{ $schema->paths->{ $path } }) {
334             say "$http_verb on $path";
335             }
336             }
337              
338             =head1 DESCRIPTION
339              
340             Get programmatic access to a Swagger / OpenAPI file.
341              
342             =head1 OBJECT MODEL
343              
344             The object model is defined with L<MooseX::DataModel>. Take a look at the
345             C<lib/Swagger/Schema.pm> file or the swagger spec
346             L<https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md>
347             to know what you can find inside the objects
348              
349             =head1 SEE ALSO
350              
351             L<https://github.com/OAI/OpenAPI-Specification>
352              
353             L<http://swagger.io>
354              
355             =head1 AUTHOR
356              
357             Jose Luis Martinez
358             CAPSiDE
359             jlmartinez@capside.com
360              
361             =head1 BUGS and SOURCE
362              
363             The source code is located here: https://github.com/pplu/swagger-schema-perl
364              
365             Please report bugs to: https://github.com/pplu/swagger-schema-perl/issues
366              
367             =head1 COPYRIGHT and LICENSE
368              
369             Copyright (c) 2015 by CAPSiDE
370              
371             This code is distributed under the Apache 2 License. The full text of the
372             license can be found in the LICENSE file included with this module.