19
README.md
19
README.md
@@ -123,8 +123,8 @@ from executing the tasks.
|
||||
- If the route is defined `async` then it's called regularly via `await`
|
||||
and FastAPI trusts you to do only non-blocking I/O operations.
|
||||
|
||||
The caveat is if you fail that trust and execute blocking operations within async routes,
|
||||
the event loop will not be able to run the next tasks until that blocking operation is done.
|
||||
The caveat is that if you violate that trust and execute blocking operations within async routes,
|
||||
the event loop will not be able to run subsequent tasks until the blocking operation completes.
|
||||
```python
|
||||
import asyncio
|
||||
import time
|
||||
@@ -346,7 +346,7 @@ If we didn't put data validation to dependency, we would have to validate `post_
|
||||
for every endpoint and write the same tests for each of them.
|
||||
|
||||
### Chain Dependencies
|
||||
Dependencies can use other dependencies and avoid code repetition for the similar logic.
|
||||
Dependencies can use other dependencies and avoid code repetition for similar logic.
|
||||
```python
|
||||
# dependencies.py
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
@@ -474,7 +474,7 @@ Developing RESTful API makes it easier to reuse dependencies in routes like thes
|
||||
2. `GET /courses/:course_id/chapters/:chapter_id/lessons`
|
||||
3. `GET /chapters/:chapter_id`
|
||||
|
||||
The only caveat is to use the same variable names in the path:
|
||||
The only caveat is having to use the same variable names in the path:
|
||||
- If you have two endpoints `GET /profiles/:profile_id` and `GET /creators/:creator_id`
|
||||
that both validate whether the given `profile_id` exists, but `GET /creators/:creator_id`
|
||||
also checks if the profile is creator, then it's better to rename `creator_id` path variable to `profile_id` and chain those two dependencies.
|
||||
@@ -510,11 +510,16 @@ async def get_user_profile_by_id(
|
||||
|
||||
```
|
||||
### FastAPI response serialization
|
||||
If you think you can return Pydantic object that matches your route's `response_model` to make some optimizations,
|
||||
then it's wrong.
|
||||
You may think you can return Pydantic object that matches your route's `response_model` to make some optimizations,
|
||||
but you'd be wrong.
|
||||
|
||||
FastAPI firstly converts that pydantic object to dict with its `jsonable_encoder`, then validates
|
||||
FastAPI first converts that pydantic object to dict with its `jsonable_encoder`, then validates
|
||||
data with your `response_model`, and only then serializes your object to JSON.
|
||||
|
||||
This means your Pydantic model object is created twice:
|
||||
- First, when you explicitly create it to return from your route.
|
||||
- Second, implicitly by FastAPI to validate the response data according to the response_model.
|
||||
|
||||
```python
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel, root_validator
|
||||
|
||||
Reference in New Issue
Block a user