Skip to content

Commit fb7e879

Browse files
committed
Allow integer response code
1 parent 89f5452 commit fb7e879

File tree

7 files changed

+57
-12
lines changed

7 files changed

+57
-12
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from yaml.constructor import SafeConstructor
2+
3+
4+
class ExtendedSafeConstructor(SafeConstructor):
5+
6+
def construct_mapping(self, node, deep=False):
7+
"""While yaml supports integer keys, these are not valid in
8+
json, and will break jsonschema. This method coerces all keys
9+
to strings.
10+
"""
11+
mapping = super(ExtendedSafeConstructor, self).construct_mapping(
12+
node, deep)
13+
14+
return {
15+
(str(key) if isinstance(key, int) else key): mapping[key]
16+
for key in mapping
17+
}

openapi_spec_validator/handlers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33

44
from six.moves.urllib.parse import urlparse
55
from six.moves.urllib.request import urlopen
6-
from yaml import safe_load
6+
from yaml import load
7+
8+
from openapi_spec_validator.loaders import ExtendedSafeLoader
79

810

911
class UrlHandler:
1012
"""OpenAPI spec validator URL scheme handler."""
1113

12-
def __init__(self, *allowed_schemes):
14+
def __init__(self, *allowed_schemes, **options):
1315
self.allowed_schemes = allowed_schemes
16+
self.options = options
17+
18+
@property
19+
def loader(self):
20+
return self.options.get('loader', ExtendedSafeLoader)
1421

1522
def __call__(self, url, timeout=1):
1623
assert urlparse(url).scheme in self.allowed_schemes
1724

1825
with contextlib.closing(urlopen(url, timeout=timeout)) as fh:
19-
return safe_load(fh)
26+
return load(fh, self.loader)

openapi_spec_validator/loaders.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from yaml.composer import Composer
2+
from yaml.parser import Parser
3+
from yaml.reader import Reader
4+
from yaml.resolver import Resolver
5+
from yaml.scanner import Scanner
6+
7+
from openapi_spec_validator.constructors import ExtendedSafeConstructor
8+
9+
10+
class ExtendedSafeLoader(
11+
Reader, Scanner, Parser, Composer, ExtendedSafeConstructor, Resolver):
12+
13+
def __init__(self, stream):
14+
Reader.__init__(self, stream)
15+
Scanner.__init__(self)
16+
Parser.__init__(self)
17+
Composer.__init__(self)
18+
ExtendedSafeConstructor.__init__(self)
19+
Resolver.__init__(self)

openapi_spec_validator/schemas.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
from pkg_resources import resource_filename
55
from six.moves.urllib import parse, request
6-
from yaml import safe_load
6+
from yaml import load
7+
8+
from openapi_spec_validator.loaders import ExtendedSafeLoader
79

810

911
def get_openapi_schema(version):
@@ -15,7 +17,7 @@ def get_openapi_schema(version):
1517
return schema, schema_url
1618

1719

18-
def read_yaml_file(path):
20+
def read_yaml_file(path, loader=ExtendedSafeLoader):
1921
"""Open a file, read it and return its contents."""
2022
with open(path) as fh:
21-
return safe_load(fh)
23+
return load(fh, loader)

tests/integration/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from yaml import safe_load
77

88
from openapi_spec_validator import openapi_v3_spec_validator
9+
from openapi_spec_validator.schemas import read_yaml_file
910

1011

1112
def spec_url(spec_file, schema='file'):
@@ -17,8 +18,7 @@ def spec_url(spec_file, schema='file'):
1718
def spec_from_file(spec_file):
1819
directory = path.abspath(path.dirname(__file__))
1920
path_full = path.join(directory, spec_file)
20-
with open(path_full) as fh:
21-
return safe_load(fh)
21+
return read_yaml_file(path_full)
2222

2323

2424
def spec_from_url(spec_url):

tests/integration/data/v2.0/petstore.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ paths:
2727
type: integer
2828
format: int32
2929
responses:
30-
"200":
30+
200:
3131
description: A paged array of pets
3232
headers:
3333
x-next:
@@ -45,7 +45,7 @@ paths:
4545
tags:
4646
- pets
4747
responses:
48-
"201":
48+
'201':
4949
description: Null response
5050
default:
5151
description: unexpected error
@@ -64,7 +64,7 @@ paths:
6464
description: The id of the pet to retrieve
6565
type: string
6666
responses:
67-
"200":
67+
'200':
6868
description: Expected response to a valid request
6969
schema:
7070
$ref: '#/definitions/Pets'

tests/integration/data/v3.0/petstore.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ paths:
2222
type: integer
2323
format: int32
2424
responses:
25-
'200':
25+
200:
2626
description: An paged array of pets
2727
headers:
2828
x-next:

0 commit comments

Comments
 (0)