Skip to content

Commit

Permalink
Load summary/description information from view function docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Aug 21, 2018
1 parent 50afe3b commit bdaa63d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
8 changes: 6 additions & 2 deletions flask_rest_api/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from apispec.ext.marshmallow.openapi import __location_map__

from .utils import deepupdate
from .utils import deepupdate, load_info_from_docstring
from .args_parser import parser
from .response import response
from .pagination import paginate, pagination_parameters_schema_factory
Expand Down Expand Up @@ -70,7 +70,11 @@ def _store_endpoint_docs(self, endpoint, obj, **kwargs):
endpoint_doc = self._docs.setdefault(endpoint, OrderedDict())

def store_method_docs(method, function):
doc = getattr(function, '_apidoc', {})
# Get summary/description from docstring
docstring = function.__doc__
doc = load_info_from_docstring(docstring) if docstring else {}
# Update doc with description from @doc decorator
doc.update(getattr(function, '_apidoc', {}))
# Add function doc to table for later registration
method_l = method.lower()
# Check another doc was not already registed for endpoint/method
Expand Down
38 changes: 38 additions & 0 deletions tests/test_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,44 @@ def patch(self):
assert path[method]['summary'] == 'Dummy {}'.format(method)
assert path[method]['description'] == 'Do dummy {}'.format(method)

def test_blueprint_doc_info_from_docstring(self, app):
api = Api(app)
blp = Blueprint('test', __name__, url_prefix='/test')

@blp.route('/')
class Resource(MethodView):

def get(self):
"""Docstring get summary"""

def put(self):
"""Docstring put summary
Docstring put description
"""

@blp.doc(
summary='Decorator patch summary',
description='Decorator patch description'
)
def patch(self):
"""Docstring patch summary
Docstring patch description
"""

api.register_blueprint(blp)
spec = api.spec.to_dict()
path = spec['paths']['/test/']

assert path['get']['summary'] == 'Docstring get summary'
assert 'description' not in path['get']
assert path['put']['summary'] == 'Docstring put summary'
assert path['put']['description'] == 'Docstring put description'
# @doc decorator overrides docstring
assert path['patch']['summary'] == 'Decorator patch summary'
assert path['patch']['description'] == 'Decorator patch description'

def test_blueprint_enforce_method_order(self, app):
api = Api(app)
blp = Blueprint('test', __name__, url_prefix='/test')
Expand Down

0 comments on commit bdaa63d

Please sign in to comment.