Skip to content

fix(pubsub): assign Message instance back to event.data.message#1864

Merged
shettyvarun268 merged 2 commits intofirebase:masterfrom
swematheus:master
Apr 8, 2026
Merged

fix(pubsub): assign Message instance back to event.data.message#1864
shettyvarun268 merged 2 commits intofirebase:masterfrom
swematheus:master

Conversation

@swematheus
Copy link
Copy Markdown
Contributor

@swematheus swematheus commented Apr 7, 2026

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);
});

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.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

@shettyvarun268 shettyvarun268 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for identifying this and submitting the fix. The solution looks solid and the tests are comprehensive. Approving.

@swematheus
Copy link
Copy Markdown
Contributor Author

swematheus commented Apr 8, 2026

Hi @shettyvarun268 ! Thank you for the review 🦾 🚀

I noticed this run:
https://github.com/firebase/firebase-functions/actions/runs/24101305435/job/70466651181?pr=1864

It has some lint warnings. Is it okay if I fix them using lint --fix? Or should we ignore them for now? 😄


Edit: Pipeline fixed after commit: 1607c32

@shettyvarun268 shettyvarun268 added this pull request to the merge queue Apr 8, 2026
Merged via the queue into firebase:master with commit aafbece Apr 8, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] onMessagePublished: event.data.message.json returns undefined in 7.2.3

3 participants