quart-schema icon indicating copy to clipboard operation
quart-schema copied to clipboard

Validating response in MethodView

Open martn opened this issue 10 months ago • 1 comments

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.

martn avatar Feb 10 '25 08:02 martn

Or how is it possible to document MethodViews registered via .as_view() method? @pgjones Thanks.

martn avatar Feb 12 '25 10:02 martn

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

pgjones avatar Apr 21 '25 17:04 pgjones

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.

tyrius02 avatar Aug 31 '25 15:08 tyrius02