marshmallow-code / marshmallow-code/apispec-webframeworks
Only single path added for Flask API MethodViews
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 33
- Forks
- 24
- Avg merge
- 5h 38m
- Merged PRs (30d)
- 4
Description
**Issue by [mathewmarcus](https://github.com/mathewmarcus)**
_Thursday Jan 18, 2018 at 21:14 GMT_
_Originally opened as https://github.com/marshmallow-code/apispec/issues/181_
----
The [Flask documentation](http://flask.pocoo.org/docs/0.12/views/#method-views-for-apis) gives a use case - for RESTful APIs - where a single method view can be added under multiple URL rules. The following code snippet is provided as an example.
```python
class UserAPI(MethodView):
def get(self, user_id):
if user_id is None:
# return a list of users
pass
else:
# expose a single user
pass
def post(self):
# create a new user
pass
def delete(self, user_id):
# delete a single user
pass
def put(self, user_id):
# update a single user
pass
user_view = UserAPI.as_view('user_api')
app.add_url_rule('/users/', defaults={'user_id': None},
view_func=user_view, methods=['GET',])
app.add_url_rule('/users/', view_func=user_view, methods=['POST',])
app.add_url_rule('/users/', view_func=user_view,
methods=['GET', 'PUT', 'DELETE'])
```
If we define the following apispec and add the above view function like so
```python
spec = APISpec(
title='Swagger Petstore',
version='1.0.0',
plugins=[
'apispec.ext.flask',
'apispec.ext.marshmallow',
],
)
with app.test_request_context():
spec.add_path(view=user_view)
```
only one of the above paths would be included in the generated apispec.
Looking at the `apispec.ext.flask` module, it seems that this behavior is a result of line 92 in the `_rule_for_view` function.
```python
# WARNING: Assume 1 rule per view function for now
rule = current_app.url_map._rules_by_endpoint[endpoint][0]
```
Given the comment, it seems like this behavior is expected. Is there any interest in/intent to modify this to enable all of the paths to be added to the apispec in the above situation?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in the apispec.ext.flask module at _rule_for_view and its _rules_by_endpoint lookup. Reproduce the Flask MethodView example with multiple URL rules, then verify that adding the view includes every associated path in the generated apispec rather than only the first rule.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, python
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100