From cf8bb940ffee5b5a2b9bdaf4fe797344783ae002 Mon Sep 17 00:00:00 2001 From: tengfei Date: Tue, 15 Jul 2025 22:50:11 +0800 Subject: [PATCH 1/2] feat: add security scheme config to openapi (#1122) --- robyn/__init__.py | 10 ++++++++++ robyn/openapi.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/robyn/__init__.py b/robyn/__init__.py index e35b01a0b..8598e3fc7 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -266,6 +266,16 @@ def _add_openapi_routes(self, auth_required: bool = False): logger.error("No openAPI") return + if self.authentication_handler: + # self.authentication_handler.token_getter + self.openapi.add_global_security_scheme( + name="bearerAuth", + scheme={ + "type": "http", + "scheme": "bearer" + } + ) + self.router.prepare_routes_openapi(self.openapi, self.included_routers) self.add_route( diff --git a/robyn/openapi.py b/robyn/openapi.py index 0e0050d76..c0bb12cee 100644 --- a/robyn/openapi.py +++ b/robyn/openapi.py @@ -161,8 +161,18 @@ def __post_init__(self): "components": asdict(self.info.components), "servers": [asdict(server) for server in self.info.servers], "externalDocs": asdict(self.info.externalDocs) if self.info.externalDocs.url else None, + "security": [], } + def add_global_security_scheme(self, name: str, scheme: dict): + """ + Adds a security scheme to the OpenAPI spec. + @param name: str The name of the security scheme. + @param scheme: dict The security scheme object to be added. + """ + self.openapi_spec["components"]["securitySchemes"][name] = scheme + self.openapi_spec["security"].append({name: []}) + def add_openapi_path_obj(self, route_type: str, endpoint: str, openapi_name: str, openapi_tags: List[str], handler: Callable): """ Adds the given path to openapi spec From 39f330a91671261cd7a56dc070c85b0b8c86025e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Jul 2025 15:08:00 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- robyn/__init__.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/robyn/__init__.py b/robyn/__init__.py index 8598e3fc7..9b11c7ac7 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -268,13 +268,7 @@ def _add_openapi_routes(self, auth_required: bool = False): if self.authentication_handler: # self.authentication_handler.token_getter - self.openapi.add_global_security_scheme( - name="bearerAuth", - scheme={ - "type": "http", - "scheme": "bearer" - } - ) + self.openapi.add_global_security_scheme(name="bearerAuth", scheme={"type": "http", "scheme": "bearer"}) self.router.prepare_routes_openapi(self.openapi, self.included_routers)