Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/384.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the issue of missing response model in the OpenAPI spec for the endpoint is decorated with `@cache`
3 changes: 2 additions & 1 deletion fastapi_cache/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ def wrapper(
func: Callable[P, Awaitable[R]]
) -> Callable[P, Awaitable[Union[R, Response]]]:
# get_typed_signature ensures that any forward references are resolved first
return_type = get_typed_return_annotation(func)
wrapped_signature = get_typed_signature(func)
wrapped_signature._return_annotation = return_type # type: ignore[attr-defined]
to_inject: List[Parameter] = []
request_param = _locate_param(wrapped_signature, injected_request, to_inject)
response_param = _locate_param(wrapped_signature, injected_response, to_inject)
return_type = get_typed_return_annotation(func)

@wraps(func)
async def inner(*args: P.args, **kwargs: P.kwargs) -> Union[R, Response]:
Expand Down
18 changes: 15 additions & 3 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

import pendulum
import pytest
from starlette.testclient import TestClient

from examples.in_memory.main import app
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from starlette.testclient import TestClient


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -111,4 +110,17 @@ def test_alternate_injected_namespace() -> None:
with TestClient(app) as client:
response = client.get("/namespaced_injection")
assert response.headers.get("X-FastAPI-Cache") == "MISS"
assert response.json() == {"__fastapi_cache_request": 42, "__fastapi_cache_response": 17}
assert response.json() == {
"__fastapi_cache_request": 42,
"__fastapi_cache_response": 17,
}


def test_annotated_return_type() -> None:
with TestClient(app) as client:
response = client.get("/openapi.json")
assert response.status_code == 200
js = response.json()
assert js["paths"]["/pydantic_instance"]["get"]["responses"]["200"]["content"][
"application/json"
]["schema"]["$ref"] == "#/components/schemas/Item"