Skip to content

Bug: APIGatewayProxyEventSchema incorrect schemas #2461

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

Closed
blytheaw opened this issue May 1, 2024 · 5 comments · Fixed by #2465
Closed

Bug: APIGatewayProxyEventSchema incorrect schemas #2461

blytheaw opened this issue May 1, 2024 · 5 comments · Fixed by #2465
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility

Comments

@blytheaw
Copy link
Contributor

blytheaw commented May 1, 2024

Expected Behavior

Valid API Gateway Proxy request (REST API) with no query string parameters and no body should pass validation when using Powertools Parser and validating against the entire APIGatewayProxyEventSchema

Current Behavior

Valid API Gateway Proxy request (REST API) with no query string parameters and no body is failling validation when using Powertools Parser and validating against the entire APIGatewayProxyEventSchema

Code snippet

import type { Context } from 'aws-lambda';
import type { APIGatewayProxyEvent } from '@aws-lambda-powertools/parser/types';

import { parser } from '@aws-lambda-powertools/parser/middleware';
import { APIGatewayProxyEventSchema } from '@aws-lambda-powertools/parser/schemas';
import middy from '@middy/core';
import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

const lambdaHandler = async (event: APIGatewayProxyEvent, _context: Context): Promise<void> => {
    logger.info('Event', { event });
};

export const handler = middy(lambdaHandler).use(
  parser({ schema: APIGatewayProxyEventSchema })
);

Steps to Reproduce

  1. Create a Lambda handler that uses Powertools Parser middy middleware and passes the default APIGatewayProxyEventSchema from Powertools Parser package as the schema to validate
  2. Configure the Lambda as a proxy integration endpoint from API Gateway v1 (REST API)
  3. Hit the endpoint with any valid request that doesn't contain query string parameters or a request body
  4. Observe Parser/Zod error for invalid fields: queryStringParameters, multiValueQueryStringParameters, and body

Possible Solution

Change the schema to make body, queryStringParameters, and multiValueQueryStringParameters nullable.

This seems to be consistent with @types/aws-lambda and the AWS docs.

These are also the values I have been getting in practice when making actual requests to Lambda and inspecting the event.

I'm happy to submit a PR with these changes if this solution seems reasonable.

Powertools for AWS Lambda (TypeScript) version

2.1.0-beta

AWS Lambda function runtime

18.x

Packaging format used

npm

Execution logs

The parser error


[
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "null",
    "path": [
      "queryStringParameters"
    ],
    "message": "Expected object, received null"
  },
  {
    "code": "invalid_type",
    "expected": "object",
    "received": "null",
    "path": [
      "multiValueQueryStringParameters"
    ],
    "message": "Expected object, received null"
  },
  {
    "code": "invalid_type",
    "expected": "string",
    "received": "null",
    "path": [
      "body"
    ],
    "message": "Expected string, received null"
  }
]
@blytheaw blytheaw added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels May 1, 2024
Copy link

boring-cyborg bot commented May 1, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

@dreamorosi
Copy link
Contributor

Hi @blytheaw, thank you for taking the time to try Parser and open an issue.

This is a mistake on our side, these fields should be nullable.

If the offer of opening a PR is still available, I'll take you up on it and help you get the PR merged.

@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility and removed triage This item has not been triaged by a maintainer, please wait labels May 1, 2024
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) May 1, 2024
@blytheaw
Copy link
Contributor Author

blytheaw commented May 1, 2024

@dreamorosi Sure, I'll open a PR. Thanks!

Copy link
Contributor

github-actions bot commented May 2, 2024

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels May 2, 2024
Copy link
Contributor

This is now released under v2.1.1 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels May 14, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility
Projects
2 participants