Skip to content

Bug: APIGatewayProxyEventSchema incorrect schemas #2461

Closed
@blytheaw

Description

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"
  }
]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedparserThis item relates to the Parser Utility

Type

No type

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions