Skip to content

Implements a destructuring compatability syntax for v1 style in v2#1832

Closed
inlined wants to merge 3 commits intomasterfrom
inlined.v2-compat
Closed

Implements a destructuring compatability syntax for v1 style in v2#1832
inlined wants to merge 3 commits intomasterfrom
inlined.v2-compat

Conversation

@inlined
Copy link
Copy Markdown
Member

@inlined inlined commented Mar 17, 2026

Description

V2 handlers that have a corresponding v1 handler will now be able to destructure the original event format.
This allows migration of function declarations without migration of business logic.

NOTE: You can do a YOLO migration with this if you rename your function "fooV2" so the CLI deletes your old one and creates your new one, but this is not a true hand-held migration. You may drop events in the interim. If you want to do a migration before tooling is supported you should do the following:

  1. Create a new v2 function signature that destructures the old params. Use it to call your v1 function
  2. Deploy. Wait a few minutes for any backlog in your v1 function to be processed
  3. Delete the v1 function and make its body the v2 function
  4. Deploy again

Code sample

Standalone feature

export const uppercase = onValueCreated("messages/{id}", ({snap, context}) => {
  myOldBusinessLogicThatExpectsAV1Payload(snap, context);
});

As a DIY migration tool

// Old
export const uppercase = functions.database.ref("/messages/{id}").onCreate((snap, context) => {
  // Does some thing with snap
});

// Intermediate:
export const uppercase = functions.database.ref("/messages/{id}").onCreate((snap, context) => {
  // Does some thing with snap
});

export const uppercaseV2 = onValueCreated("/messages/{id}", ({snap, context}) => {
  await uppercase.run(snap, context);
});

// Final:
export const uppercasev2 = onValueCreated("/messages/{id}", ({snap, context}) => {
  // original code from uppercase v1
});

@inlined inlined requested a review from shettyvarun268 March 17, 2026 20:22
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the migration path for Firebase Cloud Functions v1 users transitioning to v2. By implementing a new addV1Compat utility, v2 event handlers can now access event data using the familiar v1 event format, including properties like context and event-specific payloads (e.g., snapshot, change, message). This compatibility layer allows developers to update function declarations to v2 without immediately rewriting their core business logic, thereby reducing migration friction and potential errors.

