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

Skip raw blueprint from OpenAPI generating #37

Merged
merged 1 commit into from
Apr 29, 2021
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
7 changes: 5 additions & 2 deletions apiflask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,9 @@ def _make_tags(self) -> List[Dict[str, Any]]:
if self.config['AUTO_TAGS']:
# auto-generate tags from blueprints
for blueprint_name, blueprint in self.blueprints.items():
if blueprint_name == 'openapi' or not blueprint.enable_openapi:
if blueprint_name == 'openapi' or \
not hasattr(blueprint, 'enable_openapi') or \
not blueprint.enable_openapi:
continue
tag: Dict[str, Any] = get_tag(blueprint, blueprint_name)
tags.append(tag) # type: ignore
Expand Down Expand Up @@ -614,7 +616,8 @@ def _update_auth_info(auth: HTTPAuthType) -> None:
blueprint_name: Optional[str] = None # type: ignore
if '.' in rule.endpoint:
blueprint_name = rule.endpoint.split('.', 1)[0]
if not self.blueprints[blueprint_name].enable_openapi:
if not hasattr(self.blueprints[blueprint_name], 'enable_openapi') or \
not self.blueprints[blueprint_name].enable_openapi:
continue
# add a default 200 response for bare views
if not hasattr(view_func, '_spec'):
Expand Down
2 changes: 1 addition & 1 deletion apiflask/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def decorator(f):
if isinstance(f, MethodViewType):
# MethodView class
view_func = f.as_view(endpoint)
if self.enable_openapi:
if hasattr(self, 'enable_openapi') and self.enable_openapi:
view_func._method_spec = {}
if not hasattr(view_func, '_spec'):
view_func._spec = {}
Expand Down
38 changes: 38 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from openapi_spec_validator import validate_spec
from flask.views import MethodView
from flask import Blueprint

from apiflask import APIFlask
from apiflask import APIBlueprint
from apiflask import input
from apiflask import Schema
from apiflask.fields import Integer
Expand Down Expand Up @@ -106,3 +109,38 @@ def error():
assert rv.status_code == 200
assert 'message' in rv.json
assert rv.json['message'] == 'something was wrong'


def test_skip_raw_blueprint(app, client):
raw_bp = Blueprint('raw', __name__)
api_bp = APIBlueprint('api', __name__, tag='test')

@raw_bp.route('/foo')
def foo():
pass

@raw_bp.route('/bar')
class Bar(MethodView):
def get(self):
pass

@api_bp.get('/baz')
def baz():
pass

@api_bp.route('/spam')
class Spam(MethodView):
def get(self):
pass

app.register_blueprint(raw_bp)
app.register_blueprint(api_bp)

rv = client.get('/openapi.json')
assert rv.status_code == 200
validate_spec(rv.json)
assert rv.json['tags'] == [{'name': 'test'}]
assert '/foo' not in rv.json['paths']
assert '/bar' not in rv.json['paths']
assert '/baz' in rv.json['paths']
assert '/spam' in rv.json['paths']