marshmallow-code / marshmallow-code/flask-smorest

Allow alt_response on MethodView

Open
#280 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
717
Forks
77
Avg merge
7h 49m
Merged PRs (30d)
3

Description

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:
```python3

@blp.route('/')
@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)
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the alt_response and route decorators are applied to a Flask MethodView and its endpoint methods. Check the provided PetsById example, including the custom converter that raises 404, and determine how one class-level alt_response should affect every endpoint. Done means the class-level usage produces the intended documented 404 response for each endpoint.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.