Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter view methods with rule methods #173

Merged
merged 2 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ Contributors (chronological)
- Evgeny Seliverstov `@theirix <https://github.com/theirix>`_
- Michael Bangert `@Bangertm <https://github.com/Bangertm>`_
- Bastien Sevajol `@buxx <https://github.com/buxx>`_
- Durmus Karatay `@ukaratay <https://github.com/ukaratay>`_
9 changes: 5 additions & 4 deletions apispec/ext/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ def path_from_view(spec, view, **kwargs):
if hasattr(view, 'view_class') and issubclass(view.view_class, MethodView):
operations = {}
for method in view.methods:
method_name = method.lower()
method = getattr(view.view_class, method_name)
docstring_yaml = utils.load_yaml_from_docstring(method.__doc__)
operations[method_name] = docstring_yaml or dict()
if method in rule.methods:
method_name = method.lower()
method = getattr(view.view_class, method_name)
docstring_yaml = utils.load_yaml_from_docstring(method.__doc__)
operations[method_name] = docstring_yaml or dict()
path.operations.update(operations)
return path

Expand Down
29 changes: 29 additions & 0 deletions tests/test_ext_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ def hello():
assert get_op['description'] == 'get a greeting'
assert post_op['description'] == 'post a greeting'

def test_methods_from_rule(self, app, spec):
class HelloApi(MethodView):
"""Greeting API.
---
x-extension: global metadata
"""
def get(self):
"""A greeting endpoint.
---
description: get a greeting
responses:
200:
description: said hi
"""
return 'hi'

def post(self):
return 'hi'

def delete(self):
return 'hi'

method_view = HelloApi.as_view('hi')
app.add_url_rule("/hi", view_func=method_view, methods=('GET', 'POST'))
spec.add_path(view=method_view)
assert 'get' in spec._paths['/hi']
assert 'post' in spec._paths['/hi']
assert 'delete' not in spec._paths['/hi']

def test_integration_with_docstring_introspection(self, app, spec):

@app.route('/hello')
Expand Down