mirror of https://github.com/tiangolo/fastapi.git
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
This commit is contained in:
parent
6eb348433f
commit
a28557e0c1
|
|
@ -257,7 +257,7 @@ For example, you might try:
|
|||
q: Annotated[str | None, Query(min_length=3)] = ...
|
||||
```
|
||||
|
||||
But this will still expect a **string** value, and if the client omits `q` or tries to send `q=None`, FastAPI will raise a validation error.
|
||||
But this will still expect a **string** value, and if the client omits `q` or tries to send `q=None`, FastAPI will raise a validation error.
|
||||
In other words, `None` is not an acceptable runtime value for query parameters — only strings are.
|
||||
|
||||
If you want to accept special values (like `"None"` or an empty string) and interpret them as `None` in your application, you can handle them manually in your function:
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
from fastapi import FastAPI, Query
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Annotated[str | None, Query()] = ...):
|
||||
if q in ("None", "", "null"):
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.query_params_str_validations.tutorial006d_an_py310 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_read_items():
|
||||
response = client.get("/items/", params={"q": "None"})
|
||||
assert response.status_code == 200
|
||||
|
|
|
|||
Loading…
Reference in New Issue