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 response schema #960

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Changes from 1 commit
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
Next Next commit
feat: openapi response schema
  • Loading branch information
VishnuSanal committed Sep 14, 2024
commit 9e5364b2fac68f6c2f60ac03ce717fd011d233bd
39 changes: 24 additions & 15 deletions robyn/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,24 @@ def add_openapi_path_obj(self, route_type: str, endpoint: str, openapi_name: str

query_params = None
request_body = None
return_annotation = None

signature = inspect.signature(handler)
openapi_description = inspect.getdoc(handler) or ""

if signature and "query_params" in signature.parameters:
query_params = signature.parameters["query_params"].default
if signature:
if "query_params" in signature.parameters:
query_params = signature.parameters["query_params"].default

if signature and "body" in signature.parameters:
request_body = signature.parameters["body"].default
if "body" in signature.parameters:
request_body = signature.parameters["body"].default

return_annotation = signature.return_annotation
if signature.return_annotation is not Signature.empty:
return_annotation = signature.return_annotation

return_type = "text/plain" if return_annotation == Signature.empty or return_annotation is str else "application/json"

modified_endpoint, path_obj = self.get_path_obj(endpoint, openapi_name, openapi_description, openapi_tags, query_params, request_body, return_type)
modified_endpoint, path_obj = self.get_path_obj(
endpoint, openapi_name, openapi_description, openapi_tags, query_params, request_body, return_annotation
)

if modified_endpoint not in self.openapi_spec["paths"]:
self.openapi_spec["paths"][modified_endpoint] = {}
Expand All @@ -201,7 +204,7 @@ def get_path_obj(
tags: List[str],
query_params: Optional[TypedDict],
request_body: Optional[TypedDict],
return_type: str,
return_annotation: Optional[TypedDict],
) -> (str, dict):
"""
Get the "path" openapi object according to spec
Expand All @@ -212,7 +215,7 @@ def get_path_obj(
@param tags: List[str] for grouping of endpoints
@param query_params: Optional[TypedDict] query params for the function
@param request_body: Optional[TypedDict] request body for the function
@param return_type: str return type of the endpoint handler
@param return_annotation: Optional[TypedDict] return type of the endpoint handler

@return: (str, dict) a tuple containing the endpoint with path params wrapped in braces and the "path" openapi object
according to spec
Expand All @@ -226,7 +229,6 @@ def get_path_obj(
"description": description,
"parameters": [],
"tags": tags,
"responses": {"200": {"description": "Successful Response", "content": {return_type: {"schema": {}}}}},
}

# robyn has paths like /:url/:etc whereas openapi requires path like /{url}/{path}
Expand Down Expand Up @@ -273,7 +275,7 @@ def get_path_obj(
properties = {}

for body_item in request_body.__annotations__:
properties[body_item] = self.get_properties_object(body_item, request_body.__annotations__[body_item])
properties[body_item] = self.get_schema_object(body_item, request_body.__annotations__[body_item])

request_body_object = {
"content": {
Expand All @@ -288,6 +290,13 @@ def get_path_obj(

openapi_path_object["requestBody"] = request_body_object

response_schema = {}

if return_annotation:
response_schema = self.get_schema_object("response object", return_annotation)

openapi_path_object["responses"] = {"200": {"description": "Successful Response", "content": {"application/json": {"schema": response_schema}}}}

return endpoint_with_path_params_wrapped_in_braces, openapi_path_object

def get_openapi_type(self, typed_dict: TypedDict) -> str:
Expand All @@ -313,9 +322,9 @@ def get_openapi_type(self, typed_dict: TypedDict) -> str:
# default to "string" if type is not found
return "string"

def get_properties_object(self, parameter: str, param_type: Any) -> dict:
def get_schema_object(self, parameter: str, param_type: Any) -> dict:
"""
Get the properties object for request body
Get the schema object for request/response body

@param parameter: name of the parameter
@param param_type: Any the type to be inferred
Expand Down Expand Up @@ -351,7 +360,7 @@ def get_properties_object(self, parameter: str, param_type: Any) -> dict:
properties["properties"] = {}

for e in param_type.__annotations__:
properties["properties"][e] = self.get_properties_object(e, param_type.__annotations__[e])
properties["properties"][e] = self.get_schema_object(e, param_type.__annotations__[e])

properties["type"] = "object"

Expand Down