Skip to content

Commit

Permalink
make sure updates to Specification class are included in the move
Browse files Browse the repository at this point in the history
  • Loading branch information
dtkav committed Nov 9, 2018
1 parent 57765b3 commit d2a3595
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions connexion/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ def __len__(self):

@staticmethod
def _load_spec_from_file(arguments, specification):
from openapi_spec_validator.loaders import ExtendedSafeLoader
"""
Loads a YAML specification file, optionally rendering it with Jinja2.
Takes:
arguments - passed to Jinja2 renderer
specification - path to specification
"""
arguments = arguments or {}

with specification.open(mode='rb') as openapi_yaml:
Expand All @@ -89,10 +94,13 @@ def _load_spec_from_file(arguments, specification):
openapi_template = contents.decode('utf-8', 'replace')

openapi_string = jinja2.Template(openapi_template).render(**arguments)
return yaml.load(openapi_string, ExtendedSafeLoader)
return yaml.safe_load(openapi_string)

@classmethod
def from_file(cls, spec, arguments=None):
"""
Takes in a path to a YAML file, and returns a Specification
"""
specification_path = pathlib.Path(spec)
spec = cls._load_spec_from_file(arguments, specification_path)
return cls.from_dict(spec)
Expand All @@ -116,6 +124,20 @@ def _get_spec_version(spec):

@classmethod
def from_dict(cls, spec):
"""
Takes in a dictionary, and returns a Specification
"""
def enforce_string_keys(obj):
# YAML supports integer keys, but JSON does not
if isinstance(obj, dict):
return {
str(k): enforce_string_keys(v)
for k, v
in six.iteritems(obj)
}
return obj

spec = enforce_string_keys(spec)
version = cls._get_spec_version(spec)
if version < (3, 0, 0):
return Swagger2Specification(spec)
Expand Down

0 comments on commit d2a3595

Please sign in to comment.