Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: openapi config override #1002

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs_src/src/pages/documentation/api_reference/openapi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Out of the box, the following endpoints are setup for you:
- `/docs` The Swagger UI
- `/openapi.json` The JSON Specification

To use a custom openapi configuration, you can:

- Place the `openapi.json` config file in the root directory.
- Or, pass the file path to the `openapi_file_path` parameter in the `Robyn()` constructor. (the parameter gets priority over the file).

However, if you don't want to generate the OpenAPI docs, you can disable it by passing `--disable-openapi` flag while starting the application.

Expand Down
5 changes: 5 additions & 0 deletions docs_src/src/pages/documentation/example_app/openapi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Out of the box, the following endpoints are setup for you:

However, if you don't want to generate the OpenAPI docs, you can disable it by passing `--disable-openapi` flag while starting the application.

To use a custom openapi configuration, you can:

- Place the `openapi.json` config file in the root directory.
- Or, pass the file path to the `openapi_file_path` parameter in the `Robyn()` constructor. (the parameter gets priority over the file).

```bash
python app.py --disable-openapi
```
Expand Down
8 changes: 7 additions & 1 deletion 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_file_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_file_path:
openapi.set_json_spec(self.directory_path + "/" + openapi_file_path)
elif os.path.isfile(self.directory_path + "/openapi.json"):
openapi.set_json_spec(self.directory_path + "/openapi.json")
VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VishnuSanal , could you explain the logic here? What is this supposed to do?

It should just be a file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should invoke the override function if the path param is set or if the file i9s present in the pre-defnked path

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 Expand Up @@ -583,7 +589,7 @@ def configure_authentication(self, authentication_handler: AuthenticationHandler

class SubRouter(Robyn):
def __init__(self, file_object: str, prefix: str = "", config: Config = Config(), openapi: OpenAPI = OpenAPI()) -> None:
super().__init__(file_object, config, openapi)
super().__init__(file_object=file_object, config=config, openapi=openapi)
VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved
self.prefix = prefix

def __add_prefix(self, endpoint: str):
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)
openapi_file_override: bool = False # denotes whether running in the pre-configured file mode or not.
VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved

def __post_init__(self):
"""
Initializes the openapi_spec dict
"""
if self.openapi_file_override:
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.openapi_file_override:
return

VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved
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.openapi_file_override:
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):
VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved
"""
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.openapi_file_override = True

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