Validating response in MethodView
Validating response does not work for handlers in MethodView
from quart.views import MethodView
class UserView(MethodView):
methods = ["GET"]
@validate_response(UserResponse)
async def get(self):
user = db.get_user()
return user.model_dump()
This does not generate any response schema in OpenAPI json.
Or how is it possible to document MethodViews registered via .as_view() method? @pgjones
Thanks.
I think it is,
from quart.views import MethodView
class UserView(MethodView):
decorators = [validate_response(UserResponse)]
methods = ["GET"]
async def get(self):
user = db.get_user()
return user.model_dump()
As per https://flask.palletsprojects.com/en/stable/views/#view-decorators.
Please reopen if this doesn't work
The suggested solution won't work for class-based views, including MethodViews.
A couple of notes.
First, when using a MethodView Quart is capable of determining the supported methods. Adding methods = ["GET"] defeats this.
Second, if you use decorators = [validate_response(UserResponse)] this response is applied to all methods, not just GET.
I think handling schema generation with a generic class-based view would be a significant challenge. Handling MethodViews should be a little easier. As I noted Quart will populate methods. In OpenAPIProvider.build_paths the view func is a function that happens to have a view_class attribute. If the func has that attribute and the value happens to be an instance of MethodView perhaps then you can iterate over the supported methods to build the schema.