Skip to content

Commit 542af61

Browse files
authored
Merge pull request #104 from sondrelg/sondrelg/document-404-response
document 404 response
2 parents 318329a + 3e5c2e7 commit 542af61

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ coverage.xml
3939
*.py,cover
4040
.hypothesis/
4141
.pytest_cache/
42+
.mypy_cache/
4243

4344
# Environments
4445
.env

CONTRIBUTING.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
2-
31
Pull requests and contributions are welcome. Please read the [contributions guidelines](https://fastapi-crudrouter.awtkns.com/contributing) for more details.

fastapi_crudrouter/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.8.2"
1+
__version__ = "0.8.3"

fastapi_crudrouter/core/_base.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def __init__(
9090
response_model=self.schema,
9191
summary="Get One",
9292
dependencies=get_one_route,
93+
error_responses=[NOT_FOUND],
9394
)
9495

9596
if update_route:
@@ -100,6 +101,7 @@ def __init__(
100101
response_model=self.schema,
101102
summary="Update One",
102103
dependencies=update_route,
104+
error_responses=[NOT_FOUND],
103105
)
104106

105107
if delete_one_route:
@@ -110,17 +112,27 @@ def __init__(
110112
response_model=self.schema,
111113
summary="Delete One",
112114
dependencies=delete_one_route,
115+
error_responses=[NOT_FOUND],
113116
)
114117

115118
def _add_api_route(
116119
self,
117120
path: str,
118121
endpoint: Callable[..., Any],
119122
dependencies: Union[bool, DEPENDENCIES],
123+
error_responses: Optional[List[HTTPException]] = None,
120124
**kwargs: Any,
121125
) -> None:
122126
dependencies = [] if isinstance(dependencies, bool) else dependencies
123-
super().add_api_route(path, endpoint, dependencies=dependencies, **kwargs)
127+
responses: Any = (
128+
{err.status_code: {"detail": err.detail} for err in error_responses}
129+
if error_responses
130+
else None
131+
)
132+
133+
super().add_api_route(
134+
path, endpoint, dependencies=dependencies, responses=responses, **kwargs
135+
)
124136

125137
def api_route(
126138
self, path: str, *args: Any, **kwargs: Any

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
import inspect
32
from fastapi.testclient import TestClient
43

54
from .implementations import *

tests/test_openapi_schema.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from pytest import mark
2+
13
from tests import CUSTOM_TAGS
24

35
POTATO_TAGS = ["Potato"]
6+
PATHS = ["/potato", "/carrot"]
47
PATH_TAGS = {
58
"/potato": POTATO_TAGS,
69
"/potato/{item_id}": POTATO_TAGS,
@@ -26,3 +29,19 @@ def test_schema_tags(self, client):
2629

2730
for m in method:
2831
assert method[m]["tags"] == PATH_TAGS[path]
32+
33+
@mark.parametrize("path", PATHS)
34+
def test_response_types(self, client, path):
35+
schema = self.test_schema_exists(client).json()
36+
paths = schema["paths"]
37+
38+
for method in ["get", "post", "delete"]:
39+
assert "200" in paths[path][method]["responses"]
40+
41+
assert "422" in paths[path]["post"]["responses"]
42+
43+
item_path = path + "/{item_id}"
44+
for method in ["get", "put", "delete"]:
45+
assert "200" in paths[item_path][method]["responses"]
46+
assert "404" in paths[item_path][method]["responses"]
47+
assert "422" in paths[item_path][method]["responses"]

0 commit comments

Comments
 (0)