Skip to content

Commit 447df1e

Browse files
committed
doc: add troubleshooting section about using fastapi routers
1 parent 720e154 commit 447df1e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This FastAPI plugin allow you to automatically format any errors as Problem deta
1616
- [2. Custom error handlers](#2-custom-error-handlers)
1717
- [Wrapping up](#wrapping-up)
1818
- [Documenting your custom problems details](#documenting-your-custom-problems-details)
19+
- [Troubleshooting](#troubleshooting)
20+
- [Problem "default" openapi response is not added into additional FastAPI routers routes](#problem-default-openapi-response-is-not-added-into-additional-fastapi-routers-routes)
1921

2022
## Getting Started
2123

@@ -528,3 +530,26 @@ def get_user(user_id: str) -> Any: # noqa: ANN401
528530
Note that this has limitation. Indeed, the `UserNotFoundProblem` class just act as a model schema for openapi documentation. You actually not instantiate this class and no validation is performed when returning the problem response. It means that the error handler can returns something which does not match a `UserNotFoundProblem`.
529531

530532
This is because of the way FastAPI manages errors. At the moment, there is no way to register error handler and its response schema in the same place and there is no mechanism to ensure both are synced.
533+
534+
## Troubleshooting
535+
536+
### Problem "default" openapi response is not added into additional FastAPI routers routes
537+
538+
If you use `APIRouter` from FastAPI to bind your routes and then include routers in your main API you must initializes the problem details error handlers BEFORE including the routers if you want your routes to have their OpenAPI responses documented with the `default` problem details response.
539+
540+
```python
541+
from fastapi import APIRouter, FastAPI
542+
543+
import fastapi_problem_details as problem
544+
545+
app = FastAPI()
546+
v1 = APIRouter(prefix="/v1")
547+
548+
# THIS DOES NOT WORK
549+
app.include_router(v1)
550+
problem.init_app(app)
551+
552+
# Instead, init problem errors handlers first
553+
problem.init_app(app)
554+
app.include_router(v1)
555+
```

0 commit comments

Comments
 (0)