mirror of https://github.com/tiangolo/fastapi.git
docs: update required but nullable request body example, remove py39 files, add test_tutorial005
This commit is contained in:
parent
2f17a7a79a
commit
a3aa8583d8
|
|
@ -62,15 +62,11 @@ In Python type hints, a parameter can be **required** and still allow the value
|
|||
This means that the field must be present in the request body, but its value can be `null`
|
||||
(`None` in Python).
|
||||
|
||||
This typically happens when you use `Optional[T]` **without** providing a default value.
|
||||
This typically happens when you use something like `str | None` (or `Optional[str]`) **without** providing a default value.
|
||||
|
||||
For example:
|
||||
|
||||
{* ../../docs_src/body/tutorial005_py39.py hl[6] *}
|
||||
|
||||
And for Python 3.10+:
|
||||
|
||||
{* ../../docs_src/body/tutorial005_py310.py hl[6] *}
|
||||
{* ../../docs_src/body/tutorial005_py310.py hl[8] *}
|
||||
|
||||
In this example:
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
class Item(BaseModel):
|
||||
description: str | None
|
||||
description: str | None = None
|
||||
|
||||
|
||||
@app.post("/items/")
|
||||
|
|
|
|||
|
|
@ -3,16 +3,15 @@ import importlib
|
|||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py310
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="client",
|
||||
params=[
|
||||
pytest.param("tutorial005_py39"),
|
||||
pytest.param("tutorial005_py310", marks=needs_py310),
|
||||
pytest.param("tutorial005_py310"),
|
||||
],
|
||||
)
|
||||
|
||||
def get_client(request: pytest.FixtureRequest):
|
||||
mod = importlib.import_module(f"docs_src.body.{request.param}")
|
||||
client = TestClient(mod.app)
|
||||
|
|
@ -27,4 +26,6 @@ def test_required_nullable_field(client: TestClient):
|
|||
|
||||
def test_required_field_missing(client: TestClient):
|
||||
response = client.post("/items/", json={})
|
||||
assert response.status_code == 422
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"description": None}
|
||||
|
||||
Loading…
Reference in New Issue