Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

Fix HTTP API v2 event sending null instead of omitting empty fields (#1163)

ryan-bennett59e78b5357ff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6523e746..cc57dad2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **API Gateway v2 — `queryStringParameters`/`body` sent as `null` instead of omitted** — the HTTP API v2 Lambda proxy event included `queryStringParameters`/`body` with a `null` value when a request had no query string or no payload, rather than omitting the key entirely as real AWS does. Strict event-shape validators (e.g. AWS Lambda Powertools' `isAPIGatewayProxyEventV2`) accept `undefined` or a proper value but reject `null`, so every bodyless or querystring-less request against such a validator failed with an unhandled `InvalidEventError`, even though the invocation itself succeeded and the Lambda's own business logic never ran. Contributed by @ryan-bennett.
+
## [1.4.5] — 2026-07-22
### Added
diff --git a/ministack/services/apigateway.py b/ministack/services/apigateway.py
index a45f01a0..74917750 100644
--- a/ministack/services/apigateway.py
+++ b/ministack/services/apigateway.py
@@ -1045,7 +1045,6 @@ async def _invoke_lambda_proxy(
"rawPath": path,
"rawQueryString": raw_qs,
"headers": dict(headers),
- "queryStringParameters": qs,
"requestContext": {
"accountId": get_account_id(),
"apiId": api_id,
@@ -1064,9 +1063,21 @@ async def _invoke_lambda_proxy(
"timeEpoch": int(time.time() * 1000),
},
"pathParameters": path_params,
- "body": req_body,
"isBase64Encoded": req_is_base64,
}
+ # Real AWS omits body entirely for a bodyless request rather than sending
+ # it as null — same isAPIGatewayProxyEventV2 rejection as
+ # queryStringParameters below (undefined/a string are accepted, null is
+ # not), so this broke every GET/DELETE-style request with no payload.
+ if req_body is not None:
+ event["body"] = req_body
+ # Real AWS omits queryStringParameters entirely when there is no query
+ # string, rather than sending it as null — strict event-shape validators
+ # (e.g. Powertools' isAPIGatewayProxyEventV2) accept undefined/a record
+ # but reject null, so a present-but-null key fails every request lacking
+ # a query string.
+ if qs:
+ event["queryStringParameters"] = qs
if authorizer_claims is not None:
event["requestContext"]["authorizer"] = {
"jwt": {
diff --git a/tests/test_apigatewayv2.py b/tests/test_apigatewayv2.py
index 7482d49b..080acd52 100644
--- a/tests/test_apigatewayv2.py
+++ b/tests/test_apigatewayv2.py
@@ -971,6 +971,61 @@ def test_apigw_query_params_and_headers_in_event(apigw, lam):
lam.delete_function(FunctionName=fname)
+def test_apigw_v2_event_omits_null_fields_when_absent(apigw, lam):
+ """Real AWS omits queryStringParameters/body from the v2 proxy event
+ entirely when there is no query string / no request body, rather than
+ including the key with a null value. Strict event-shape validators (e.g.
+ AWS Lambda Powertools' isAPIGatewayProxyEventV2) accept `undefined`
+ (Python: key absent) or a proper value, but reject `null` — a
+ present-but-null key broke every bodyless/querystring-less request
+ against such a validator, even though the invocation itself succeeded.
+
+ dict.get() can't distinguish an absent key from one explicitly set to
+ None, so this checks for key presence via `in` rather than the value.
+ """
+ import urllib.request as _urlreq
+ import uuid as _uuid
+
+ fname = f"intg-nullfields-{_uuid.uuid4().hex[:8]}"
+ code = (
+ "import json\n"
+ "def handler(event, context):\n"
+ " return {'statusCode': 200, 'body': json.dumps({\n"
+ " 'hasQs': 'queryStringParameters' in event,\n"
+ " 'hasBody': 'body' in event,\n"
+ " })}\n"
+ )
+ lam.create_function(
+ FunctionName=fname,
+ Runtime="python3.12",
+ Role=_LAMBDA_ROLE,
+ Handler="index.handler",
+ Code={"ZipFile": _make_zip(code)},
+ )
+ api_id = apigw.create_api(Name=f"nullfields-api-{fname}", ProtocolType="HTTP")["ApiId"]
+ int_id = apigw.create_integration(
+ ApiId=api_id,
+ IntegrationType="AWS_PROXY",
+ IntegrationUri=f"arn:aws:lambda:us-east-1:000000000000:function:{fname}",
+ PayloadFormatVersion="2.0",
+ )["IntegrationId"]
+ apigw.create_route(ApiId=api_id, RouteKey="GET /nofields", Target=f"integrations/{int_id}")
+ apigw.create_stage(ApiId=api_id, StageName="$default")
+
+ try:
+ url = f"http://{api_id}.execute-api.localhost:{_EXECUTE_PORT}/$default/nofields"
+ req = _urlreq.Request(url, method="GET")
+ req.add_header("Host", f"{api_id}.execute-api.localhost:{_EXECUTE_PORT}")
+ resp = _urlreq.urlopen(req)
+ assert resp.status == 200
+ body = json.loads(resp.read())
+ assert body["hasQs"] is False
+ assert body["hasBody"] is False
+ finally:
+ apigw.delete_api(ApiId=api_id)
+ lam.delete_function(FunctionName=fname)
+
+
def test_apigw_raw_query_string_percent_encoded(apigw, lam):
"""rawQueryString must stay percent-encoded like AWS: a space in a value comes
back as %20, not a literal space (which breaks lambda_http / http::Uri) — #1035."""