Skip to content

fix(event_handlers): Bedrock kwargs and media type, AppSync registry and context, Router duplicates, Function URL base path, Depends with classes - #8629

Open
vishwakt wants to merge 8 commits into
aws-powertools:developfrom
vishwakt:fix/event-handlers-8584
Open

vishwakt wants to merge 8 commits into
aws-powertools:developfrom
vishwakt:fix/event-handlers-8584

Conversation

@vishwakt

Copy link
Copy Markdown
Contributor

Issue number: closes #8584

Summary

Changes

One fix plus one test per bug, in the order listed in the issue:

  1. Bedrock Agent kwargs: with enable_validation=False, every event parameter was injected as a kwarg, so a query parameter missing from the handler signature raised TypeError. _convert_matches_into_route_keys now injects only parameters matching the route path keys.
  2. Bedrock Agent response media type: a Response without a content type produced "responseBody": {"null": {...}}. The media type key now defaults to application/json.
  3. AppSync Events registry: registering a resolver with an invalid path warned and returned None, so the decorated function name was bound to None. The function is now returned unregistered.
  4. AppSync context leak: resolve() returned the exception handler result before clear_context(), so app.context leaked into the next invocation. Clearing now happens in a finally block. 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.
  5. Router duplicates: Router silently overwrote a duplicate route registration while the resolver warns. Router.route now emits the same warning.
  6. Function URL base path: _get_base_path compared request_context.http.method against the stage prefix instead of http.path. Unreachable today since Function URLs only use $default, fixed as a typo and covered by a direct test of the method.
  7. Depends with a class: 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.

User experience

Each repro from the issue snippet now behaves as expected: no TypeError for extra Bedrock parameters, a valid media type key, the decorated AppSync Events function stays usable, app.context is empty after an exception handler runs, duplicate Router routes warn, and a class dependency can receive Request or nested Depends.

Seven tests added, one per bug, each verified to fail before its fix. Full tests/functional/event_handler and tests/unit/event_handler suites 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.

…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
@sonarqubecloud

Copy link
Copy Markdown

@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 @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.

This branch has not been deployed

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

Labels

event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

2 participants