diff --git a/.gitignore b/.gitignore index cd099f5..2679283 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ coverage.xml *.py,cover .hypothesis/ .pytest_cache/ +.mypy_cache/ # Environments .env diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 37256bf..48baf0b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1 @@ - - Pull requests and contributions are welcome. Please read the [contributions guidelines](https://fastapi-crudrouter.awtkns.com/contributing) for more details. diff --git a/fastapi_crudrouter/_version.py b/fastapi_crudrouter/_version.py index deded32..732155f 100644 --- a/fastapi_crudrouter/_version.py +++ b/fastapi_crudrouter/_version.py @@ -1 +1 @@ -__version__ = "0.8.2" +__version__ = "0.8.3" diff --git a/fastapi_crudrouter/core/_base.py b/fastapi_crudrouter/core/_base.py index 3e3f394..f6379e8 100644 --- a/fastapi_crudrouter/core/_base.py +++ b/fastapi_crudrouter/core/_base.py @@ -90,6 +90,7 @@ def __init__( response_model=self.schema, summary="Get One", dependencies=get_one_route, + error_responses=[NOT_FOUND], ) if update_route: @@ -100,6 +101,7 @@ def __init__( response_model=self.schema, summary="Update One", dependencies=update_route, + error_responses=[NOT_FOUND], ) if delete_one_route: @@ -110,6 +112,7 @@ def __init__( response_model=self.schema, summary="Delete One", dependencies=delete_one_route, + error_responses=[NOT_FOUND], ) def _add_api_route( @@ -117,10 +120,19 @@ def _add_api_route( path: str, endpoint: Callable[..., Any], dependencies: Union[bool, DEPENDENCIES], + error_responses: Optional[List[HTTPException]] = None, **kwargs: Any, ) -> None: dependencies = [] if isinstance(dependencies, bool) else dependencies - super().add_api_route(path, endpoint, dependencies=dependencies, **kwargs) + responses: Any = ( + {err.status_code: {"detail": err.detail} for err in error_responses} + if error_responses + else None + ) + + super().add_api_route( + path, endpoint, dependencies=dependencies, responses=responses, **kwargs + ) def api_route( self, path: str, *args: Any, **kwargs: Any diff --git a/tests/conftest.py b/tests/conftest.py index af6903f..74ac91a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,4 @@ import pytest -import inspect from fastapi.testclient import TestClient from .implementations import * diff --git a/tests/test_openapi_schema.py b/tests/test_openapi_schema.py index 73de040..61faf75 100644 --- a/tests/test_openapi_schema.py +++ b/tests/test_openapi_schema.py @@ -1,6 +1,9 @@ +from pytest import mark + from tests import CUSTOM_TAGS POTATO_TAGS = ["Potato"] +PATHS = ["/potato", "/carrot"] PATH_TAGS = { "/potato": POTATO_TAGS, "/potato/{item_id}": POTATO_TAGS, @@ -26,3 +29,19 @@ def test_schema_tags(self, client): for m in method: assert method[m]["tags"] == PATH_TAGS[path] + + @mark.parametrize("path", PATHS) + def test_response_types(self, client, path): + schema = self.test_schema_exists(client).json() + paths = schema["paths"] + + for method in ["get", "post", "delete"]: + assert "200" in paths[path][method]["responses"] + + assert "422" in paths[path]["post"]["responses"] + + item_path = path + "/{item_id}" + for method in ["get", "put", "delete"]: + assert "200" in paths[item_path][method]["responses"] + assert "404" in paths[item_path][method]["responses"] + assert "422" in paths[item_path][method]["responses"]