From 4e7ed9388e21ac17253c0140151c6f80220221f7 Mon Sep 17 00:00:00 2001 From: KiraPC Date: Mon, 12 Apr 2021 10:22:29 +0200 Subject: [PATCH 1/2] deny usage of same router for multiple controller --- example/sample_app/controller/sample_controller.py | 4 ++++ fastapi_router_controller/__init__.py | 2 -- fastapi_router_controller/lib/controller.py | 12 ++++++++++-- fastapi_router_controller/lib/exceptions.py | 3 +++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 fastapi_router_controller/lib/exceptions.py diff --git a/example/sample_app/controller/sample_controller.py b/example/sample_app/controller/sample_controller.py index e10eec6..682f863 100644 --- a/example/sample_app/controller/sample_controller.py +++ b/example/sample_app/controller/sample_controller.py @@ -9,6 +9,10 @@ 'name': 'sample_controller', }) +new_controller = Controller(router, openapi_tag={ + 'name': 'sample_controller', +}) + class SampleObject(BaseModel): id: str = Field(..., description='sample id') diff --git a/fastapi_router_controller/__init__.py b/fastapi_router_controller/__init__.py index e2ec494..c8c901a 100644 --- a/fastapi_router_controller/__init__.py +++ b/fastapi_router_controller/__init__.py @@ -1,7 +1,5 @@ """FastAPI Router Contoller, FastAPI utility to allow Controller Class usage""" -__version__ = "0.1.0" - from fastapi_router_controller.lib.controller import Controller as Controller from fastapi_router_controller.lib.controller import OPEN_API_TAGS as ControllersTags from fastapi_router_controller.lib.controller_loader import ControllerLoader as ControllerLoader diff --git a/fastapi_router_controller/lib/controller.py b/fastapi_router_controller/lib/controller.py index d1a2fa7..3fc91c9 100644 --- a/fastapi_router_controller/lib/controller.py +++ b/fastapi_router_controller/lib/controller.py @@ -1,6 +1,6 @@ import inspect -from copy import deepcopy from fastapi import APIRouter, Depends +from fastapi_router_controller.lib.exceptions import MultipleRouterException OPEN_API_TAGS = [] __app_controllers__ = [] @@ -36,17 +36,24 @@ class Controller(): ''' RC_KEY = '__router__' SIGNATURE_KEY = '__signature__' + HAS_CONTROLLER_KEY = '__has_controller__' def __init__(self, router: APIRouter, openapi_tag: dict = None) -> None: ''' :param router: The FastApi router to link to the Class :param openapi_tag: An openapi object that will describe your routes in the openapi tamplate ''' + # Each Controller must be linked to one fastapi router + if hasattr(router, Controller.HAS_CONTROLLER_KEY): + raise MultipleRouterException + self.router = router self.openapi_tag = openapi_tag if openapi_tag: OPEN_API_TAGS.append(openapi_tag) + + setattr(router, Controller.HAS_CONTROLLER_KEY, True) def __get_parent_routes(self, router: APIRouter): ''' @@ -70,7 +77,7 @@ def wrapper(cls): if hasattr(cls, Controller.RC_KEY): self.__get_parent_routes(cls.__router__) - cls.__router__ = deepcopy(self.router) + cls.__router__ = self.router cls.router = lambda: Controller.__parse_controller_router(cls) return cls @@ -106,6 +113,7 @@ def __parse_controller_router(cls): parameter.replace(kind=inspect.Parameter.KEYWORD_ONLY) for parameter in signature_parameters[1:] ] + new_signature = signature.replace(parameters=new_parameters) setattr(route.endpoint, Controller.SIGNATURE_KEY, new_signature) diff --git a/fastapi_router_controller/lib/exceptions.py b/fastapi_router_controller/lib/exceptions.py new file mode 100644 index 0000000..70f8b14 --- /dev/null +++ b/fastapi_router_controller/lib/exceptions.py @@ -0,0 +1,3 @@ +class MultipleRouterException(Exception): + def __init__(self): + super().__init__('Router already used by another Controller') From bba171a7ed38e5d08340a09d216ae254827e7f5a Mon Sep 17 00:00:00 2001 From: KiraPC Date: Tue, 13 Apr 2021 09:31:19 +0200 Subject: [PATCH 2/2] removed error in sample app --- example/sample_app/controller/sample_controller.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/example/sample_app/controller/sample_controller.py b/example/sample_app/controller/sample_controller.py index 682f863..e10eec6 100644 --- a/example/sample_app/controller/sample_controller.py +++ b/example/sample_app/controller/sample_controller.py @@ -9,10 +9,6 @@ 'name': 'sample_controller', }) -new_controller = Controller(router, openapi_tag={ - 'name': 'sample_controller', -}) - class SampleObject(BaseModel): id: str = Field(..., description='sample id')