Skip to content

Commit 9f59374

Browse files
authored
fix(event_handler): fix format for OpenAPI path templating (#3399)
1 parent 0cc687a commit 9f59374

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ def __init__(
316316
"""
317317
self.method = method.upper()
318318
self.path = "/" if path.strip() == "" else path
319+
320+
# OpenAPI spec only understands paths with { }. So we'll have to convert Powertools' < >.
321+
# https://swagger.io/specification/#path-templating
322+
self.openapi_path = re.sub(r"<(.*?)>", lambda m: f"{{{''.join(m.group(1))}}}", self.path)
323+
319324
self.rule = rule
320325
self.func = func
321326
self._middleware_stack = func
@@ -435,7 +440,7 @@ def dependant(self) -> "Dependant":
435440
if self._dependant is None:
436441
from aws_lambda_powertools.event_handler.openapi.dependant import get_dependant
437442

438-
self._dependant = get_dependant(path=self.path, call=self.func)
443+
self._dependant = get_dependant(path=self.openapi_path, call=self.func)
439444

440445
return self._dependant
441446

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

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

694699
def _generate_operation_id(self) -> str:
695-
operation_id = self.func.__name__ + self.path
700+
operation_id = self.func.__name__ + self.openapi_path
696701
operation_id = re.sub(r"\W", "_", operation_id)
697702
operation_id = operation_id + "_" + self.method.lower()
698703
return operation_id
@@ -1452,7 +1457,7 @@ def get_openapi_schema(
14521457
if result:
14531458
path, path_definitions = result
14541459
if path:
1455-
paths.setdefault(route.path, {}).update(path)
1460+
paths.setdefault(route.openapi_path, {}).update(path)
14561461
if path_definitions:
14571462
definitions.update(path_definitions)
14581463

aws_lambda_powertools/event_handler/openapi/dependant.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
124124

125125
def get_path_param_names(path: str) -> Set[str]:
126126
"""
127-
Returns the path parameter names from a path template. Those are the strings between < and >.
127+
Returns the path parameter names from a path template. Those are the strings between { and }.
128128
129129
Parameters
130130
----------
@@ -137,7 +137,7 @@ def get_path_param_names(path: str) -> Set[str]:
137137
The path parameter names
138138
139139
"""
140-
return set(re.findall("<(.*?)>", path))
140+
return set(re.findall("{(.*?)}", path))
141141

142142

143143
def get_dependant(

tests/functional/event_handler/test_openapi_params.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ def handler(user_id: str, include_extra: bool = False):
7070
assert schema.info.version == "0.2.2"
7171

7272
assert len(schema.paths.keys()) == 1
73-
assert "/users/<user_id>" in schema.paths
73+
assert "/users/{user_id}" in schema.paths
7474

75-
path = schema.paths["/users/<user_id>"]
75+
path = schema.paths["/users/{user_id}"]
7676
assert path.get
7777

7878
get = path.get
79-
assert get.summary == "GET /users/<user_id>"
79+
assert get.summary == "GET /users/{user_id}"
8080
assert get.operationId == "handler_users__user_id__get"
8181
assert len(get.parameters) == 2
8282

0 commit comments

Comments
 (0)