Issue 1:
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.

Commit to Help

I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI

app = FastAPI()

@app.post("/items:queue")
def queue_new_item(item: dict):
return "An item has been queued for later inclusion."

Description

Open the browser and POST /items:queue

For example, go to http://127.0.0.1:8000/docs#/default/queue_new_item_items_queue_post

Click to Try it out

Click to Execute (default request body of {} is fine)

I expect the string response "An item has been queued for later inclusion."

I get a 404 not found response, with JSON body {"detail": "Not Found"}

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.78.0

Python Version

Python 3.9.12

Additional Context

No response

Issue 2:
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:

commit before response validation if path operation succeeds

rollback if path operation fails (raises custom exception)

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...