Nullable Query Parameters #8438
-
First Check
Commit to Help
Example Codefrom typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home(q: Optional[bool] = True):
return {"Hello": q}
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=5001)DescriptionI have a simple FastAPI route with an I've tried all the following but in every case it is rejected with the messager
Is there any way to pass Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.73.0 Python VersionPython 3.9.7 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
|
Did you try to not mention |
Beta Was this translation helpful? Give feedback.
-
|
Yes, in that case it provides the default value of |
Beta Was this translation helpful? Give feedback.
-
|
I must agree that your question makes sense and would make life easier if we could set the query to none instead of avoiding it. |
Beta Was this translation helpful? Give feedback.
-
|
This is not a relevant issue directly to the If you think that those values were supposed to be treated a bool false value, you could extends the from typing import Optional
from fastapi import FastAPI
from pydantic.validators import BOOL_FALSE
BOOL_FALSE.update(["none", "null", ""])
app = FastAPI()
@app.get("/")
def home(q: Optional[bool] = True):
return {"Hello": q} |
Beta Was this translation helpful? Give feedback.
-
|
Ran into this once, too. This is not really an issue with FastAPI nor Pydantic. Query string parameters are always strings, meaning everything you pass here will always end up being a string before being parsed by FastAPI or Pydantic. You could argue that for boolean values this problem is easy to evade as you only have three possible values here: true, false and null. However this would only be a fix for this particular case and would create some strange inconsistencies inside the way things work here. Lets' change this to being a @app.get("/")
def home(q: Optional[str] = "foo"):
return {"Hello": q}How could we now pass
So there is no really sane way to distinguish the different cases. Even using some special string like So after running into this myself I figured FastAPI should probably not create a special case for boolean fields - this will lead to confusion and make things harder to unserstand. My solution was using an enum btw: class NullBolleanEnum(str, Enum):
NULL = "null"
TRUE = "true"
FALSE = "false"
def to_python(self) -> Optional[bool]:
return {"null": None, "true": True, "false": False}.get(self.value)
@app.get("/")
def home(q: NullBolleanEnum = NullBolleanEnum.TRUE):
return {"Hello": q.to_python()}(not 100% my solution and just written down here without running it, but should give you an idea) |
Beta Was this translation helpful? Give feedback.
-
|
I have encountered exactly the same issue: Before upgrading, I could provide explicitly None as the optional query parameter value and it would work well. |
Beta Was this translation helpful? Give feedback.
Ran into this once, too.
This is not really an issue with FastAPI nor Pydantic. Query string parameters are always strings, meaning everything you pass here will always end up being a string before being parsed by FastAPI or Pydantic.
You could argue that for boolean values this problem is easy to evade as you only have three possible values here: true, false and null. However this would only be a fix for this particular case and would create some strange inconsistencies inside the way things work here.
Lets' change this to being a
strand see what that changes:How could we now pass
Nonehere?/: Must be the defau…