Skip to content

fix(event_handlers): omit explicit None HTTP header values #1793

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 2 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
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
25 changes: 14 additions & 11 deletions aws_lambda_powertools/shared/headers_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
if isinstance(values, str):
combined_headers[key] = values
else:
combined_headers[key] = ", ".join(values)
if values:
combined_headers[key] = ", ".join(values)

return {"headers": combined_headers, "cookies": list(map(str, cookies))}

Expand All @@ -65,8 +66,9 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
if isinstance(values, str):
payload[key].append(values)
else:
for value in values:
payload[key].append(value)
if values:
Copy link
Contributor

Choose a reason for hiding this comment

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

when you accept a falsy value but not null, check for null otherwise a X-Is-Admin: false will be omitted here. For example, if values is None, would work in both cases.

for value in values:
payload[key].append(value)
Copy link
Contributor

Choose a reason for hiding this comment

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

we can drop an additional for loop by using list.extend, as it accepts an Iterable and it'll add all elements at the end of the list. This drops the complexity


if cookies:
payload.setdefault("Set-Cookie", [])
Expand Down Expand Up @@ -101,13 +103,14 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
if isinstance(values, str):
payload["headers"][key] = values
else:
if len(values) > 1:
warnings.warn(
f"Can't encode more than one header value for the same key ('{key}') in the response. "
"Did you enable multiValueHeaders on the ALB Target Group?"
)

# We can only set one header per key, send the last one
payload["headers"][key] = values[-1]
if values:
if len(values) > 1:
warnings.warn(
f"Can't encode more than one header value for the same key ('{key}') in the response. "
"Did you enable multiValueHeaders on the ALB Target Group?"
)

# We can only set one header per key, send the last one
payload["headers"][key] = values[-1]

return payload
18 changes: 18 additions & 0 deletions tests/functional/test_headers_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,21 @@ def test_single_value_headers_with_multiple_header_values_warning():
payload = serializer.serialize(cookies=[], headers=headers)

assert payload["headers"]["Foo"] == headers["Foo"][-1]


def test_http_api_headers_serializer_with_null_values():
serializer = HttpApiHeadersSerializer()
payload = serializer.serialize(headers={"Foo": None}, cookies=[])
assert payload == {"headers": {}, "cookies": []}


def test_multi_value_headers_serializer_with_null_values():
serializer = MultiValueHeadersSerializer()
payload = serializer.serialize(headers={"Foo": None}, cookies=[])
assert payload == {"multiValueHeaders": {}}


def test_single_value_headers_serializer_with_null_values():
serializer = SingleValueHeadersSerializer()
payload = serializer.serialize(headers={"Foo": None}, cookies=[])
assert payload["headers"] == {}