Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 18f4418

Browse files
committedOct 23, 2023
fix: improve coverage of params.py
1 parent 80375e4 commit 18f4418

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed
 

‎aws_lambda_powertools/event_handler/openapi/dependant.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def get_flat_params(dependant: Dependant) -> List[ModelField]:
258258
A list of ModelField objects containing the flat parameters from the Dependant object.
259259
260260
"""
261-
flat_dependant = get_flat_dependant(dependant, skip_repeats=True)
261+
flat_dependant = get_flat_dependant(dependant)
262262
return (
263263
flat_dependant.path_params
264264
+ flat_dependant.query_params

‎aws_lambda_powertools/event_handler/openapi/params.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def __init__(
4747
cookie_params: Optional[List[ModelField]] = None,
4848
body_params: Optional[List[ModelField]] = None,
4949
return_param: Optional[ModelField] = None,
50-
dependencies: Optional[List["Dependant"]] = None,
5150
name: Optional[str] = None,
5251
call: Optional[Callable[..., Any]] = None,
5352
request_param_name: Optional[str] = None,
@@ -63,7 +62,6 @@ def __init__(
6362
self.cookie_params = cookie_params or []
6463
self.body_params = body_params or []
6564
self.return_param = return_param or None
66-
self.dependencies = dependencies or []
6765
self.request_param_name = request_param_name
6866
self.websocket_param_name = websocket_param_name
6967
self.http_connection_param_name = http_connection_param_name
@@ -618,8 +616,6 @@ def __init__(
618616

619617
def get_flat_dependant(
620618
dependant: Dependant,
621-
*,
622-
skip_repeats: bool = False,
623619
visited: Optional[List[CacheKey]] = None,
624620
) -> Dependant:
625621
"""
@@ -647,27 +643,14 @@ def get_flat_dependant(
647643
visited = []
648644
visited.append(dependant.cache_key)
649645

650-
flat_dependant = Dependant(
646+
return Dependant(
651647
path_params=dependant.path_params.copy(),
652648
query_params=dependant.query_params.copy(),
653649
header_params=dependant.header_params.copy(),
654650
cookie_params=dependant.cookie_params.copy(),
655651
body_params=dependant.body_params.copy(),
656652
path=dependant.path,
657653
)
658-
for sub_dependant in dependant.dependencies:
659-
if skip_repeats and sub_dependant.cache_key in visited:
660-
continue
661-
662-
flat_sub = get_flat_dependant(sub_dependant, skip_repeats=skip_repeats, visited=visited)
663-
664-
flat_dependant.path_params.extend(flat_sub.path_params)
665-
flat_dependant.query_params.extend(flat_sub.query_params)
666-
flat_dependant.header_params.extend(flat_sub.header_params)
667-
flat_dependant.cookie_params.extend(flat_sub.cookie_params)
668-
flat_dependant.body_params.extend(flat_sub.body_params)
669-
670-
return flat_dependant
671654

672655

673656
def analyze_param(

‎tests/functional/event_handler/test_openapi_params.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
ParameterInType,
1212
Schema,
1313
)
14-
from aws_lambda_powertools.event_handler.openapi.params import Body, Query
14+
from aws_lambda_powertools.event_handler.openapi.params import (
15+
Body,
16+
Header,
17+
Param,
18+
ParamTypes,
19+
Query,
20+
_create_model_field,
21+
)
1522
from aws_lambda_powertools.shared.types import Annotated
1623

1724
JSON_CONTENT_TYPE = "application/json"
@@ -274,3 +281,35 @@ def handler(user: Annotated[User, Body(embed=True)]):
274281
assert "Body_handler_users_post" in components.schemas
275282
body_post_handler_schema = components.schemas["Body_handler_users_post"]
276283
assert body_post_handler_schema.properties["user"].ref == "#/components/schemas/User"
284+
285+
286+
def test_create_header():
287+
header = Header(convert_underscores=True)
288+
assert header.convert_underscores is True
289+
290+
291+
def test_create_body():
292+
body = Body(embed=True, examples=[Example(summary="Example 1", value=10)])
293+
assert body.embed is True
294+
295+
296+
# Tests that when we try to create a model without a field type, we return None
297+
def test_create_empty_model_field():
298+
result = _create_model_field(None, int, "name", False)
299+
assert result is None
300+
301+
302+
# Tests that when we try to crate a param model without a source, we default to "query"
303+
def test_create_model_field_with_empty_in():
304+
field_info = Param()
305+
306+
result = _create_model_field(field_info, int, "name", False)
307+
assert result.field_info.in_ == ParamTypes.query
308+
309+
310+
# Tests that when we try to create a model field with convert_underscore, we convert the field name
311+
def test_create_model_field_convert_underscore():
312+
field_info = Header(alias=None, convert_underscores=True)
313+
314+
result = _create_model_field(field_info, int, "user_id", False)
315+
assert result.alias == "user-id"

0 commit comments

Comments
 (0)
Please sign in to comment.