From cdffdf426990db46f225d50f241c8f718780d022 Mon Sep 17 00:00:00 2001 From: Diogo Fernandes Date: Thu, 3 Mar 2022 22:16:51 +0000 Subject: [PATCH 1/2] Fix 404 on openapi spec --- fastapi_crudrouter/core/_base.py | 19 ++++++++++++++++--- fastapi_crudrouter/core/databases.py | 6 +++--- fastapi_crudrouter/core/gino_starlette.py | 2 +- fastapi_crudrouter/core/mem.py | 6 +++--- fastapi_crudrouter/core/ormar.py | 2 +- fastapi_crudrouter/core/sqlalchemy.py | 2 +- fastapi_crudrouter/core/tortoise.py | 2 +- 7 files changed, 26 insertions(+), 13 deletions(-) diff --git a/fastapi_crudrouter/core/_base.py b/fastapi_crudrouter/core/_base.py index e45d33fe..e298e52b 100644 --- a/fastapi_crudrouter/core/_base.py +++ b/fastapi_crudrouter/core/_base.py @@ -3,11 +3,21 @@ from fastapi import APIRouter, HTTPException from fastapi.types import DecoratedCallable +from pydantic import BaseModel -from ._types import T, DEPENDENCIES +from ._types import DEPENDENCIES, T from ._utils import pagination_factory, schema_factory -NOT_FOUND = HTTPException(404, "Item not found") + +class NotFoundModel(BaseModel): + detail: str + + +NOT_FOUND = { + "status_code": 404, + "http_exeption": HTTPException(404, "Item not found"), + "class_exeption": NotFoundModel, +} class CRUDGenerator(Generic[T], APIRouter, ABC): @@ -126,7 +136,10 @@ def _add_api_route( ) -> None: dependencies = [] if isinstance(dependencies, bool) else dependencies responses: Any = ( - {err.status_code: {"detail": err.detail} for err in error_responses} + { + err["status_code"]: {"model": err["class_exeption"]} + for err in error_responses + } if error_responses else None ) diff --git a/fastapi_crudrouter/core/databases.py b/fastapi_crudrouter/core/databases.py index 7ea3c711..b798476a 100644 --- a/fastapi_crudrouter/core/databases.py +++ b/fastapi_crudrouter/core/databases.py @@ -103,7 +103,7 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore if model: return pydantify_record(model) # type: ignore else: - raise NOT_FOUND + raise NOT_FOUND["http_exeption"] return route @@ -136,7 +136,7 @@ async def route( ) return await self._get_one()(item_id) except Exception as e: - raise NOT_FOUND from e + raise NOT_FOUND["http_exeption"] from e return route @@ -158,6 +158,6 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore await self.db.execute(query=query) return row except Exception as e: - raise NOT_FOUND from e + raise NOT_FOUND["http_exeption"] from e return route diff --git a/fastapi_crudrouter/core/gino_starlette.py b/fastapi_crudrouter/core/gino_starlette.py index d07d893e..57768bfc 100644 --- a/fastapi_crudrouter/core/gino_starlette.py +++ b/fastapi_crudrouter/core/gino_starlette.py @@ -86,7 +86,7 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore if model: return model else: - raise NOT_FOUND + raise NOT_FOUND["http_exeption"] return route diff --git a/fastapi_crudrouter/core/mem.py b/fastapi_crudrouter/core/mem.py index d4e13c11..70d27b06 100644 --- a/fastapi_crudrouter/core/mem.py +++ b/fastapi_crudrouter/core/mem.py @@ -62,7 +62,7 @@ def route(item_id: int) -> SCHEMA: if model.id == item_id: # type: ignore return model - raise NOT_FOUND + raise NOT_FOUND["http_exeption"] return route @@ -85,7 +85,7 @@ def route(item_id: int, model: self.update_schema) -> SCHEMA: # type: ignore ) return self.models[ind] - raise NOT_FOUND + raise NOT_FOUND["http_exeption"] return route @@ -103,7 +103,7 @@ def route(item_id: int) -> SCHEMA: del self.models[ind] return model - raise NOT_FOUND + raise NOT_FOUND["http_exeption"] return route diff --git a/fastapi_crudrouter/core/ormar.py b/fastapi_crudrouter/core/ormar.py index 99952600..523582c7 100644 --- a/fastapi_crudrouter/core/ormar.py +++ b/fastapi_crudrouter/core/ormar.py @@ -87,7 +87,7 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore _exclude=False, **filter_ ).first() except NoMatch: - raise NOT_FOUND from None + raise NOT_FOUND["http_exeption"] from None return model return route diff --git a/fastapi_crudrouter/core/sqlalchemy.py b/fastapi_crudrouter/core/sqlalchemy.py index 58270f34..7202a40b 100644 --- a/fastapi_crudrouter/core/sqlalchemy.py +++ b/fastapi_crudrouter/core/sqlalchemy.py @@ -93,7 +93,7 @@ def route( if model: return model else: - raise NOT_FOUND from None + raise NOT_FOUND["http_exeption"] from None return route diff --git a/fastapi_crudrouter/core/tortoise.py b/fastapi_crudrouter/core/tortoise.py index 52972a48..c42b3adf 100644 --- a/fastapi_crudrouter/core/tortoise.py +++ b/fastapi_crudrouter/core/tortoise.py @@ -74,7 +74,7 @@ async def route(item_id: int) -> Model: if model: return model else: - raise NOT_FOUND + raise NOT_FOUND["http_exeption"] return route From 1cfdef8fc0192b792d768934b7966ed2bbe80b81 Mon Sep 17 00:00:00 2001 From: Diogo Fernandes Date: Tue, 31 May 2022 20:42:22 +0100 Subject: [PATCH 2/2] lint --- fastapi_crudrouter/core/_base.py | 10 ++-------- fastapi_crudrouter/core/databases.py | 6 +++--- fastapi_crudrouter/core/gino_starlette.py | 2 +- fastapi_crudrouter/core/mem.py | 6 +++--- fastapi_crudrouter/core/ormar.py | 2 +- fastapi_crudrouter/core/sqlalchemy.py | 2 +- fastapi_crudrouter/core/tortoise.py | 2 +- 7 files changed, 12 insertions(+), 18 deletions(-) diff --git a/fastapi_crudrouter/core/_base.py b/fastapi_crudrouter/core/_base.py index e298e52b..1dd64438 100644 --- a/fastapi_crudrouter/core/_base.py +++ b/fastapi_crudrouter/core/_base.py @@ -12,13 +12,7 @@ class NotFoundModel(BaseModel): detail: str - -NOT_FOUND = { - "status_code": 404, - "http_exeption": HTTPException(404, "Item not found"), - "class_exeption": NotFoundModel, -} - +NOT_FOUND = HTTPException(404, "Item not found") class CRUDGenerator(Generic[T], APIRouter, ABC): schema: Type[T] @@ -137,7 +131,7 @@ def _add_api_route( dependencies = [] if isinstance(dependencies, bool) else dependencies responses: Any = ( { - err["status_code"]: {"model": err["class_exeption"]} + err.status_code: {"model": NotFoundModel} for err in error_responses } if error_responses diff --git a/fastapi_crudrouter/core/databases.py b/fastapi_crudrouter/core/databases.py index b798476a..7ea3c711 100644 --- a/fastapi_crudrouter/core/databases.py +++ b/fastapi_crudrouter/core/databases.py @@ -103,7 +103,7 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore if model: return pydantify_record(model) # type: ignore else: - raise NOT_FOUND["http_exeption"] + raise NOT_FOUND return route @@ -136,7 +136,7 @@ async def route( ) return await self._get_one()(item_id) except Exception as e: - raise NOT_FOUND["http_exeption"] from e + raise NOT_FOUND from e return route @@ -158,6 +158,6 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore await self.db.execute(query=query) return row except Exception as e: - raise NOT_FOUND["http_exeption"] from e + raise NOT_FOUND from e return route diff --git a/fastapi_crudrouter/core/gino_starlette.py b/fastapi_crudrouter/core/gino_starlette.py index 57768bfc..d07d893e 100644 --- a/fastapi_crudrouter/core/gino_starlette.py +++ b/fastapi_crudrouter/core/gino_starlette.py @@ -86,7 +86,7 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore if model: return model else: - raise NOT_FOUND["http_exeption"] + raise NOT_FOUND return route diff --git a/fastapi_crudrouter/core/mem.py b/fastapi_crudrouter/core/mem.py index 70d27b06..d4e13c11 100644 --- a/fastapi_crudrouter/core/mem.py +++ b/fastapi_crudrouter/core/mem.py @@ -62,7 +62,7 @@ def route(item_id: int) -> SCHEMA: if model.id == item_id: # type: ignore return model - raise NOT_FOUND["http_exeption"] + raise NOT_FOUND return route @@ -85,7 +85,7 @@ def route(item_id: int, model: self.update_schema) -> SCHEMA: # type: ignore ) return self.models[ind] - raise NOT_FOUND["http_exeption"] + raise NOT_FOUND return route @@ -103,7 +103,7 @@ def route(item_id: int) -> SCHEMA: del self.models[ind] return model - raise NOT_FOUND["http_exeption"] + raise NOT_FOUND return route diff --git a/fastapi_crudrouter/core/ormar.py b/fastapi_crudrouter/core/ormar.py index 523582c7..99952600 100644 --- a/fastapi_crudrouter/core/ormar.py +++ b/fastapi_crudrouter/core/ormar.py @@ -87,7 +87,7 @@ async def route(item_id: self._pk_type) -> Model: # type: ignore _exclude=False, **filter_ ).first() except NoMatch: - raise NOT_FOUND["http_exeption"] from None + raise NOT_FOUND from None return model return route diff --git a/fastapi_crudrouter/core/sqlalchemy.py b/fastapi_crudrouter/core/sqlalchemy.py index 7202a40b..58270f34 100644 --- a/fastapi_crudrouter/core/sqlalchemy.py +++ b/fastapi_crudrouter/core/sqlalchemy.py @@ -93,7 +93,7 @@ def route( if model: return model else: - raise NOT_FOUND["http_exeption"] from None + raise NOT_FOUND from None return route diff --git a/fastapi_crudrouter/core/tortoise.py b/fastapi_crudrouter/core/tortoise.py index c42b3adf..52972a48 100644 --- a/fastapi_crudrouter/core/tortoise.py +++ b/fastapi_crudrouter/core/tortoise.py @@ -74,7 +74,7 @@ async def route(item_id: int) -> Model: if model: return model else: - raise NOT_FOUND["http_exeption"] + raise NOT_FOUND return route