Skip to content

fix(parser): API Gateway Envelopes handle non-JSON #3511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Jan 22, 2025

Summary

Changes

Please provide a summary of what's being changed

This PR refactors the API Gateway (REST) & API Gateway v2 (HTTP) envelopes so that they don't force the transform of the body field and thus can handle plain text and base64 binary-encoded bodies.

The body is now left as a string and it's up to the caller to set the appropriate transform in the Zod schema passed to the envelope, for example to

import { z } from 'zod';
import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';

const schema = z.object({
  message: z.string()
});

ApiGatewayEnvelope.parse(event, JSONStringified(schema));

If there's demand for it, in the future we can consider adding an helper for base64 decoding.

Additionally I standardized the error structure by always throwing a ParseError with the original error as cause:

new ParseError('Failed to parse API Gateway body', {
  cause: new ZodError([
    {
      code: 'invalid_type',
      expected: 'string',
      received: 'undefined',
      path: ['path'],
      message: 'Required',
    },
    {
      code: 'invalid_type',
      expected: 'object',
      received: 'null',
      path: ['body'],
      message: 'Expected object, received null',
    },
  ]),
})

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: closes #3512


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.

@dreamorosi dreamorosi self-assigned this Jan 22, 2025
@dreamorosi dreamorosi requested a review from a team January 22, 2025 16:58
@dreamorosi dreamorosi requested a review from a team as a code owner January 22, 2025 16:58
@boring-cyborg boring-cyborg bot added parser This item relates to the Parser Utility tests PRs that add or change tests labels Jan 22, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Jan 22, 2025

This comment was marked as outdated.

@github-actions github-actions bot added do-not-merge This item should not be merged need-issue This PR needs an issue before it can be reviewed/worked on further bug Something isn't working labels Jan 22, 2025
@dreamorosi dreamorosi removed do-not-merge This item should not be merged need-issue This PR needs an issue before it can be reviewed/worked on further labels Jan 22, 2025
Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for PR, and making the tests more explicit with ParseErrors.

@am29d am29d merged commit a4846af into main Jan 23, 2025
49 checks passed
@am29d am29d deleted the fix/api_gateway_envelopes branch January 23, 2025 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser This item relates to the Parser 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: API Gateway envelopes assumes message body is always a JSON
2 participants