From f0ea3c8384a0beae6230dc4a4175ac9a133b680d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santos?= Date: Sun, 14 May 2023 14:06:25 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=96=20chore(spec.py):=20add=20publ?= =?UTF-8?q?ish=5Fapidoc=20flag=20to=20SpecTree=20constructor=20The=20`publ?= =?UTF-8?q?ish=5Fapidoc`=20flag=20is=20added=20to=20the=20`SpecTree`=20con?= =?UTF-8?q?structor=20to=20allow=20users=20to=20disable=20publishing=20of?= =?UTF-8?q?=20the=20API=20documentation.=20If=20the=20flag=20is=20set=20to?= =?UTF-8?q?=20`False`,=20the=20`register=5Froute`=20method=20will=20not=20?= =?UTF-8?q?be=20called,=20and=20the=20`backend.app`=20attribute=20will=20b?= =?UTF-8?q?e=20set=20to=20the=20`app`=20attribute.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spectree/spec.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spectree/spec.py b/spectree/spec.py index 66836bbc..af43207c 100644 --- a/spectree/spec.py +++ b/spectree/spec.py @@ -67,6 +67,7 @@ def __init__( validation_error_model: Optional[ModelType] = None, naming_strategy: NamingStrategy = get_model_key, nested_naming_strategy: NestedNamingStrategy = get_nested_key, + publish_apidoc: bool = True, **kwargs: Any, ): self.naming_strategy = naming_strategy @@ -77,6 +78,7 @@ def __init__( self.validation_error_model = validation_error_model or ValidationError self.config: Configuration = Configuration.parse_obj(kwargs) self.backend_name = backend_name + self.publish_apidoc = publish_apidoc if backend: self.backend = backend(self) else: @@ -95,7 +97,10 @@ def register(self, app: Any): init step. """ self.app = app - self.backend.register_route(self.app) + if self.publish_apidoc: + self.backend.register_route(self.app) + else: + self.backend.app = self.app @property def spec(self): From 7d585b6eaef721abc115dfb40851cf9912538c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santos?= Date: Sun, 14 May 2023 14:18:08 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20fix(spectree):=20remove=20un?= =?UTF-8?q?necessary=20app=20attribute=20from=20backend=20The=20app=20attr?= =?UTF-8?q?ibute=20was=20being=20set=20in=20the=20backend,=20but=20it=20wa?= =?UTF-8?q?s=20not=20being=20used.=20This=20commit=20removes=20the=20app?= =?UTF-8?q?=20attribute=20from=20the=20backend.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spectree/plugins/falcon_plugin.py | 7 +++---- spectree/plugins/starlette_plugin.py | 8 +++----- spectree/spec.py | 2 -- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/spectree/plugins/falcon_plugin.py b/spectree/plugins/falcon_plugin.py index 892661eb..8539f0b7 100644 --- a/spectree/plugins/falcon_plugin.py +++ b/spectree/plugins/falcon_plugin.py @@ -70,12 +70,11 @@ def __init__(self, spectree): self.INT_ARGS_NAMES = ("num_digits", "min", "max") def register_route(self, app: Any): - self.app = app - self.app.add_route( + app.add_route( self.config.spec_url, self.OPEN_API_ROUTE_CLASS(self.spectree.spec) ) for ui in self.config.page_templates: - self.app.add_route( + app.add_route( f"/{self.config.path}/{ui}", self.DOC_PAGE_ROUTE_CLASS( self.config.page_templates[ui], @@ -95,7 +94,7 @@ def find_node(node): for child in node.children: find_node(child) - for route in self.app._router._roots: + for route in self.spectree.app._router._roots: find_node(route) return routes diff --git a/spectree/plugins/starlette_plugin.py b/spectree/plugins/starlette_plugin.py index f0ef2226..c9d0c391 100644 --- a/spectree/plugins/starlette_plugin.py +++ b/spectree/plugins/starlette_plugin.py @@ -36,15 +36,13 @@ def __init__(self, spectree): self.conv2type = {conv: typ for typ, conv in CONVERTOR_TYPES.items()} def register_route(self, app): - self.app = app - - self.app.add_route( + app.add_route( self.config.spec_url, lambda request: JSONResponse(self.spectree.spec), ) for ui in self.config.page_templates: - self.app.add_route( + app.add_route( f"/{self.config.path}/{ui}", lambda request, ui=ui: HTMLResponse( self.config.page_templates[ui].format( @@ -177,7 +175,7 @@ def parse_route(app, prefix=""): else: parse_route(route, prefix=f"{prefix}{route.path}") - parse_route(self.app) + parse_route(self.spectree.app) return routes def bypass(self, func, method): diff --git a/spectree/spec.py b/spectree/spec.py index af43207c..fc0a172c 100644 --- a/spectree/spec.py +++ b/spectree/spec.py @@ -99,8 +99,6 @@ def register(self, app: Any): self.app = app if self.publish_apidoc: self.backend.register_route(self.app) - else: - self.backend.app = self.app @property def spec(self):