Skip to content

Commit

Permalink
feat(openapi): override openapi json
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Oct 28, 2024
1 parent deea29a commit 5c3b035
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(
self,
file_object: str,
config: Config = Config(),
openapi_json_path: str = None,
openapi: OpenAPI = OpenAPI(),
dependencies: DependencyMap = DependencyMap(),
) -> None:
Expand All @@ -51,6 +52,11 @@ def __init__(
self.dependencies = dependencies
self.openapi = openapi

if openapi_json_path:
openapi.set_json_spec(self.directory_path + "/" + openapi_json_path)
elif os.path.isfile(self.directory_path + "/openapi.json"):
openapi.set_json_spec(self.directory_path + "/openapi.json")

if not bool(os.environ.get("ROBYN_CLI", False)):
# the env variables are already set when are running through the cli
load_vars(project_root=directory_path)
Expand Down
22 changes: 22 additions & 0 deletions robyn/openapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import json
import typing
from dataclasses import asdict, dataclass, field
from importlib import resources
Expand Down Expand Up @@ -138,11 +139,15 @@ class OpenAPI:

info: OpenAPIInfo = field(default_factory=OpenAPIInfo)
openapi_spec: dict = field(init=False)
is_pre_configured: bool = False

def __post_init__(self):
"""
Initializes the openapi_spec dict
"""
if self.is_pre_configured:
return

self.openapi_spec = {
"openapi": "3.1.0",
"info": asdict(self.info),
Expand All @@ -163,6 +168,9 @@ def add_openapi_path_obj(self, route_type: str, endpoint: str, openapi_name: str
@param handler: Callable the handler function for the endpoint
"""

if self.is_pre_configured:
return

query_params = None
request_body = None
return_annotation = None
Expand Down Expand Up @@ -212,6 +220,10 @@ def add_subrouter_paths(self, subrouter_openapi: "OpenAPI"):
@param subrouter_openapi: OpenAPI the OpenAPI object of the current subrouter
"""

if self.is_pre_configured:
return

paths = subrouter_openapi.openapi_spec["paths"]

for path in paths:
Expand Down Expand Up @@ -393,6 +405,16 @@ def get_schema_object(self, parameter: str, param_type: Any) -> dict:

return properties

def set_json_spec(self, openapi_json_spec_path: str):
"""
Set a pre-configured OpenAPI spec
@param openapi_json_spec_path: str the path to the json file
"""
with open(openapi_json_spec_path) as json_file:
json_file_content = json.load(json_file)
self.openapi_spec = dict(json_file_content)
self.is_pre_configured = True

def get_openapi_docs_page(self) -> FileResponse:
"""
Handler to the swagger html page to be deployed to the endpoint `/docs`
Expand Down

0 comments on commit 5c3b035

Please sign in to comment.