diff --git a/docs/en/docs/tutorial/body.md b/docs/en/docs/tutorial/body.md index 22fdf90dc..f7c2ae304 100644 --- a/docs/en/docs/tutorial/body.md +++ b/docs/en/docs/tutorial/body.md @@ -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: diff --git a/docs_src/body/tutorial005_py310.py b/docs_src/body/tutorial005_py310.py index 7a9c9c9d9..073cde6be 100644 --- a/docs_src/body/tutorial005_py310.py +++ b/docs_src/body/tutorial005_py310.py @@ -5,7 +5,7 @@ app = FastAPI() class Item(BaseModel): - description: str | None + description: str | None = None @app.post("/items/") diff --git a/tests/test_tutorial/test_body_required_nullable/test_tutorial001.py b/tests/test_tutorial/test_body/test_tutorial005.py similarity index 77% rename from tests/test_tutorial/test_body_required_nullable/test_tutorial001.py rename to tests/test_tutorial/test_body/test_tutorial005.py index ed1268866..8e12c0462 100644 --- a/tests/test_tutorial/test_body_required_nullable/test_tutorial001.py +++ b/tests/test_tutorial/test_body/test_tutorial005.py @@ -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} + diff --git a/tests/test_tutorial/test_body_required_nullable/__init__.py b/tests/test_tutorial/test_body_required_nullable/__init__.py deleted file mode 100644 index e69de29bb..000000000