i have a ressource and want to have a post api endpoint to modify it. My problem is if i set all propertys Optional[...] how did i know if i want to "delete" one property or set it to null? If i set it in the request to null: I get NoneType. But if i don't set it in the request i also get NoneType. Is there a solution to differ between this cases?
Here is an example program:
from typing import Optional
from fastapi import FastAPI
import uvicorn
from pydantic import BaseModel
class TestEntity(BaseModel):
first: Optional[str]
second: Optional[str]
third: Optional[str]
app = FastAPI()
@app.post("/test")
def test(entity: TestEntity):
return entity
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5000)
I want to set first to null and don't do anything with the other propertys, I do:
{
"first":null
}
via POST request. As response I get:
{
"first": null,
"second": null,
"third": null
}
As you can see you cannot know which property is set null and which propertys should remain the same.