Repository navigation
fix(pubsub): accept messages from requests without CloudEvent headers - #334
IzaakGough wants to merge 1 commit into
Conversation
functions-framework converts such requests itself, producing a message with only camelCase keys, an "@type" key and no subscription, which made on_message_published raise KeyError: 'publish_time'.
There was a problem hiding this comment.
Code Review
This pull request adds support for Pub/Sub push requests that lack CloudEvent headers by mapping camelCase keys to snake_case and making the subscription field optional. A test case is also added to verify this behavior. The review feedback suggests checking for the existence of camelCase keys before calling setdefault to prevent injecting None values, which could lead to downstream errors.
| message_dict.setdefault("message_id", message_dict.get("messageId")) | ||
| message_dict.setdefault("publish_time", message_dict.get("publishTime")) |
There was a problem hiding this comment.
Using message_dict.get() here will return None if the camelCase keys are missing. If both the snake_case and camelCase keys are absent (e.g., in a malformed request), setdefault will set the keys to None. This can lead to confusing downstream errors, such as a TypeError: argument of type 'NoneType' is not iterable when checking for . in publish_time on line 119, instead of a standard KeyError. Checking for the existence of the camelCase keys before calling setdefault avoids injecting None values.
| message_dict.setdefault("message_id", message_dict.get("messageId")) | |
| message_dict.setdefault("publish_time", message_dict.get("publishTime")) | |
| if "messageId" in message_dict: | |
| message_dict.setdefault("message_id", message_dict["messageId"]) | |
| if "publishTime" in message_dict: | |
| message_dict.setdefault("publish_time", message_dict["publishTime"]) |
Fixes #333.
When a Pub/Sub request reached an
on_message_publishedfunction without CloudEvent headers, the function failed withKeyError: 'publish_time'before the handler ran.This accepts the message that functions-framework builds for those requests, matching how the Node SDK reads it.
MessagePublishedData.subscriptionis nowstr | None. The request has no subscription on this path, and leaving the type asstrwould hide that.Tested with a unit test using functions-framework's conversion and with the issue's MRE locally. Not tested with a real Eventarc delivery.