apispec: Flask extension does not support blueprints
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.
# 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
Maybe something like spec.add_paths() could be added to handle compound view objects?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 19 (12 by maintainers)
A flask app with blueprints starts out looking something like:
Blueprints are not defined in the flask application context, so the paths must be register to apispec in the flask app context:
This defeats the benefit of blueprints encapsulating view specific details away from the core application logic.
My current alternative is to import the app into a spec file and explicitly register the views within the app context. My core app logic is no longer aware of the blueprints implementation details, but I am still leaking those details outside of the blueprint.
Having the flask extension be able to register blueprints would correct the app blueprint encapsulation issue:
Having the flask extension add all the paths in an application at once would be icing on the cake.
This is really about providing a blessed pattern for large apps that have turned to blueprints for organization, then find the apispec docs no longer apply.
Maybe
add_paths()is outside the scope of this project. An alternative is a wrapper library likeapispec-flask-blueprints(following the example ofmarshmallow-jsonapi).Hello, so what’s the best approach for this issue? @sloria I would like to use blueprints.
I’ve come here because I try to add apispec to document an API that I have in a blueprint, I use flask view functions and not MethodViews, and ends up with a solution that believe interesting in this discussion.
As the url paths on flask are defined as Rules on Flask.url_map I define a helper based on
path_from_view()that loads the path from a Rule object:I ignored the
current_app.config['APPLICATION_ROOT']inpath_from_view()as I couldn’t find a standard explanation about it’s use.Then I created a function that iter the rules on
url_mapand add the path for the matching rules:And that’s it, all the paths in my blueprint documented.
@deckar01:
Notice a blueprint can be registered with a custom
url_prefix:So you may need to do:
Ah, I see; the benefit would be that plugins could implement a paths_helper that would abstract extracting paths. Thanks for the clarification.
An
add_pathsmethod is within scope ofapispec. The question is whether is worth the increased API surface area. You presented a valid use case, and I would gladly review and merge a PR for it.@andho If I remember well I had all my API inside a blueprint, sharing the Flask App with ‘normal’ web endpoints in other blueprints, but only one blueprint having the whole API.
Nothing avoids you to create the
specat the app level and use theadd_paths_for_blueprintfunction to add the paths from different blueprints to the spec.@tinproject Tried this out for myself almost worked perfectly, except for me the
len(ep) == 1condition should have resulted in acontinueand not abreakbecause there was an app route in between my blueprint routes. Not sure why that was the case but none the less thank you for this contribution!