1
1
# -*- coding: utf-8 -*-
2
+ """
3
+ Validate Swagger Specs against the Swagger 1.2 Specification. The
4
+ validator aims to check for full compliance with the Specification.
2
5
3
- # Validate Swagger Specs against the Swagger 1.2 Specification. The
4
- # validator aims to check for full compliance with the Specification.
5
- #
6
- # The validator uses the published jsonschema files for basic structural
7
- # validation, augmented with custom validation code where necessary.
8
- #
9
- # https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md
6
+ The validator uses the published jsonschema files for basic structural
7
+ validation, augmented with custom validation code where necessary.
8
+
9
+ https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md
10
+ """
10
11
import logging
11
12
import os
12
- import urllib2
13
- from urlparse import urlparse
13
+
14
+ import six
15
+ from six .moves .urllib .parse import urlparse
14
16
15
17
from swagger_spec_validator .common import (SwaggerValidationError ,
16
- TIMEOUT_SEC ,
17
- json ,
18
+ load_json ,
18
19
validate_json ,
19
20
wrap_exception )
20
21
26
27
27
28
def get_model_ids (api_declaration ):
28
29
models = api_declaration .get ('models' , {})
29
- model_ids = []
30
- for model in models .itervalues ():
31
- model_ids .append (model ['id' ])
32
- return model_ids
30
+ return [model ['id' ] for model in six .itervalues (models )]
33
31
34
32
35
33
def get_resource_path (url , resource ):
@@ -72,8 +70,7 @@ def validate_spec_url(url):
72
70
"""
73
71
74
72
log .info ('Validating %s' % url )
75
- resource_listing = json .load (urllib2 .urlopen (url , timeout = TIMEOUT_SEC ))
76
- validate_spec (resource_listing , url )
73
+ validate_spec (load_json (url ), url )
77
74
78
75
79
76
def validate_spec (resource_listing , url ):
@@ -95,8 +92,7 @@ def validate_spec(resource_listing, url):
95
92
for api in resource_listing ['apis' ]:
96
93
path = get_resource_path (url , api ['path' ])
97
94
log .info ('Validating %s' % path )
98
- api_declaration = json .load (urllib2 .urlopen (path , timeout = TIMEOUT_SEC ))
99
- validate_api_declaration (api_declaration )
95
+ validate_api_declaration (load_json (path ))
100
96
101
97
102
98
def validate_data_type (obj , model_ids , allow_arrays = True , allow_voids = False ,
@@ -163,7 +159,7 @@ def validate_model(model, model_name, model_ids):
163
159
'Model "%s": required property "%s" not found' %
164
160
(model_name , required ))
165
161
166
- for prop_name , prop in model .get ('properties' , {}). iteritems ( ):
162
+ for prop_name , prop in six . iteritems ( model .get ('properties' , {})):
167
163
try :
168
164
validate_data_type (prop , model_ids , allow_refs = True )
169
165
except SwaggerValidationError as e :
@@ -219,7 +215,7 @@ def validate_api_declaration(api_declaration):
219
215
for api in api_declaration ['apis' ]:
220
216
validate_api (api , model_ids )
221
217
222
- for model_name , model in api_declaration .get ('models' , {}). iteritems ( ):
218
+ for model_name , model in six . iteritems ( api_declaration .get ('models' , {})):
223
219
validate_model (model , model_name , model_ids )
224
220
225
221
0 commit comments