fix(event_handler): skip compression when the response has no body - #8485
leandrodamascena merged 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks @Om-singhaI for finding and fixing this. The fix makes sense, and the tests cover the affected cases. Approved!
|
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. |
|
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |



Issue number: closes #8484
Summary
Changes
ResponseBuilder._compresssetsContent-Encoding: gzip, converts astrbody tobytes, then hands the body tozlib. AResponsebuilt without a body still hasbody=Noneat that point, sozlibraisesTypeError: a bytes-like object is required, not 'NoneType'. The error escapesapp.resolve()and the invocation fails, so API Gateway answers 502 instead of the 204 or 304 the route returned. Browsers and SDKs always sendAccept-Encoding: gzip, so an affected route fails on every request.It takes both compression enabled and an explicit
Responsewith no body. ReturningNoneor(None, 204)from a route is serialized to"null"before compression, so those never crashed.ALBResolverwas already safe, since #8194 replaces aNonebody with""._compressnow returns early whenself.response.body is None, with a debug log, soContent-Encodingis not set on a response that has nothing to encode.tests/functional/event_handler/required_dependencies/test_api_gateway.py: acompress=Trueroute returningResponse(204)andResponse(304),Response(status_code=204, compress=True)on a route that does not setcompress, and an exception handler returning aResponsewith no body under acompress=Trueroute.User experience
Before: a
compress=Trueroute returningResponse(status_code=204)raisesTypeError: 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-Encodingheader. 304 and status only exception handler responses behave the same way, and responses that do have a body are still gzipped.Testing:
developwithTypeError: 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 compressin 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 rangepyproject.tomlpins.mypy aws_lambda_powertools/event_handler/api_gateway.py: no new errors. The only two it reports are missingboto3andbotocorestubs in my local environment, in other modules, and they show up ondeveloptoo.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.