Skip to content

Bug: JSON serialization should be the last operation before returning a response from an event handler #3391

Closed
@rubenfonseca

Description

@rubenfonseca

Expected Behaviour

Middleware writers should access the original return of the handler, before serialization happens.

Current Behaviour

When writing your own middleware, you only get access to a serialized version of the handler response, and not the original. This introduces challenges when you want to manipulate the original response.

Code snippet

class MyMiddleware(BaseMiddlewareHandler):
    def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) -> Response:
        # Call the handler by calling the next middleware
        response = next_middleware(app)

        response.body # this should be the original response, not a serialized version

Possible Solution

Move the serialization as the last operation to be done before returning, after running all the middlewares.

Steps to Reproduce

See code snippet above

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.7

Packaging format used

PyPi

Debugging logs

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Shipped

Relationships

None yet

Development

No branches or pull requests

Issue actions