mirror of https://github.com/tiangolo/fastapi.git
Rename code example file and add variants
This commit is contained in:
parent
96d16535c8
commit
48a876d54a
|
|
@ -276,7 +276,7 @@ In other words, `None` is not an acceptable runtime value for query parameters
|
|||
|
||||
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:
|
||||
|
||||
{* ../../docs_src/query_params_str_validations/tutorial006d_an_py310.py hl[10:13,18] *}
|
||||
{* ../../docs_src/query_params_str_validations/tutorial006c_an_py310.py hl[9:12,17] *}
|
||||
|
||||
/// note
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Union[str, None] = Query(min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
|
|
@ -1,14 +1,20 @@
|
|||
from typing import Union
|
||||
from typing import Optional, Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
from pydantic import BeforeValidator
|
||||
from typing_extensions import Annotated
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def nullable_str(val: str) -> Union[str, None]:
|
||||
if val in ("None", "", "null"):
|
||||
return None
|
||||
return val
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Annotated[Union[str, None], Query(min_length=3)]):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
async def read_items(
|
||||
q: Annotated[Optional[str], Query(min_length=3), BeforeValidator(nullable_str)],
|
||||
):
|
||||
return {"q": q}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,19 @@
|
|||
from typing import Annotated
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
from pydantic import BeforeValidator
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def nullable_str(val: str) -> str | None:
|
||||
if val in ("None", "", "null"):
|
||||
return None
|
||||
return val
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Annotated[str | None, Query(min_length=3)]):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
async def read_items(
|
||||
q: Annotated[str | None, Query(min_length=3), BeforeValidator(nullable_str)],
|
||||
):
|
||||
return {"q": q}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,19 @@
|
|||
from typing import Annotated, Union
|
||||
from typing import Annotated, Optional, Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
from pydantic import BeforeValidator
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def nullable_str(val: str) -> Union[str, None]:
|
||||
if val in ("None", "", "null"):
|
||||
return None
|
||||
return val
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Annotated[Union[str, None], Query(min_length=3)]):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
async def read_items(
|
||||
q: Annotated[Optional[str], Query(min_length=3), BeforeValidator(nullable_str)],
|
||||
):
|
||||
return {"q": q}
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
from typing import Optional, Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
from pydantic import BeforeValidator
|
||||
from typing_extensions import Annotated
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def nullable_str(val: str) -> Union[str, None]:
|
||||
if val in ("None", "", "null"):
|
||||
return None
|
||||
return val
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Annotated[Optional[str], Query(min_length=3), BeforeValidator(nullable_str)],
|
||||
):
|
||||
return {"q": q}
|
||||
|
|
@ -1,14 +1,26 @@
|
|||
import importlib
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from tests.utils import needs_pydanticv2
|
||||
|
||||
from ...utils import needs_py39, needs_py310
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
from docs_src.query_params_str_validations.tutorial006d_an_py310 import app
|
||||
|
||||
yield TestClient(app)
|
||||
@pytest.fixture(
|
||||
name="client",
|
||||
params=[
|
||||
"tutorial006c_an",
|
||||
pytest.param("tutorial006c_an_py310", marks=needs_py310),
|
||||
pytest.param("tutorial006c_an_py39", marks=needs_py39),
|
||||
],
|
||||
)
|
||||
def get_client(request: pytest.FixtureRequest):
|
||||
mod = importlib.import_module(
|
||||
f"docs_src.query_params_str_validations.{request.param}"
|
||||
)
|
||||
yield TestClient(mod.app)
|
||||
|
||||
|
||||
@needs_pydanticv2
|
||||
Loading…
Reference in New Issue