Highlights

  • V1 Compatibility Utility: Introduced a new, generic addV1Compat utility function that dynamically adds V1-style compatibility properties (like context and event-specific data) as lazy-evaluated getters to V2 CloudEvent objects. This function is idempotent, ensuring properties are added only once.
  • Provider Integration: Integrated the addV1Compat function across various Cloud Functions for Firebase V2 providers, including Realtime Database, Firestore, Pub/Sub, Remote Config, Scheduler, Cloud Storage, and Cloud Tasks. This enables v2 handlers to destructure event objects using familiar v1 event formats.
  • Type System Enhancements: Updated event handler signatures and introduced V1Compat types to provide strong type inference and ensure correct usage of the new V1-compatible properties in v2 functions.
  • Comprehensive Testing: Added extensive test cases for each affected provider to thoroughly verify the correct behavior and data mapping of the V1-compatible getters, ensuring a robust migration path.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • spec/v2/compat.spec.ts
    • Renamed patchV1Compat to addV1Compat in tests.
    • Refactored tests to validate the new getter-based V1 compatibility mechanism.
    • Removed tests for unsupported event types and malformed Pub/Sub events, as the new addV1Compat is generic.
  • spec/v2/providers/database.spec.ts
    • Added a test suite to verify V1-compatible getters for Realtime Database onValueWritten events.
  • spec/v2/providers/firestore.spec.ts
    • Added test suites to verify V1-compatible getters for Firestore onOperation and onChangedOperation events.
  • spec/v2/providers/pubsub.spec.ts
    • Corrected MessagePublishedData instantiation in an existing test.
    • Added a test to ensure backward compatibility for user tests passing POJOs without V1 getters.
    • Added a test suite to verify V1-compatible getters for Pub/Sub onMessagePublished events.
  • spec/v2/providers/remoteConfig.spec.ts
    • Updated handler argument types in existing tests from 1 as any to {}.
    • Added a test suite to verify V1-compatible getters for Remote Config onConfigUpdated events.
  • spec/v2/providers/scheduler.spec.ts
    • Added a test suite to verify V1-compatible context for Scheduler onSchedule events.
  • spec/v2/providers/storage.spec.ts
    • Added a test suite to verify V1-compatible getters for Cloud Storage onObjectArchived events.
  • spec/v2/providers/tasks.spec.ts
    • Added a test suite to verify V1-compatible context for Cloud Tasks onTaskDispatched requests.
  • src/common/providers/tasks.ts
    • Added a context getter to the Request object for task dispatch handlers to provide V1 compatibility.
  • src/v2/compat.ts
    • Renamed patchV1Compat to addV1Compat.
    • Refactored the function to be a generic utility that adds V1 compatibility properties as lazy-evaluated getters to any CloudEvent object.
    • Removed specific Pub/Sub compatibility logic and related types (V1Context, PubSubCloudEvent, V1PubSubMessage).
    • Introduced V1Compat type for better type inference.
  • src/v2/index.ts
    • Removed the export of PubSubCloudEvent.
  • src/v2/providers/database.ts
    • Imported addV1Compat and V1Compat.
    • Added overloads to onValueWritten, onValueCreated, onValueUpdated, onValueDeleted to include V1Compat types in handler signatures.
    • Modified onChangedOperation and onOperation to use addV1Compat to inject V1-compatible context and change/snapshot getters.
  • src/v2/providers/firestore.ts
    • Imported addV1Compat and V1Compat.
    • Added overloads to onDocumentWritten, onDocumentCreated, onDocumentUpdated, onDocumentDeleted (and their WithAuthContext counterparts) to include V1Compat types.
    • Modified onOperation and onChangedOperation to use addV1Compat to inject V1-compatible context and snapshot/change getters.
  • src/v2/providers/pubsub.ts
    • Replaced patchV1Compat with addV1Compat and V1Compat.
    • Added overloads to onMessagePublished to include V1Compat types.
    • Modified the internal function to use addV1Compat to inject V1-compatible context and message getters.
  • src/v2/providers/remoteConfig.ts
    • Imported addV1Compat and V1Compat.
    • Added overloads to onConfigUpdated to include V1Compat types.
    • Modified the internal function to use addV1Compat to inject V1-compatible context and version getters.
  • src/v2/providers/scheduler.ts
    • Imported EventContext from v1/cloud-functions.
    • Added overloads to onSchedule to include context: EventContext in handler signatures.
    • Modified the internal function to define a context getter on the ScheduledEvent object.
  • src/v2/providers/storage.ts
    • Imported addV1Compat and V1Compat.
    • Added overloads to onObjectArchived, onObjectFinalized, onObjectDeleted, onObjectMetadataUpdated to include V1Compat types.
    • Modified onOperation to use addV1Compat to inject V1-compatible context and object getters.
  • src/v2/providers/tasks.ts
    • Imported TaskContext.
    • Added overloads to onTaskDispatched to include context: TaskContext in handler signatures.
Activity
  • The pull request introduces a new compatibility feature for v1 to v2 Cloud Functions migration.
  • New test cases have been added across multiple providers to ensure the V1 compatibility works as expected.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 introduces a significant and well-executed refactoring to implement a generic v1 compatibility layer for v2 functions, enabling easier migration. The core change is the new generic addV1Compat function, which replaces provider-specific patching logic. This greatly improves code structure and maintainability. The use of TypeScript overloads to support both v1 and v2 style handlers is a clever approach to maintain type safety for developers. The accompanying test changes are comprehensive and validate the new compatibility layer effectively. I have one minor suggestion to improve robustness in an edge case.

@inlined
Copy link
Copy Markdown
Member Author

inlined commented Mar 20, 2026

Broken into 7 PRs

@inlined inlined closed this Mar 20, 2026
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.

2 participants