fix(pubsub): assign Message instance back to event.data.message#1864
fix(pubsub): assign Message instance back to event.data.message#1864shettyvarun268 merged 2 commits intofirebase:masterfrom
Conversation
The refactor in 01ed20c that replaced patchV1Compat with addV1Compat dropped the mutation of event.data.message, leaving it as a raw POJO without the .json getter. This caused event.data.message.json to return undefined instead of the parsed JSON payload. Add the missing assignment and regression tests that exercise the POJO-to-Message wrapping path the runtime actually delivers.
There was a problem hiding this comment.
Code Review
This pull request ensures that event.data.message in the Pub/Sub onMessagePublished handler is consistently wrapped as a Message instance, even when the input is a raw POJO. Comprehensive tests were added to verify field preservation, the .json getter functionality, and error handling for malformed events. I have no feedback to provide.
shettyvarun268
left a comment
There was a problem hiding this comment.
Thank you so much for identifying this and submitting the fix. The solution looks solid and the tests are comprehensive. Approving.
|
Hi @shettyvarun268 ! Thank you for the review 🦾 🚀 I noticed this run: It has some lint warnings. Is it okay if I fix them using Edit: Pipeline fixed after commit: 1607c32 |
Resolves #1863
Description
Fixes a regression introduced in 01ed20c ("Destructuring API pt 1: foundation & pubsub (#1837)") where event.data.message.json returns undefined for onMessagePublished triggers.
When patchV1Compat was replaced with the generic addV1Compat utility, the line that mutated event.data.message — replacing the raw POJO with a Message class instance — was dropped. The new code creates a Message instance in a local variable (v2Message) but never assigns it back to event.data.message, so consumers accessing the canonical V2 path get a plain object without the .json getter.
The fix: One line added at src/v2/providers/pubsub.ts:350 to assign the Message instance back:
(pubsubData as any).message = v2Message;
Tests
Added 6 regression tests in a new "event.data.message wrapping (POJO -> Message instance)" describe block that exercise the POJO-to-Message conversion path (the path the Cloud Functions runtime actually uses). The existing tests always pre-constructed a Message instance, so they never caught this.
Path
event.data.message.json (V2)
event.message.json (V1 compat)
Code sample
import { onMessagePublished } from "firebase-functions/v2/pubsub";
export const myFunction = onMessagePublished("my-topic", (event) => {
// Before fix: undefined. After fix: { hello: "world" }
const payload = event.data.message.json;
console.log(payload);
});