-
First Check
Commit to Help
Example Codefrom typing import Optional
import uvicorn
from fastapi import APIRouter, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Invoice(BaseModel):
id: str
title: Optional[str] = None
customer: str
total: float
class InvoiceEvent(BaseModel):
description: str
paid: bool
class InvoiceEventReceived(BaseModel):
ok: bool
class InvoiceEventReceived2(BaseModel):
okydoky: bool
invoices_callback_router = APIRouter()
@invoices_callback_router.post(
"{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
pass
invoices_callback_router2 = APIRouter()
@invoices_callback_router2.post(
"{$callback_url2}/invoices2/{$request.body.id}", response_model=InvoiceEventReceived2
)
def invoice_notification2(body: InvoiceEvent):
pass
@app.post("/invoices/", callbacks=[invoices_callback_router.routes, invoices_callback_router2.routes])
def create_invoice(invoice: Invoice):
return {"msg": "Invoice received"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)DescriptionHi I am trying to have multiple callbacks for a "callbacks": {}did I do something wrong or should I used another approach for this purpose my goal is to have something like this in documentation Operating SystemLinux Operating System DetailsNo response FastAPI Version0.75.1 Python Version3.8.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
I just add some logging line in https://github.com/tiangolo/fastapi/blob/master/fastapi/applications.py#L563 ) -> Callable[[DecoratedCallable], DecoratedCallable]:
print(f"len(callbacks) = {len(callbacks)}")
for callback in callbacks:
print(type(callback))
if isinstance(callback, list):
print(type(callback[0]))if I use @app.post("/invoices/", callbacks=invoices_callback_router.routes)then, the output will be len(callbacks) = 1
<class 'fastapi.routing.APIRoute'>however, by setting @app.post("/invoices/", callbacks=[invoices_callback_router.routes, invoices_callback_router2.routes])we will have len(callbacks) = 2
<class 'list'>
<class 'fastapi.routing.APIRoute'>
<class 'list'>
<class 'fastapi.routing.APIRoute'>so, as you can see, in the second case, it seems we have array of arrays ! |
Beta Was this translation helpful? Give feedback.
-
|
I could fix it within class FastAPI(Starlette):
...
...
...
def post(
...
...
) -> Callable[[DecoratedCallable], DecoratedCallable]:
tmp_callbacks = []
for callback in callbacks:
if isinstance(callback, list):
tmp_callbacks.append(callback[0])
else:
tmp_callbacks.append(callback)
return self.router.post(
...
callbacks=tmp_callbacks,
...I am not sure, but in my point of view, it seems this is a bug |
Beta Was this translation helpful? Give feedback.
-
|
we can also use to resolve the issue @app.post("/invoices/", callbacks=invoices_callback_router.routes + invoices_callback_router2.routes)
|
Beta Was this translation helpful? Give feedback.

we can also use to resolve the issue
@app.post("/invoices/", callbacks=invoices_callback_router.routes + invoices_callback_router2.routes)APIRouter().routesreturns alistobject, so, I think this should be the better approach