mirror of
https://github.com/fastapi/fastapi.git
synced 2026-06-15 11:01:13 -04:00
Compare commits
5 Commits
translate-
...
0.137.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a82e5f2fac | ||
|
|
edd1461589 | ||
|
|
b78c82262f | ||
|
|
e0f8cadf09 | ||
|
|
d8aad201eb |
@@ -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
|
||||
|
||||
@@ -492,7 +492,9 @@ item: Item
|
||||
|
||||
### アプリをデプロイ(任意) { #deploy-your-app-optional }
|
||||
|
||||
1 コマンドで FastAPI アプリを [FastAPI Cloud](https://fastapicloud.com) にデプロイできます。 🚀
|
||||
必要に応じて FastAPI アプリを [FastAPI Cloud](https://fastapicloud.com) にデプロイできます。まだの場合はウェイティングリストに参加してください。 🚀
|
||||
|
||||
すでに **FastAPI Cloud** アカウント(ウェイティングリストから招待されました 😉)がある場合は、1 コマンドでアプリケーションをデプロイできます。
|
||||
|
||||
<div class="termy">
|
||||
|
||||
@@ -508,8 +510,6 @@ Deploying to FastAPI Cloud...
|
||||
|
||||
</div>
|
||||
|
||||
CLI は自動的に FastAPI アプリケーションを検出し、クラウドへデプロイします。ログインしていない場合は、認証を完了するためにブラウザが開きます。
|
||||
|
||||
これで完了です!その URL でアプリにアクセスできます。 ✨
|
||||
|
||||
#### FastAPI Cloud について { #about-fastapi-cloud }
|
||||
|
||||
@@ -110,7 +110,7 @@ q: str | None = None
|
||||
|
||||
{* ../../docs_src/body_multiple_params/tutorial004_an_py310.py hl[28] *}
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
`Body`もまた、後述する `Query` や `Path` などと同様に、すべての追加検証パラメータとメタデータパラメータを持っています。
|
||||
|
||||
@@ -125,7 +125,7 @@ Pydanticモデル`Item`の単一の`item`ボディパラメータしかないと
|
||||
しかし、追加のボディパラメータを宣言したときのように、キー `item` を持つ JSON と、その中のモデル内容を期待したい場合は、特別な `Body` パラメータ `embed` を使うことができます:
|
||||
|
||||
```Python
|
||||
item: Annotated[Item, Body(embed=True)]
|
||||
item: Item = Body(embed=True)
|
||||
```
|
||||
|
||||
以下において:
|
||||
|
||||
@@ -8,7 +8,7 @@ APIはほとんどの場合 **レスポンス** ボディを送信する必要
|
||||
|
||||
**リクエスト**ボディを宣言するには、[Pydantic](https://docs.pydantic.dev/) モデルを使用し、その強力な機能とメリットをすべて利用します。
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
データを送信するには、`POST`(より一般的)、`PUT`、`DELETE`、`PATCH` のいずれかを使用すべきです。
|
||||
|
||||
|
||||
@@ -24,13 +24,13 @@
|
||||
|
||||
///
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
クッキーを宣言するには、`Cookie`を使う必要があります。なぜなら、そうしないとパラメータがクエリのパラメータとして解釈されてしまうからです。
|
||||
|
||||
///
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
**ブラウザがクッキーを**特殊な方法で裏側で扱うため、**JavaScript** から簡単には触れられないことを念頭に置いてください。
|
||||
|
||||
|
||||
@@ -72,13 +72,13 @@ docstringに[Markdown](https://en.wikipedia.org/wiki/Markdown)を記述すれば
|
||||
|
||||
{* ../../docs_src/path_operation_configuration/tutorial005_py310.py hl[18] *}
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
`response_description`は具体的にレスポンスを参照し、`description`は*path operation*全般を参照していることに注意してください。
|
||||
|
||||
///
|
||||
|
||||
/// tip | 豆知識
|
||||
/// check | 確認
|
||||
|
||||
OpenAPIは*path operation*ごとにレスポンスの説明を必要としています。
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
{* ../../docs_src/path_params_numeric_validations/tutorial001_an_py310.py hl[1,3] *}
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
FastAPI はバージョン 0.95.0 で`Annotated`のサポートを追加し(そして推奨し始めました)。
|
||||
|
||||
@@ -131,7 +131,7 @@ Pythonはその`*`で何かをすることはありませんが、それ以降
|
||||
* `lt`: `l`ess `t`han
|
||||
* `le`: `l`ess than or `e`qual
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
`Query`、`Path`、および後で見る他のクラスは、共通の`Param`クラスのサブクラスです。
|
||||
|
||||
|
||||
@@ -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` を直接継承したクラスです。
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
`File`と`Form`を同時に使うことでファイルとフォームフィールドを定義することができます。
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
アップロードされたファイルやフォームデータを受信するには、まず[`python-multipart`](https://github.com/Kludex/python-multipart)をインストールします。
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
`status_code`パラメータはHTTPステータスコードを含む数値を受け取ります。
|
||||
|
||||
/// note | 備考
|
||||
/// info | 情報
|
||||
|
||||
`status_code`は代わりに、Pythonの[`http.HTTPStatus`](https://docs.python.org/3/library/http.html#http.HTTPStatus)のように、`IntEnum`を受け取ることもできます。
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
## `TestClient` を使用 { #using-testclient }
|
||||
|
||||
/// note | 備考
|
||||
/// info
|
||||
|
||||
`TestClient` を使用するには、まず [`httpx`](https://www.python-httpx.org) をインストールします。
|
||||
|
||||
@@ -32,7 +32,7 @@ $ pip install httpx
|
||||
|
||||
{* ../../docs_src/app_testing/tutorial001_py310.py hl[2,12,15:18] *}
|
||||
|
||||
/// tip | 豆知識
|
||||
/// tip
|
||||
|
||||
テスト関数は `async def` ではなく、通常の `def` であることに注意してください。
|
||||
|
||||
@@ -50,7 +50,7 @@ $ pip install httpx
|
||||
|
||||
///
|
||||
|
||||
/// tip | 豆知識
|
||||
/// tip
|
||||
|
||||
FastAPIアプリケーションへのリクエストの送信とは別に、テストで `async` 関数 (非同期データベース関数など) を呼び出したい場合は、高度なチュートリアルの[Async Tests](../advanced/async-tests.md) を参照してください。
|
||||
|
||||
@@ -144,7 +144,7 @@ FastAPIアプリケーションへのリクエストの送信とは別に、テ
|
||||
|
||||
(`httpx` または `TestClient` を使用して) バックエンドにデータを渡す方法の詳細は、[HTTPXのドキュメント](https://www.python-httpx.org)を確認してください。
|
||||
|
||||
/// note | 備考
|
||||
/// info
|
||||
|
||||
`TestClient` は、Pydanticモデルではなく、JSONに変換できるデータを受け取ることに注意してください。
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -1062,6 +1062,41 @@ def _populate_api_route_state(
|
||||
|
||||
|
||||
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 +2470,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})"
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user