Compare commits

..

9 Commits

Author SHA1 Message Date
github-actions[bot]
63eb33ce10 🎨 Auto format 2026-06-15 13:33:02 +00:00
Yurii Motov
3b0c595c2e Fix response_model being set to None for non-generator endpoints 2026-06-15 15:31:57 +02:00
Yurii Motov
6ab384c423 Move blocks down 2026-06-15 15:30:11 +02:00
Yurii Motov
2fd2248d40 Add tests for response_model_* params with non-generator Iterable return type 2026-06-15 15:15:25 +02:00
Sebastián Ramírez
a82e5f2fac 🔖 Release version 0.137.1 (#15766)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-06-15 11:26:48 +00:00
github-actions[bot]
edd1461589 📝 Update release notes
[skip ci]
2026-06-15 11:20:24 +00:00
Sebastián Ramírez
b78c82262f 🚨 Fix typing checks for APIRoute (#15765) 2026-06-15 13:19:51 +02:00
github-actions[bot]
e0f8cadf09 📝 Update release notes
[skip ci]
2026-06-15 10:55:32 +00:00
Sebastián Ramírez
d8aad201eb 🐛 Fix bug, allow empty path in path operation in prefixless router (#15763) 2026-06-15 12:55:06 +02:00
15 changed files with 202 additions and 65 deletions

View File

@@ -7,6 +7,13 @@ hide:
## Latest Changes
## 0.137.1 (2026-06-15)
### Fixes
* 🚨 Fix typing checks for APIRoute. PR [#15765](https://github.com/fastapi/fastapi/pull/15765) by [@tiangolo](https://github.com/tiangolo).
* 🐛 Fix bug, allow empty path in path operation in prefixless router. PR [#15763](https://github.com/fastapi/fastapi/pull/15763) by [@tiangolo](https://github.com/tiangolo).
## 0.137.0 (2026-06-14)
### Breaking Changes

View File

@@ -492,7 +492,9 @@ item: Item
### Розгортання застосунку (необовʼязково) { #deploy-your-app-optional }
За бажання ви можете розгорнути ваш застосунок FastAPI у [FastAPI Cloud](https://fastapicloud.com) однією командою. 🚀
За бажання ви можете розгорнути ваш застосунок FastAPI у [FastAPI Cloud](https://fastapicloud.com), перейдіть і приєднайтеся до списку очікування, якщо ви ще цього не зробили. 🚀
Якщо у вас вже є обліковий запис **FastAPI Cloud** (ми запросили вас зі списку очікування 😉), ви можете розгорнути ваш застосунок однією командою.
<div class="termy">
@@ -508,8 +510,6 @@ Deploying to FastAPI Cloud...
</div>
CLI автоматично визначить ваш застосунок FastAPI і розгорне його в хмарі. Якщо ви не ввійшли в обліковий запис, ваш браузер відкриється для завершення процесу автентифікації.
Ось і все! Тепер ви можете отримати доступ до вашого застосунку за цією URL-адресою. ✨
#### Про FastAPI Cloud { #about-fastapi-cloud }

View File

@@ -111,7 +111,7 @@ q: str | None = None
{* ../../docs_src/body_multiple_params/tutorial004_an_py310.py hl[28] *}
/// note | Примітка
/// info | Інформація
`Body` також має всі ті самі додаткові параметри валідації та метаданих, що й `Query`, `Path` та інші, які ви побачите пізніше.
@@ -126,7 +126,7 @@ q: str | None = None
Але якщо ви хочете, щоб він очікував JSON з ключем `item`, а всередині нього - вміст моделі, як це відбувається, коли ви оголошуєте додаткові параметри тіла, ви можете використати спеціальний параметр `Body` - `embed`:
```Python
item: Annotated[Item, Body(embed=True)]
item: Item = Body(embed=True)
```
як у прикладі:

View File

@@ -8,7 +8,7 @@
Щоб оголосити тіло **запиту**, ви використовуєте [Pydantic](https://docs.pydantic.dev/) моделі з усією їх потужністю та перевагами.
/// note | Примітка
/// info | Інформація
Щоб надіслати дані, ви повинні використовувати один із: `POST` (більш поширений), `PUT`, `DELETE` або `PATCH`.

View File

@@ -24,13 +24,13 @@
///
/// note
/// info
Для визначення кукі ви маєте використовувати `Cookie`, тому що в іншому випадку параметри будуть інтерпретовані як параметри запиту.
///
/// note
/// info
Майте на увазі, що оскільки **браузери обробляють кукі** спеціальним чином і за лаштунками, вони **не** дозволяють **JavaScript** легко взаємодіяти з ними.

View File

@@ -72,13 +72,13 @@ FastAPI підтримує це так само, як і зі звичайним
{* ../../docs_src/path_operation_configuration/tutorial005_py310.py hl[18] *}
/// note | Примітка
/// info | Інформація
Зверніть увагу, що `response_description` стосується саме відповіді, а `description` стосується «операції шляху» загалом.
///
/// tip | Порада
/// check | Перевірте
OpenAPI визначає, що кожна «операція шляху» потребує опису відповіді.

View File

@@ -8,7 +8,7 @@
{* ../../docs_src/path_params_numeric_validations/tutorial001_an_py310.py hl[1,3] *}
/// note | Примітка
/// info | Інформація
FastAPI додав підтримку `Annotated` (і почав рекомендувати його використання) у версії 0.95.0.
@@ -131,7 +131,7 @@ Python нічого не зробить із цією `*`, але розпізн
* `lt`: `l`ess `t`han
* `le`: `l`ess than or `e`qual
/// note | Примітка
/// info | Інформація
`Query`, `Path` та інші класи, які ви побачите пізніше, є підкласами спільного класу `Param`.

View File

@@ -2,7 +2,7 @@
Ви можете визначити файли, які будуть завантажуватися клієнтом, використовуючи `File`.
/// note | Примітка
/// info | Інформація
Щоб отримувати завантажені файли, спочатку встановіть [`python-multipart`](https://github.com/Kludex/python-multipart).
@@ -28,7 +28,7 @@ $ pip install python-multipart
{* ../../docs_src/request_files/tutorial001_an_py310.py hl[9] *}
/// note | Примітка
/// info | Інформація
`File` — це клас, який безпосередньо успадковує `Form`.

View File

@@ -2,7 +2,7 @@
Ви можете одночасно визначати файли та поля форми, використовуючи `File` і `Form`.
/// note | Примітка
/// info | Інформація
Щоб отримувати завантажені файли та/або дані форми, спочатку встановіть [`python-multipart`](https://github.com/Kludex/python-multipart).

View File

@@ -18,7 +18,7 @@
Параметр `status_code` приймає число з HTTP кодом статусу.
/// note | Примітка
/// info | Інформація
`status_code` також може, як альтернативу, приймати `IntEnum`, наприклад, Python [`http.HTTPStatus`](https://docs.python.org/3/library/http.html#http.HTTPStatus).

View File

@@ -8,7 +8,7 @@
## Використання `TestClient` { #using-testclient }
/// note | Примітка
/// info | Інформація
Щоб використовувати `TestClient`, спочатку встановіть [`httpx`](https://www.python-httpx.org).
@@ -144,7 +144,7 @@ $ pip install httpx
Докладніше про передачу даних у бекенд (за допомогою `httpx` або `TestClient`) можна знайти в [документації HTTPX](https://www.python-httpx.org).
/// note | Примітка
/// info | Інформація
Зверніть увагу, що `TestClient` отримує дані, які можна конвертувати в JSON, а не Pydantic-моделі.

View File

@@ -1,6 +1,6 @@
"""FastAPI framework, high performance, easy to learn, fast to code, ready for production"""
__version__ = "0.137.0"
__version__ = "0.137.1"
from starlette import status as status

View File

@@ -930,28 +930,6 @@ def _populate_api_route_state(
route.path = path
route.endpoint = endpoint
route.stream_item_type = None
if isinstance(response_model, DefaultPlaceholder):
return_annotation = get_typed_return_annotation(endpoint)
if lenient_issubclass(return_annotation, Response):
response_model = None
else:
stream_item = get_stream_item_type(return_annotation)
if stream_item is not None:
# Extract item type for JSONL or SSE streaming when
# response_class is DefaultPlaceholder (JSONL) or
# EventSourceResponse (SSE).
# ServerSentEvent is excluded: it's a transport
# wrapper, not a data model, so it shouldn't feed
# into validation or OpenAPI schema generation.
if (
isinstance(response_class, DefaultPlaceholder)
or lenient_issubclass(response_class, EventSourceResponse)
) and not lenient_issubclass(stream_item, ServerSentEvent):
route.stream_item_type = stream_item
response_model = None
else:
response_model = return_annotation
route.response_model = response_model
route.summary = summary
route.response_description = response_description
route.deprecated = deprecated
@@ -987,27 +965,6 @@ def _populate_api_route_state(
if isinstance(status_code, IntEnum):
status_code = int(status_code)
route.status_code = status_code
if route.response_model:
assert is_body_allowed_for_status_code(status_code), (
f"Status code {status_code} must not have a response body"
)
response_name = "Response_" + route.unique_id
route.response_field = create_model_field(
name=response_name,
type_=route.response_model,
mode="serialization",
)
else:
route.response_field = None
if route.stream_item_type:
stream_item_name = "StreamItem_" + route.unique_id
route.stream_item_field = create_model_field(
name=stream_item_name,
type_=route.stream_item_type,
mode="serialization",
)
else:
route.stream_item_field = None
route.dependencies = list(dependencies or [])
route.description = description or inspect.cleandoc(route.endpoint.__doc__ or "")
# if a "form feed" character (page break) is found in the description text,
@@ -1059,9 +1016,88 @@ def _populate_api_route_state(
route.is_json_stream = is_generator and isinstance(
response_class, DefaultPlaceholder
)
if isinstance(response_model, DefaultPlaceholder):
return_annotation = get_typed_return_annotation(endpoint)
if lenient_issubclass(return_annotation, Response):
response_model = None
else:
stream_item = get_stream_item_type(return_annotation)
if stream_item is not None and is_generator:
# Extract item type for JSONL or SSE streaming for
# generator endpoints when response_class is
# DefaultPlaceholder (JSONL) or EventSourceResponse
# (SSE).
# ServerSentEvent is excluded: it's a transport
# wrapper, not a data model, so it shouldn't feed
# into validation or OpenAPI schema generation.
if (
isinstance(response_class, DefaultPlaceholder)
or lenient_issubclass(response_class, EventSourceResponse)
) and not lenient_issubclass(stream_item, ServerSentEvent):
route.stream_item_type = stream_item
response_model = None
else:
response_model = return_annotation
route.response_model = response_model
if route.response_model:
assert is_body_allowed_for_status_code(status_code), (
f"Status code {status_code} must not have a response body"
)
response_name = "Response_" + route.unique_id
route.response_field = create_model_field(
name=response_name,
type_=route.response_model,
mode="serialization",
)
else:
route.response_field = None
if route.stream_item_type:
stream_item_name = "StreamItem_" + route.unique_id
route.stream_item_field = create_model_field(
name=stream_item_name,
type_=route.stream_item_type,
mode="serialization",
)
else:
route.stream_item_field = None
class APIRoute(routing.Route):
stream_item_type: Any | None
response_model: Any
summary: str | None
response_description: str
deprecated: bool | None
operation_id: str | None
response_model_include: IncEx | None
response_model_exclude: IncEx | None
response_model_by_alias: bool
response_model_exclude_unset: bool
response_model_exclude_defaults: bool
response_model_exclude_none: bool
include_in_schema: bool
response_class: type[Response] | DefaultPlaceholder
dependency_overrides_provider: Any | None
callbacks: list[BaseRoute] | None
openapi_extra: dict[str, Any] | None
generate_unique_id_function: Callable[[Any], str] | DefaultPlaceholder
strict_content_type: bool | DefaultPlaceholder
tags: list[str | Enum]
responses: dict[int | str, dict[str, Any]]
unique_id: str
status_code: int | None
response_field: ModelField | None
stream_item_field: ModelField | None
dependencies: list[params.Depends]
description: str
response_fields: dict[int | str, ModelField]
dependant: Dependant
_flat_dependant: Dependant
_embed_body_fields: bool
body_field: ModelField | None
is_sse_stream: bool
is_json_stream: bool
def __init__(
self,
path: str,
@@ -2435,9 +2471,16 @@ class APIRouter(routing.Router):
"A path prefix must not end with '/', as the routes will start with '/'"
)
else:
for r in _iter_included_route_candidates(router.routes):
path = getattr(r, "path", None)
name = getattr(r, "name", "unknown")
for route, route_context in _iter_routes_with_context(router.routes):
if route_context is None:
path = getattr(route, "path", None)
name = getattr(route, "name", "unknown")
elif route_context.starlette_route is not None:
path = getattr(route_context.starlette_route, "path", None)
name = getattr(route_context.starlette_route, "name", "unknown")
else:
path = route_context.path
name = route_context.name
if path is not None and not path:
raise FastAPIError(
f"Prefix and path cannot be both empty (path operation: {name})"

View File

@@ -2,6 +2,7 @@ from typing import Annotated, cast
import pytest
from fastapi import APIRouter, Body, Depends, FastAPI, Request
from fastapi.exceptions import FastAPIError
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse
from fastapi.routing import (
APIRoute,
@@ -807,6 +808,60 @@ def test_no_prefix_include_validation_sees_effective_starlette_route_candidates(
assert cast(Route, candidates[0]).path == "/child/items"
def test_no_prefix_include_validation_sees_effective_api_route_path():
leaf_router = APIRouter()
@leaf_router.get("")
def read_items():
return []
parent_router = APIRouter()
parent_router.include_router(leaf_router, prefix="/items")
# for coverage
candidates = list(_iter_included_route_candidates(parent_router.routes))
assert cast(APIRoute, candidates[0]).path == ""
app = FastAPI()
app.include_router(parent_router)
client = TestClient(app)
response = client.get("/items")
assert response.status_code == 200, response.text
assert response.json() == []
def test_no_prefix_include_validation_sees_effective_starlette_route_path():
def endpoint(request):
return PlainTextResponse("ok")
child_router = APIRouter(routes=[Route("/items", endpoint, name="read_items")])
parent_router = APIRouter()
parent_router.include_router(child_router, prefix="/child")
app = FastAPI()
app.include_router(parent_router)
client = TestClient(app)
response = client.get("/child/items")
assert response.status_code == 200, response.text
assert response.text == "ok"
def test_no_prefix_include_validation_rejects_empty_effective_api_route_path():
router = APIRouter()
@router.get("")
def read_items(): # pragma: no cover
return []
app = FastAPI()
with pytest.raises(FastAPIError):
app.include_router(router)
def test_apirouter_matches_fallback_without_include_context():
router = APIRouter()

View File

@@ -1,3 +1,5 @@
from collections.abc import Iterable
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
@@ -65,6 +67,21 @@ def get_exclude_unset_none() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
@app.get("/iterable_exclude_unset", response_model_exclude_unset=True)
def get_iterable_exclude_unset() -> Iterable[ModelDefaults]:
return [ModelDefaults(x=None, y="y")]
@app.get("/iterable_exclude_defaults", response_model_exclude_defaults=True)
def get_iterable_exclude_defaults() -> Iterable[ModelDefaults]:
return [ModelDefaults(x=None, y="y")]
@app.get("/iterable_exclude_none", response_model_exclude_none=True)
def get_iterable_exclude_none() -> Iterable[ModelDefaults]:
return [ModelDefaults(x=None, y="y")]
client = TestClient(app)
@@ -91,3 +108,18 @@ def test_return_exclude_none():
def test_return_exclude_unset_none():
response = client.get("/exclude_unset_none")
assert response.json() == {"y": "y"}
def test_return_iterable_exclude_unset():
response = client.get("/iterable_exclude_unset")
assert response.json() == [{"x": None, "y": "y"}]
def test_return_iterable_exclude_defaults():
response = client.get("/iterable_exclude_defaults")
assert response.json() == [{}]
def test_return_iterable_exclude_none():
response = client.get("/iterable_exclude_none")
assert response.json() == [{"y": "y", "z": "z"}]