Assuming the following:
class User(BaseModel):
name : str
bio : Optional[str]
class UpdateUser(BaseModel):
name = Optional[str] = Field(None)
bio = Optional[str] = Field(None)
@app.put("/users/{id}", response_model=User)
async def put_user(pk:int, payload:UpdateUser) -> User:
...
If omitted fields are simply defaulted to None in payload
, how can I tell the difference between a client omitting the field (because they don't want to update it) vs explicitly setting the field to None
?
Restated a different way: How do I implement an API that doesn't require every field to be specified on an update, but is still able to differentiate between omitted fields and an explicit update of a field to None
(when appropriate)?