|
|
|
@ -1,5 +1,6 @@ |
|
|
|
from __future__ import annotations |
|
|
|
|
|
|
|
from dataclasses import dataclass |
|
|
|
from typing import TYPE_CHECKING, Annotated |
|
|
|
|
|
|
|
import pytest |
|
|
|
@ -27,6 +28,25 @@ async def get_client() -> AsyncGenerator[DummyClient, None]: |
|
|
|
|
|
|
|
Client = Annotated[DummyClient, Depends(get_client)] |
|
|
|
|
|
|
|
app_with_late_forward_ref = FastAPI() |
|
|
|
|
|
|
|
|
|
|
|
def get_potato() -> Potato: |
|
|
|
return Potato(color="red", size=10) |
|
|
|
|
|
|
|
|
|
|
|
@app_with_late_forward_ref.get("/") |
|
|
|
async def get_late_forward_ref( |
|
|
|
potato: Annotated[Potato, Depends(get_potato)], |
|
|
|
) -> dict[str, str]: |
|
|
|
return {"color": potato.color} |
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
|
class Potato: |
|
|
|
color: str |
|
|
|
size: int |
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="client") |
|
|
|
def client_fixture() -> TestClient: |
|
|
|
@ -77,3 +97,43 @@ def test_openapi_schema(client: TestClient): |
|
|
|
}, |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def test_late_forward_ref_dependency(): |
|
|
|
client = TestClient(app_with_late_forward_ref) |
|
|
|
|
|
|
|
response = client.get("/") |
|
|
|
|
|
|
|
assert response.status_code == 200, response.text |
|
|
|
assert response.json() == {"color": "red"} |
|
|
|
|
|
|
|
|
|
|
|
def test_late_forward_ref_dependency_openapi(): |
|
|
|
assert app_with_late_forward_ref.openapi() == snapshot( |
|
|
|
{ |
|
|
|
"openapi": "3.1.0", |
|
|
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
|
|
|
"paths": { |
|
|
|
"/": { |
|
|
|
"get": { |
|
|
|
"summary": "Get Late Forward Ref", |
|
|
|
"operationId": "get_late_forward_ref__get", |
|
|
|
"responses": { |
|
|
|
"200": { |
|
|
|
"description": "Successful Response", |
|
|
|
"content": { |
|
|
|
"application/json": { |
|
|
|
"schema": { |
|
|
|
"additionalProperties": {"type": "string"}, |
|
|
|
"type": "object", |
|
|
|
"title": "Response Get Late Forward Ref Get", |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
) |
|
|
|
|