Skip to content

feat(views): override as_view method in MethodResource #244

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 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions flask_apispec/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
form typing import Any, Callable
import flask.views

from flask_apispec.annotations import activate
Expand Down Expand Up @@ -47,3 +48,34 @@ class MethodResource(flask.views.MethodView, metaclass=MethodResourceMeta):
exactly like `MethodView` but inherits **flask-apispec** annotations.
"""
methods = None

@classmethod
def as_view(
cls, name: str, *class_args: Any, **class_kwargs: Any
) -> Callable:
"""Override as_view method with support __apispec__ from class.

Example:
@doc(
tags=['pets'],
params={'pet_id': {'description': 'the pet name'}},
)
class CatResource(MethodResource):

@marshal_with(PetSchema)
def get(self, pet_id):
return Pet('calici', 'cat')

@marshal_with(PetSchema)
def put(self, pet_id):
return Pet('calici', 'cat')


cat_resource_view = CatResource.as_view('CatResource')

app.add_url_rule('/cat/<pet_id>', view_func=cat_resource_view)
docs.register(cat_resource_view, endpoint='CatResource')
"""
view = super().as_view(name, *class_args, **class_kwargs)
view.__apispec__ = getattr(cls, "__apispec__", {})
return view