Skip to content

Conversation

@arnabrahman
Copy link
Contributor

@arnabrahman arnabrahman commented Nov 10, 2025

Summary

Previously, we couldn't return an array as an event handler response. This PR fixes that.

Changes

  • update HandlerResponse type, replace JSONObject with JSONValue
  • update error handling function to handle the new type
  • Add unit tests to cover array response

Example:

import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { ScanCommand, DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';

const ddbDocClient = DynamoDBDocument.from(new DynamoDBClient());
const app = new Router();

app.get('/orders', async () => {
  const response = await ddbDocClient.send(
    new ScanCommand({
      TableName: 'OrdersWorkshop',
    })
  );
  if (response.Items && response.Items.length > 0) {
    return response.Items;
  }
  return { message: 'No orders found' };
});

export const lambdaHandler = (
  event: APIGatewayProxyEvent,
  context: Context
) => {
  app.resolve(event, context);
};

Issue number: closes #4593


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.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Nov 10, 2025
@boring-cyborg boring-cyborg bot added event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Nov 10, 2025
@arnabrahman arnabrahman marked this pull request as ready for review November 11, 2025 04:52
@svozza svozza changed the title fix(rest): allow event handler response to return array fix(event-handler): allow event handler response to return array Nov 11, 2025
@svozza
Copy link
Contributor

svozza commented Nov 11, 2025

Two very minor comments but other than that, this is pretty much good to go.

@arnabrahman arnabrahman requested a review from svozza November 11, 2025 12:27
@sonarqubecloud
Copy link

@svozza svozza merged commit eef92ca into aws-powertools:main Nov 11, 2025
34 checks passed
@svozza
Copy link
Contributor

svozza commented Nov 11, 2025

Great stuff @arnabrahman , merged now!

@arnabrahman arnabrahman deleted the 4593-handler-array-response branch November 11, 2025 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event-handler This item relates to the Event Handler Utility size/L PRs between 100-499 LOC tests PRs that add or change tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Unable to return an array in the event handler response

2 participants