Repository navigation
fix(event_handlers): Bedrock kwargs and media type, AppSync registry and context, Router duplicates, Function URL base path, Depends with classes - #8629
Conversation
…a type With validation off, every event parameter was injected as a kwarg, so a query parameter missing from the handler signature raised TypeError. Only parameters matching the route path keys are injected now. A Response without a content type produced a 'null' media type key in responseBody. It defaults to application/json now.
…th is invalid The registry returned None after warning about an invalid path, so the decorated function name was bound to None. The function is returned unregistered instead.
resolve() returned the exception handler result before clear_context(), so app.context leaked into the next invocation. Clearing now happens in a finally block, keeping the coroutine exemption.
Router silently overwrote a duplicate route registration, last one wins, while the resolver warns. Router now emits the same warning.
…base path _get_base_path compared request_context.http.method against the stage prefix instead of http.path.
Depends(SomeClass) read annotations with get_type_hints on the class, which returns class-level attributes, so Request injection and nested Depends declared on __init__ were never found. Hints are now read from __init__ when the dependency is a class. Closes aws-powertools#8584
|
leandrodamascena
left a comment
There was a problem hiding this comment.
Thanks @vishwakt, all seven hold up. I ran the AppSync context one on a real Lambda with three warm invocations: on develop each invocation started with the context left by the previous one, with your finally it starts empty every time while the exception handler still sees the current invocation's context. The Bedrock kwargs and media type, the Events registry, the Router warning and the class-based Depends all behave as the issue asked.
Your branch conflicted with tests I had added to test_depends.py in the meantime, so I merged develop into it keeping both sets and formatted the file. Nothing for you to do.
Approving. This goes out in 3.39.0, so it will sit here for a while before I merge it. I'll note in the release notes that AppSyncResolver now also clears the context when an exception propagates, since that is visible. You'll be credited there.



Issue number: closes #8584
Summary
Changes
One fix plus one test per bug, in the order listed in the issue:
enable_validation=False, every event parameter was injected as a kwarg, so a query parameter missing from the handler signature raisedTypeError._convert_matches_into_route_keysnow injects only parameters matching the route path keys.Responsewithout a content type produced"responseBody": {"null": {...}}. The media type key now defaults toapplication/json.None, so the decorated function name was bound toNone. The function is now returned unregistered.resolve()returned the exception handler result beforeclear_context(), soapp.contextleaked into the next invocation. Clearing now happens in afinallyblock. The coroutine exemption from Bug: Router context not being available when using AppSync with async resolvers #5290 is preserved, and an unhandled exception now also clears the context, which matches the documented behavior of clearing after every invocation.Routersilently overwrote a duplicate route registration while the resolver warns.Router.routenow emits the same warning._get_base_pathcomparedrequest_context.http.methodagainst the stage prefix instead ofhttp.path. Unreachable today since Function URLs only use$default, fixed as a typo and covered by a direct test of the method.Depends(SomeClass)read annotations withget_type_hintson the class, which returns class-level attributes, soRequestinjection and nestedDependsdeclared on__init__were never found. Hints are now read from__init__when the dependency is a class.User experience
Each repro from the issue snippet now behaves as expected: no
TypeErrorfor extra Bedrock parameters, a valid media type key, the decorated AppSync Events function stays usable,app.contextis empty after an exception handler runs, duplicateRouterroutes warn, and a class dependency can receiveRequestor nestedDepends.Seven tests added, one per bug, each verified to fail before its fix. Full
tests/functional/event_handlerandtests/unit/event_handlersuites pass (1198 tests), ruff and mypy clean on the changed files.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.