Skip to content

Commit

Permalink
Merge pull request #101 from DanCardin/master
Browse files Browse the repository at this point in the history
Add support for vendor extensions in top-level config.
  • Loading branch information
rochacbruno committed May 15, 2017
2 parents eb9607a + a2db517 commit 3464ef1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pep8:

.PHONY: test
test: pep8
@py.test tests -s -vv --cov --cov-config=.coveragerc
@py.test tests -s -vv --cov --cov-config=.coveragerc --doctest-modules flasgger

.PHONY: sdist
sdist: test
Expand Down
29 changes: 29 additions & 0 deletions examples/top_level_vendor_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Ensure that top-level vendor extension config is preserved
"""

from flasgger import Swagger
from flask import Flask, jsonify

app = Flask(__name__)
app.config['SWAGGER'] = {
'title': 'Vendor extension test',
'uiversion': 2,
'x-groupTag': 'Test',
}
Swagger(app)


def test_swag(client, specs_data):
"""
This test is runs automatically in Travis CI
:param client: Flask app test client
:param specs_data: {'url': {swag_specs}} for every spec in app
"""
assert 'x-groupTag' in specs_data['/apispec_1.json']
assert specs_data['/apispec_1.json']['x-groupTag'] == 'Test'


if __name__ == "__main__":
app.run(debug=True)
7 changes: 6 additions & 1 deletion flasgger/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from flasgger.marshmallow_apispec import SwaggerView, convert_schemas
from flasgger.utils import (extract_definitions, has_valid_dispatch_view_docs,
is_valid_method_view, parse_definition_docstring,
parse_docstring)
parse_docstring, get_vendor_extension_fields)

NO_SANITIZER = lambda text: text # noqa
BR_SANITIZER = lambda text: text.replace('\n', '<br/>') if text else text # noqa
Expand Down Expand Up @@ -138,6 +138,11 @@ def get(self):
"definitions": self.config.get('definitions') or defaultdict(dict)
}

# Support extension properties in the top level config
top_level_extension_options = get_vendor_extension_fields(self.config)
if top_level_extension_options:
data.update(top_level_extension_options)

# if True schemaa ids will be prefized by function_method_{id}
# for backwards compatibility with <= 0.5.14
prefix_ids = self.config.get('prefix_ids')
Expand Down
15 changes: 14 additions & 1 deletion flasgger/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import jsonschema
import yaml
from flask import Response, abort, request
from flask import abort, request, Response
from flask.views import MethodView
from jsonschema import ValidationError # noqa
from six import string_types
Expand Down Expand Up @@ -520,3 +520,16 @@ def is_valid_method_view(endpoint):
return issubclass(klass, MethodView)
except TypeError:
return False


def get_vendor_extension_fields(mapping):
"""
Identify vendor extension fields and extract them into a new dictionary.
Examples:
>>> get_vendor_extension_fields({'test': 1})
{}
>>> get_vendor_extension_fields({'test': 1, 'x-test': 2})
{'x-test': 2}
"""
return {k: v for k, v in mapping.items() if k.startswith('x-')}

0 comments on commit 3464ef1

Please sign in to comment.