Skip to content

How to write a decorator for path operation? #2662

@gocreating

Description

@gocreating

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

def with_transaction(func):
    @wraps(func)
    async def wrap_func(request, *args, **kwargs):
        session = request.app.state.database.get_session()
        try:
            result = await func(request, *args, **kwargs)
            session.commit()
        except Exception as e:
            session.rollback()
        finally:
            session.close()
        return result

    return wrap_func

@router.post('/users', response_model=schemas.User)
@with_transaction
def create_user(user: schemas.UserCreate, session: Session = Depends(get_session)):
    user = User(provider=user.provider)
    session.add(user)
    if user.foobar == 'force_error':
        raise Exception('my custom exception')
    return user

Description

My requirements:

  1. commit before response validation if path operation succeeds
  2. rollback if path operation fails (raises custom exception)
  3. such feature is optional for each endpoint

After suffering from the limitation of dependencies, middlewares, and a lot of custom xxx, I finally decided to manage transactions on my own. (sigh, neither dependencies nor middlewares have complete control on request lifecycles)
It seems decorator is the last and optimal practice.
However, the sample decorator above cannot get dependencies injected...

TypeError: wrap_func() missing 1 required positional argument: 'request'

How sad and frustrating such a great framework can't even do this simple and frequently-used feature...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions