From 1596d27afad0a14524e14b821aa635a82b289264 Mon Sep 17 00:00:00 2001 From: YuhangWu Date: Fri, 2 Feb 2024 13:37:59 +0800 Subject: [PATCH 1/3] fix: return_annotation is missing #384 --- fastapi_cache/decorator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index 4c7cf33b..bf3523e7 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -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]: From 8de34c31b11a7d5d88226538d577ec57abb23cba Mon Sep 17 00:00:00 2001 From: YuhangWu Date: Fri, 2 Feb 2024 13:38:11 +0800 Subject: [PATCH 2/3] test: add a testcases. --- tests/test_decorator.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/test_decorator.py b/tests/test_decorator.py index fefa182b..f542f43a 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -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) @@ -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" From a40f41e946a96eb1e71863c2d5806bebf1002cae Mon Sep 17 00:00:00 2001 From: YuhangWu Date: Fri, 2 Feb 2024 15:02:32 +0800 Subject: [PATCH 3/3] docs: Add a changelog. --- changelog.d/384.bugfix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/384.bugfix.md diff --git a/changelog.d/384.bugfix.md b/changelog.d/384.bugfix.md new file mode 100644 index 00000000..07eb5a6b --- /dev/null +++ b/changelog.d/384.bugfix.md @@ -0,0 +1 @@ +Fix the issue of missing response model in the OpenAPI spec for the endpoint is decorated with `@cache`