marshmallow-code / marshmallow-code/apispec-webframeworks

Flask extension does not support blueprints

Open
#11 19 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

flask ready to claim
Dominant language
Python
Stars
33
Forks
24
Avg merge
5h 38m
Merged PRs (30d)
4

Description

**Issue by [deckar01](https://github.com/deckar01)**
_Wednesday Apr 27, 2016 at 17:42 GMT_
_Originally opened as https://github.com/marshmallow-code/apispec/issues/68_

----

I have organized my views with Flask blueprints, but am unable to document them in the view file, because it is not executed in the application context.

``` py
# app/views/example.py

from flask import Blueprint
example_view = Blueprint('example_view', __name__)

from app.spec import spec

@bp.route('example', methods=['GET'])
def get_example():
"""An example.
---
get:
description: Get an example
responses:
200:
description: An example
schema: ExampleSchema
"""
return 'example', 200

spec.add_path(view=get_example)
```

```
...
File "/usr/local/lib/python2.7/site-packages/apispec/core.py", line 170, in add_path
self, path=path, operations=operations, **kwargs
File "/usr/local/lib/python2.7/site-packages/apispec/ext/flask.py", line 62, in path_from_view
rule = _rule_for_view(view)
File "/usr/local/lib/python2.7/site-packages/apispec/ext/flask.py", line 38, in _rule_for_view
view_funcs = current_app.view_functions
File "/usr/local/lib/python2.7/site-packages/werkzeug/local.py", line 343, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/local/lib/python2.7/site-packages/werkzeug/local.py", line 302, in _get_current_object
return self.__local()
File "/usr/local/lib/python2.7/site-packages/flask/globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
```

Is there a way to keep the spec declarations with the blueprint? (It seems like there might not be.)

Do you think it would be useful to add the ability to add all the views from a blueprint at once?

I noticed that the flask extension seems to acknowledge that a view could contain multiple paths, but assumes it only contains one. [apispec/ext/flask.py#L46](https://github.com/marshmallow-code/apispec/blob/1076180/apispec/ext/flask.py#L46)

Maybe something like `spec.add_paths()` could be added to handle compound view objects?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading apispec/ext/flask.py, especially _rule_for_view and the path_from_view flow, then review how spec.add_path is used in the reported blueprint example. Determine how blueprint-contained views and views with multiple paths should be discovered without requiring an application context. Done means the requested declarations can remain with a Flask blueprint and compound view objects are handled consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.