Skip to content

Update extension.py to support Blueprint Name in init #167

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_pet(pet_id):

docs.register(get_pet)

class MethodResourceMeta(ResourceMeta, flask.views.MethodViewType):
class MethodResourceMeta(ResourceMeta, flask.views.MethodView):
pass

class MethodResource(six.with_metaclass(MethodResourceMeta, flask.views.MethodView)):
Expand Down
27 changes: 16 additions & 11 deletions flask_apispec/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, app=None):
self.view_converter = None
self.resource_converter = None
self.spec = None
self.blueprint = None

if app:
self.init_app(app)
Expand All @@ -54,6 +55,7 @@ def init_app(self, app):
make_apispec(self.app.config.get('APISPEC_TITLE', 'flask-apispec'),
self.app.config.get('APISPEC_VERSION', 'v1'),
self.app.config.get('APISPEC_OAS_VERSION', '2.0'))
self.blueprint = self.app.config.get('APISPEC_BLUEPRINT') or make_blueprint()
self.add_swagger_routes()
self.resource_converter = ResourceConverter(self.app, spec=self.spec)
self.view_converter = ViewConverter(app=self.app, spec=self.spec)
Expand All @@ -68,23 +70,15 @@ def _defer(self, callable, *args, **kwargs):
bound()

def add_swagger_routes(self):
blueprint = flask.Blueprint(
'flask-apispec',
__name__,
static_folder='./static',
template_folder='./templates',
static_url_path='/flask-apispec/static',
)

json_url = self.app.config.get('APISPEC_SWAGGER_URL', '/swagger/')
if json_url:
blueprint.add_url_rule(json_url, 'swagger-json', self.swagger_json)
self.blueprint.add_url_rule(json_url, 'swagger-json', self.swagger_json)

ui_url = self.app.config.get('APISPEC_SWAGGER_UI_URL', '/swagger-ui/')
if ui_url:
blueprint.add_url_rule(ui_url, 'swagger-ui', self.swagger_ui)
self.blueprint.add_url_rule(ui_url, 'swagger-ui', self.swagger_ui)

self.app.register_blueprint(blueprint)
self.app.register_blueprint(self.blueprint)

def swagger_json(self):
return flask.jsonify(self.spec.to_dict())
Expand Down Expand Up @@ -148,6 +142,17 @@ def _register(self, target, endpoint=None, blueprint=None,
self.spec.path(**path)


def make_blueprint(name='flask-apispec', static_folder='./static', template_folder='./templates',
static_url_path='/flask-apispec/static'):
return flask.Blueprint(
name,
__name__,
static_folder=static_folder,
template_folder=template_folder,
static_url_path=static_url_path,
)


def make_apispec(title='flask-apispec', version='v1', openapi_version='2.0'):
return APISpec(
title=title,
Expand Down
2 changes: 1 addition & 1 deletion flask_apispec/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __new__(mcs, name, bases, attrs):
value.__apispec__['ismethod'] = True
return klass

class MethodResourceMeta(ResourceMeta, flask.views.MethodViewType):
class MethodResourceMeta(ResourceMeta, flask.views.MethodView):
pass

class MethodResource(six.with_metaclass(MethodResourceMeta, flask.views.MethodView)):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ def test_apispec_config(self, app):
assert docs.spec.title == 'test-extension'
assert docs.spec.version == '2.1'
assert docs.spec.openapi_version == '2.0'

def test_apispec_blueprint_config(self, app):
from flask_apispec.extension import make_blueprint
app.config['APISPEC_BLUEPRINT'] = make_blueprint(name='1')
docs = FlaskApiSpec(app)

assert docs.blueprint.name == '1'