Skip to content

fix(event_handler): skip compression when the response has no body - #8485

Merged
leandrodamascena merged 2 commits into
aws-powertools:developfrom
Om-singhaI:fix-compress-body-less-response
Sep 24, 2026
Merged

leandrodamascena merged 2 commits into
aws-powertools:developfrom
Om-singhaI:fix-compress-body-less-response

Conversation

@Om-singhaI

Copy link
Copy Markdown
Contributor

Issue number: closes #8484

Summary

Changes

ResponseBuilder._compress sets Content-Encoding: gzip, converts a str body to bytes, then hands the body to zlib. A Response built without a body still has body=None at that point, so zlib raises TypeError: a bytes-like object is required, not 'NoneType'. The error escapes app.resolve() and the invocation fails, so API Gateway answers 502 instead of the 204 or 304 the route returned. Browsers and SDKs always send Accept-Encoding: gzip, so an affected route fails on every request.

It takes both compression enabled and an explicit Response with no body. Returning None or (None, 204) from a route is serialized to "null" before compression, so those never crashed. ALBResolver was already safe, since #8194 replaces a None body with "".

  • _compress now returns early when self.response.body is None, with a debug log, so Content-Encoding is not set on a response that has nothing to encode.
  • Nothing else changes. An empty string body is still compressed exactly as before, and so is every non empty body.
  • Four new functional tests in tests/functional/event_handler/required_dependencies/test_api_gateway.py: a compress=True route returning Response(204) and Response(304), Response(status_code=204, compress=True) on a route that does not set compress, and an exception handler returning a Response with no body under a compress=True route.

User experience

Before: a compress=True route returning Response(status_code=204) raises TypeError: a bytes-like object is required, not 'NoneType' and the client gets 502 Internal server error.

After: the same route returns 204 with a null body and no Content-Encoding header. 304 and status only exception handler responses behave the same way, and responses that do have a body are still gzipped.

Testing:

  • The four new tests fail on develop with TypeError: a bytes-like object is required, not 'NoneType' raised inside _compress, and pass here.
  • tests/functional/event_handler/required_dependencies/test_api_gateway.py: 99 passed. The 11 tests matching -k compress in that file all pass.
  • tests/functional/event_handler/required_dependencies: 435 passed, 1 skipped. tests/functional/event_handler/_pydantic: 437 passed.
  • ruff format --check aws_lambda_powertools tests examples: 1296 files already formatted. ruff check aws_lambda_powertools tests examples: all checks passed. Ruff 0.16.7, inside the range pyproject.toml pins.
  • mypy aws_lambda_powertools/event_handler/api_gateway.py: no new errors. The only two it reports are missing boto3 and botocore stubs in my local environment, in other modules, and they show up on develop too.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@Om-singhaI
Om-singhaI requested a review from a team as a code owner September 23, 2026 16:50
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 23, 2026
@codecov

codecov Bot commented Sep 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.80%. Comparing base (c8d9937) to head (e46fd0b).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8485   +/-   ##
========================================
  Coverage    96.79%   96.80%           
========================================
  Files          310      310           
  Lines        15531    15534    +3     
  Branches      1352     1353    +1     
========================================
+ Hits         15034    15037    +3     
  Misses         361      361           
  Partials       136      136           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Om-singhaI for finding and fixing this. The fix makes sense, and the tests cover the affected cases. Approved!

@mergify

mergify Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena
leandrodamascena merged commit fe1edc0 into aws-powertools:develop Sep 24, 2026
13 checks passed
@powertools-for-aws-oss-automation

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@boring-cyborg

boring-cyborg Bot commented Sep 24, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: compress=True raises TypeError on a Response with no body, so 204 and 304 return 502

2 participants