From 0ab6a550a809a662a37636f65840c63ed2b4d996 Mon Sep 17 00:00:00 2001 From: "oleh.perlovskyi" Date: Tue, 15 Nov 2022 17:41:30 +0200 Subject: [PATCH] feat(views): add method override as_view in MethodResource for support apispec in instance of a view --- flask_apispec/views.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/flask_apispec/views.py b/flask_apispec/views.py index 90a0b56..dc49f30 100644 --- a/flask_apispec/views.py +++ b/flask_apispec/views.py @@ -1,3 +1,4 @@ +form typing import Any, Callable import flask.views from flask_apispec.annotations import activate @@ -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/', 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