Skip to content

document 404 response #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 19, 2021
Merged
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
.mypy_cache/

# Environments
.env
Expand Down
2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@


Pull requests and contributions are welcome. Please read the [contributions guidelines](https://fastapi-crudrouter.awtkns.com/contributing) for more details.
2 changes: 1 addition & 1 deletion fastapi_crudrouter/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.8.2"
__version__ = "0.8.3"
14 changes: 13 additions & 1 deletion fastapi_crudrouter/core/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(
response_model=self.schema,
summary="Get One",
dependencies=get_one_route,
error_responses=[NOT_FOUND],
)

if update_route:
Expand All @@ -100,6 +101,7 @@ def __init__(
response_model=self.schema,
summary="Update One",
dependencies=update_route,
error_responses=[NOT_FOUND],
)

if delete_one_route:
Expand All @@ -110,17 +112,27 @@ def __init__(
response_model=self.schema,
summary="Delete One",
dependencies=delete_one_route,
error_responses=[NOT_FOUND],
)

def _add_api_route(
self,
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
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import inspect
from fastapi.testclient import TestClient

from .implementations import *
Expand Down
19 changes: 19 additions & 0 deletions tests/test_openapi_schema.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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"]