Browse Source

docs: update required but nullable request body example, remove py39 files, add test_tutorial005

pull/14751/head
Alberto Zambrano 5 months ago
parent
commit
a3aa8583d8
  1. 8
      docs/en/docs/tutorial/body.md
  2. 2
      docs_src/body/tutorial005_py310.py
  3. 9
      tests/test_tutorial/test_body/test_tutorial005.py
  4. 0
      tests/test_tutorial/test_body_required_nullable/__init__.py

8
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` This means that the field must be present in the request body, but its value can be `null`
(`None` in Python). (`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: For example:
{* ../../docs_src/body/tutorial005_py39.py hl[6] *} {* ../../docs_src/body/tutorial005_py310.py hl[8] *}
And for Python 3.10+:
{* ../../docs_src/body/tutorial005_py310.py hl[6] *}
In this example: In this example:

2
docs_src/body/tutorial005_py310.py

@ -5,7 +5,7 @@ app = FastAPI()
class Item(BaseModel): class Item(BaseModel):
description: str | None description: str | None = None
@app.post("/items/") @app.post("/items/")

9
tests/test_tutorial/test_body_required_nullable/test_tutorial001.py → tests/test_tutorial/test_body/test_tutorial005.py

@ -3,16 +3,15 @@ import importlib
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from ...utils import needs_py310
@pytest.fixture( @pytest.fixture(
name="client", name="client",
params=[ params=[
pytest.param("tutorial005_py39"), pytest.param("tutorial005_py310"),
pytest.param("tutorial005_py310", marks=needs_py310),
], ],
) )
def get_client(request: pytest.FixtureRequest): def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body.{request.param}") mod = importlib.import_module(f"docs_src.body.{request.param}")
client = TestClient(mod.app) client = TestClient(mod.app)
@ -27,4 +26,6 @@ def test_required_nullable_field(client: TestClient):
def test_required_field_missing(client: TestClient): def test_required_field_missing(client: TestClient):
response = client.post("/items/", json={}) response = client.post("/items/", json={})
assert response.status_code == 422 assert response.status_code == 200
assert response.json() == {"description": None}

0
tests/test_tutorial/test_body_required_nullable/__init__.py

Loading…
Cancel
Save