diff --git a/README.md b/README.md index a86e9ce..da17268 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ class Item(BaseModel): @app.get("/items", response_model=List[Item], description="This is a route") def get_operation(): - return [Item(id=0, name="ciao"), Item(id=1, name="hola"), Item(id=1, name="hello")] + return [Item(id=1, name="ciao"), Item(id=2, name="hola"), Item(id=3, name="hello")] ``` Te result of `GET /items`: @@ -82,15 +82,15 @@ content-type: application/json { "data": [ { - "id": 0, + "id": 1, "name": "ciao" }, { - "id": 1, + "id": 2, "name": "hola" }, { - "id": 1, + "id": 3, "name": "hello" } ], diff --git a/fastapi_responseschema/integrations/pagination.py b/fastapi_responseschema/integrations/pagination.py index 8f0b75f..64ccc6e 100644 --- a/fastapi_responseschema/integrations/pagination.py +++ b/fastapi_responseschema/integrations/pagination.py @@ -125,13 +125,3 @@ def get_wrapper_model(self, is_error: bool, response_model: Type[Any]) -> Type[A return self.paged_response_schema return self.error_response_schema if is_error else self.paged_response_schema return super().get_wrapper_model(is_error, response_model) - - def override_response_model(self, wrapper_model: Type[AbstractResponseSchema[Any]], response_model: Type[Any]) -> Type[AbstractResponseSchema[Any]]: - if issubclass(response_model, AbstractPagedResponseSchema): - return response_model - return super().override_response_model(wrapper_model, response_model) - - def _wrap_endpoint_output(self, endpoint_output: Any, wrapper_model: Type[AbstractResponseSchema], response_model: Type[Any], **params) -> Any: - if issubclass(response_model, AbstractPagedResponseSchema): - response_model = response_model.__inner_types__ - return super()._wrap_endpoint_output(endpoint_output, wrapper_model, response_model, **params) diff --git a/tests/test_integration.py b/tests/test_integration.py index 39556de..da6c6b3 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -10,13 +10,13 @@ class Route(SchemaAPIRoute): wrap_app_responses(app_wrapped_no_router, Route) -@app_wrapped_no_router.get("/wrapped", response_schema=bool) +@app_wrapped_no_router.get("/wrapped", response_model=bool) def wrapped_call_first(): return True router_unwrapped = APIRouter() -@router_unwrapped.get("/unwrapped", response_schema=dict) +@router_unwrapped.get("/unwrapped", response_model=dict) def unwrapped_call_first(): return {"unwrapped": True} @@ -37,3 +37,33 @@ def test_router_unwrapped_call(): resp = r.json() assert resp.get("unwrapped") + +app_not_wrapped = FastAPI() + +@app_not_wrapped.get("/unwrapped", response_model=dict) +def un_wrapped_call_second(): + return {"unwrapped": True} + + +router_wrapped = APIRouter(route_class=Route) +@router_wrapped.get("/wrapped", response_model=dict) +def wrapped_call_second(): + return {"wrapped": True} + +app_not_wrapped.include_router(router_wrapped) + +client_app_not_wrapped = TestClient(app_not_wrapped) + + +def test_not_recursive_wrapping_on_router(): + r = client_app_not_wrapped.get("/wrapped") + resp = r.json() + assert resp.get("data").get("wrapped") + assert not resp.get("error") + + +def test_app_unwrapped_call(): + r = client_app_wrapped_no_router.get("/unwrapped") + resp = r.json() + assert resp.get("unwrapped") + diff --git a/tests/test_pagination_integration.py b/tests/test_pagination_integration.py index 99d18c5..a2846eb 100644 --- a/tests/test_pagination_integration.py +++ b/tests/test_pagination_integration.py @@ -1,6 +1,6 @@ from typing import TypeVar, Generic, Any, Sequence import pytest -from fastapi import FastAPI +from fastapi import FastAPI, APIRouter from fastapi.testclient import TestClient from fastapi_responseschema.integrations.pagination import ( AbstractPagedResponseSchema, @@ -112,14 +112,30 @@ class Route(PagedSchemaAPIRoute): def with_response_model(): return paginate([True, False, True, False]) +router = APIRouter(route_class=Route) + +@router.get("/with-router", response_model=SimplePagedResponseSchema[bool]) +def with_router(): + return paginate([True, False, True, False]) + +app.include_router(router) + add_pagination(app) client = TestClient(app) -def test_response_model_paginated_wrapping(): +def test_response_model_paginated_wrapping_in_app(): raw = client.get("/with-model") r = raw.json() assert not r.get("data")[1] assert r.get("pagination").get("total") == 4 + assert not r.get("error") + + +def test_response_model_paginated_wrapping_in_router(): + raw = client.get("/with-router") + r = raw.json() + assert not r.get("data")[1] + assert r.get("pagination").get("total") == 4 assert not r.get("error") \ No newline at end of file