Skip to content

fix(parser): SNS Envelope handles non-JSON #3506

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 3 commits into from
Jan 22, 2025
Merged

fix(parser): SNS Envelope handles non-JSON #3506

merged 3 commits into from
Jan 22, 2025

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Jan 21, 2025

Summary

Changes

Please provide a summary of what's being changed

This PR updates the SNSEnvelope built-in envelope so that it's able to handle non JSON-stringified payloads in the Message field.

Before this PR the envelope would always assume messages are JSON-stringified objects and attempt to transform them after having parsed them. This prevented customers from working with payloads that are simple strings, i.e.

{
  "Records": [
    {
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:us-east-2:123456789012:sns-la ...",
      "EventSource": "aws:sns",
      "Sns": {
        "SignatureVersion": "1",
        "Timestamp": "2019-01-02T12:45:07.000Z",
        "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==",
        "SigningCertUrl": "https://sns.us-east-2.amazonaws.com/SimpleNotification",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "Message": "Hello from SNS!",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        },
        "Type": "Notification",
        "UnsubscribeUrl": "https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe",
        "TopicArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda",
        "Subject": "TestInvoke"
      }
    }
  ]
}

The new implementation of the envelope moves the parsing in its own implementation and removes the usage of the Envelope abstraction, as well as leaving the choice of adding a transform to customers using it, i.e.

SnsEnvelope.safeParse(
  event,
  JSONStringified(
    z.object({
      foo: z.string(),
    })
  )
);

Additionally, similar to what done in other recent PRs I have also improved the error messaging so that it includes the full path of the error and also made it so the Records array must contain at least one item. Below an example of the new error:

new ParseError('Failed to parse SNS messages at indexes 0, 1', {
  cause: new ZodError([
    {
      code: 'custom',
      message: 'Invalid JSON',
      path: ['Records', 0, 'Sns', 'Message'],
    },
    {
      code: 'custom',
      message: 'Invalid JSON',
      path: ['Records', 1, 'Sns', 'Message'],
    },
  ]),
});

The PR also moves out of the SNS envelope the SnsSqsEnvelope, which despite the name, is actually a SQS envelope that wraps SNS messages. For this reason I had to also move the unit tests. A more thorough refactor will come in a future PR when I work on the SQS envelopes.

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

Issue number: fixes #3266


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 21, 2025
@dreamorosi dreamorosi requested a review from a team January 21, 2025 17:35
@dreamorosi dreamorosi requested a review from a team as a code owner January 21, 2025 17:35
@boring-cyborg boring-cyborg bot added parser This item relates to the Parser Utility tests PRs that add or change tests labels Jan 21, 2025
@pull-request-size pull-request-size bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Jan 21, 2025
@github-actions github-actions bot added the bug Something isn't working label Jan 21, 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 the PR! The code duplication is expected, since we pull back some of the code from Envelope.

@am29d am29d self-requested a review January 22, 2025 10:39
@am29d am29d merged commit 4d7f05f into main Jan 22, 2025
38 checks passed
@am29d am29d deleted the fix/sns_envelope branch January 22, 2025 10:39
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/XL PRs between 500-999 LOC, often PRs that grown with feedback tests PRs that add or change tests
Projects
Development

Successfully merging this pull request may close these issues.

Bug: SNS envelopes assumes data is always a JSON
2 participants