Support app.add_api() with API-dictionary as an alternative to file path

This commit is contained in:
Daniel DeFisher
2016-10-12 23:12:44 -04:00
parent 5892feb471
commit 5a6cdb51a2
3 changed files with 50 additions and 13 deletions

View File

@@ -81,7 +81,6 @@ class Api(object):
"""
self.debug = debug
self.resolver_error_handler = resolver_error_handler
self.swagger_yaml_path = pathlib.Path(swagger_yaml_path)
logger.debug('Loading specification: %s', swagger_yaml_path,
extra={'swagger_yaml': swagger_yaml_path,
'base_url': base_url,
@@ -90,20 +89,16 @@ class Api(object):
'swagger_path': swagger_path,
'swagger_url': swagger_url,
'auth_all_paths': auth_all_paths})
arguments = arguments or {}
with swagger_yaml_path.open(mode='rb') as swagger_yaml:
contents = swagger_yaml.read()
try:
swagger_template = contents.decode()
except UnicodeDecodeError:
swagger_template = contents.decode('utf-8', 'replace')
swagger_string = jinja2.Template(swagger_template).render(**arguments)
self.specification = yaml.safe_load(swagger_string) # type: dict
logger.debug('Read specification', extra={'spec': self.specification})
if isinstance(swagger_yaml_path, dict):
self.specification = swagger_yaml_path
else:
self.swagger_yaml_path = pathlib.Path(swagger_yaml_path)
self.load_spec_from_file(arguments, swagger_yaml_path)
self.specification = compatibility_layer(self.specification)
logger.debug('Read specification', extra={'spec': self.specification})
# Avoid validator having ability to modify specification
spec = copy.deepcopy(self.specification)
validate_spec(spec)
@@ -313,3 +308,16 @@ class Api(object):
:type filename: str
"""
return flask.send_from_directory(str(self.swagger_path), filename)
def load_spec_from_file(self, arguments, swagger_yaml_path):
arguments = arguments or {}
with swagger_yaml_path.open(mode='rb') as swagger_yaml:
contents = swagger_yaml.read()
try:
swagger_template = contents.decode()
except UnicodeDecodeError:
swagger_template = contents.decode('utf-8', 'replace')
swagger_string = jinja2.Template(swagger_template).render(**arguments)
self.specification = yaml.safe_load(swagger_string) # type: dict

View File

@@ -134,7 +134,12 @@ class App(object):
# TODO test if base_url starts with an / (if not none)
arguments = arguments or dict()
arguments = dict(self.arguments, **arguments) # copy global arguments and update with api specfic
if isinstance(swagger_file, dict):
yaml_path = swagger_file
else:
yaml_path = self.specification_dir / swagger_file
api = Api(swagger_yaml_path=yaml_path,
base_url=base_path, arguments=arguments,
swagger_json=swagger_json,

View File

@@ -2,6 +2,8 @@ from connexion.app import App
from connexion.exceptions import InvalidSpecification
import pytest
import jinja2
import yaml
from conftest import TEST_FOLDER, build_app_from_fixture
@@ -52,6 +54,28 @@ def test_no_swagger_json_app(simple_api_spec_dir):
assert swagger_json.status_code == 404
def test_dic_as_yaml_path(simple_api_spec_dir):
swagger_yaml_path = simple_api_spec_dir / 'swagger.yaml'
with swagger_yaml_path.open(mode='rb') as swagger_yaml:
contents = swagger_yaml.read()
try:
swagger_template = contents.decode()
except UnicodeDecodeError:
swagger_template = contents.decode('utf-8', 'replace')
swagger_string = jinja2.Template(swagger_template).render({})
specification = yaml.safe_load(swagger_string) # type: dict
app = App(__name__, 5001, simple_api_spec_dir, debug=True)
app.add_api(specification)
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
assert swagger_json.status_code == 200
def test_swagger_json_api(simple_api_spec_dir):
""" Verify the swagger.json file is returned for default setting passed to api. """
app = App(__name__, 5001, simple_api_spec_dir, debug=True)