Skip to content

fix(event_handler): fix format for OpenAPI path templating #3399

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

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ def __init__(
"""
self.method = method.upper()
self.path = "/" if path.strip() == "" else path

# OpenAPI spec only understands paths with { }. So we'll have to convert Powertools' < >.
# https://swagger.io/specification/#path-templating
self.openapi_path = re.sub(r"<(.*?)>", lambda m: f"{{{''.join(m.group(1))}}}", self.path)

self.rule = rule
self.func = func
self._middleware_stack = func
Expand Down Expand Up @@ -435,7 +440,7 @@ def dependant(self) -> "Dependant":
if self._dependant is None:
from aws_lambda_powertools.event_handler.openapi.dependant import get_dependant

self._dependant = get_dependant(path=self.path, call=self.func)
self._dependant = get_dependant(path=self.openapi_path, call=self.func)

return self._dependant

Expand Down Expand Up @@ -542,7 +547,7 @@ def _openapi_operation_summary(self) -> str:
Returns the OpenAPI operation summary. If the user has not provided a summary, we
generate one based on the route path and method.
"""
return self.summary or f"{self.method.upper()} {self.path}"
return self.summary or f"{self.method.upper()} {self.openapi_path}"

def _openapi_operation_metadata(self, operation_ids: Set[str]) -> Dict[str, Any]:
"""
Expand Down Expand Up @@ -692,7 +697,7 @@ def _openapi_operation_return(
return {"schema": return_schema}

def _generate_operation_id(self) -> str:
operation_id = self.func.__name__ + self.path
operation_id = self.func.__name__ + self.openapi_path
operation_id = re.sub(r"\W", "_", operation_id)
operation_id = operation_id + "_" + self.method.lower()
return operation_id
Expand Down Expand Up @@ -1452,7 +1457,7 @@ def get_openapi_schema(
if result:
path, path_definitions = result
if path:
paths.setdefault(route.path, {}).update(path)
paths.setdefault(route.openapi_path, {}).update(path)
if path_definitions:
definitions.update(path_definitions)

Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_powertools/event_handler/openapi/dependant.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:

def get_path_param_names(path: str) -> Set[str]:
"""
Returns the path parameter names from a path template. Those are the strings between < and >.
Returns the path parameter names from a path template. Those are the strings between { and }.

Parameters
----------
Expand All @@ -137,7 +137,7 @@ def get_path_param_names(path: str) -> Set[str]:
The path parameter names

"""
return set(re.findall("<(.*?)>", path))
return set(re.findall("{(.*?)}", path))


def get_dependant(
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/event_handler/test_openapi_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def handler(user_id: str, include_extra: bool = False):
assert schema.info.version == "0.2.2"

assert len(schema.paths.keys()) == 1
assert "/users/<user_id>" in schema.paths
assert "/users/{user_id}" in schema.paths

path = schema.paths["/users/<user_id>"]
path = schema.paths["/users/{user_id}"]
assert path.get

get = path.get
assert get.summary == "GET /users/<user_id>"
assert get.summary == "GET /users/{user_id}"
assert get.operationId == "handler_users__user_id__get"
assert len(get.parameters) == 2

Expand Down