flask-smorest icon indicating copy to clipboard operation
flask-smorest copied to clipboard

Allow alt_response on MethodView

Open der-joel opened this issue 4 years ago • 5 comments

It would be a nice enhancement to allow the usage of the alt_response-Decorator on MethodView-classes. It could act as a shortcut to decorating every endpoint of the view with alt_response.

An example use case would be a custom converter that rejects any pet_id not present in the pet database. Since its declared in the route-decorator, every endpoint will raise 404 if the pet was not found:


@blp.route('/<object_id(must_exist=True):pet_id>')
@blp.alt_response(404, ErrorSchema)
class PetsById(MethodView):

    @blp.response(200, PetSchema)
    def get(self, pet_id):
        """Get pet by ID"""
        return Pet.get_by_id(pet_id)

    @blp.response(204)
    def delete(self, pet_id):
        """Delete pet"""
        Pet.delete(pet_id)

der-joel avatar Sep 22 '21 10:09 der-joel

This makes sense. I have a few other things listed for alt_response (#256, #260). We could do this as well.

Would you like to investigate any further?

lafrech avatar Sep 27 '21 21:09 lafrech

From what I can see, this feature has been added now?

Unfortunately it seems like adding alt_response on MethodView makes method-level response get ignored… Is this intended?

If not, should I file a separate issue or is this already listed (I couldn't find anything)?

Edit: in fact, it seems like all method-level annotations (eg Docstrings) get ignored when providing a class-level alt_response… Obviously that seems undesirable and a bug 😅

zedrdave avatar Aug 16 '23 09:08 zedrdave

I don't remember but from your findings, the use case is still unsupported. It just seems to work but screws everything up.

Let's keep the discussion in this thread.

lafrech avatar Aug 16 '23 10:08 lafrech

Thanks! My bad then: just assumed it had been implemented, but if it's still WIP, feel free to ignore the above comment 😉

zedrdave avatar Aug 16 '23 10:08 zedrdave

No pb. Your comment may help someone facing the same issue.

As you can see, it is not really in progress, unless someone wants to take charge.

lafrech avatar Aug 16 '23 10:08 lafrech