Repository navigation
fix(event_handler): fill request context from ASGI scope in HttpResolverLocal - #8841
Conversation
…verLocal HttpProxyEvent.from_asgi only copied method, path, headers, body and query string, so the request context fell back to a stub with only stage, requestId and http method/path, and HttpProxyEvent had no request_context property. Handlers reading source_ip or user_agent from the request context raised AttributeError under uvicorn. Fill the request context from the ASGI scope: the source IP from scope client and the user agent from the user-agent header, and expose it through a request_context property on the local event. Refs aws-powertools#8802 Signed-off-by: Ajey Awasthi <115537579+dropps07@users.noreply.github.com>
|
Hi @leandrodamascena, the PR is ready for review whenever you have a moment. Please let me know if it needs anything added, removed or changed. |
|
Hi @leandrodamascena @hjgraca , this PR is still waiting for its first review. It fills the ASGI request context from the scope in HttpResolverLocal and is mergeable with no conflicts. Could you please take a look when you get a chance. Thanks. |
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks @dropps07! Filling only what the ASGI scope can actually tell us, and leaving the Lambda-specific fields alone, is the scope I had in mind. I ran it under uvicorn and a handler reading source_ip and user_agent goes from a 500 on develop to the right values here. Goes out in 3.42.0, no need to rebase, you'll be in the release notes. @dacevedo12 this should cover what your handlers read; shout if something is missing.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #8841 +/- ##
===========================================
- Coverage 97.15% 97.14% -0.01%
===========================================
Files 327 327
Lines 16813 16828 +15
Branches 1539 1539
===========================================
+ Hits 16334 16348 +14
- Misses 336 337 +1
Partials 143 143 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
5cd5491
into
aws-powertools:develop



Issue number: closes #8802
Summary
Changes
Fill request_context from the ASGI scope so handlers running under uvicorn read the same fields they read in Lambda. Fixes #8802.
from_asgi fills the source IP from the scope client address and the user agent from the user-agent header, and exposes it through a request_context property on HttpProxyEvent. Lambda-specific fields stay as they are today.
The v2.0 local event shape option from the issue is not included. Leandro's scope for this issue is the request_context fill only. Happy to follow up on the event shape option separately if he wants it.
User experience
Before this change, handlers running under uvicorn that read the source IP or user agent from the request context raised AttributeError, because the request context was not filled from the ASGI scope. After this change, those handlers read the same fields they read in Lambda, with the source IP taken from the ASGI scope client address and the user agent taken from the user-agent header.
Verification
Added 2 new tests through HttpResolverLocal with a realistic ASGI scope. All 69 HTTP resolver tests pass. Negative control: both new tests fail on unfixed code with AttributeError, the exact bug from the issue. Ruff format check, ruff check and mypy are clean on the changed file. Not verified: no live uvicorn run and no full make pr run.
Acknowledgement: This work was AI-assisted. I verified it myself by running the repo's pytest suite for the HTTP resolver, plus ruff and mypy on the changed file. I did not run a live uvicorn server or the full make pr pipeline.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.