Skip to content

feat: add toJson method to Timestamp#290

Open
OutdatedGuy wants to merge 2 commits into
firebase:mainfrom
OutdatedGuy:feat/encodable
Open

feat: add toJson method to Timestamp#290
OutdatedGuy wants to merge 2 commits into
firebase:mainfrom
OutdatedGuy:feat/encodable

Conversation

@OutdatedGuy

Copy link
Copy Markdown
Contributor

Related Issues

fixes #289

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).

  • I have updated the CHANGELOG.md of the relevant packages.
    Changelog files must be edited under the form:

    ## Unreleased fix/major/minor
    
    - Description of your change. (thanks to @yourGithubId)
  • If this contains new features or behavior changes,
    I have updated the documentation to match those changes.

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

Copy link
Copy Markdown
Contributor

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 adds a toJson method to the Timestamp class to make it encodable, along with corresponding unit tests and a changelog update. The reviewer recommends implementing a symmetric fromJson factory constructor, changing the return type of toJson to the idiomatic Map<String, dynamic>, and adding unit tests to verify the deserialization logic.

Comment thread packages/google_cloud_firestore/lib/src/timestamp.dart
Comment thread packages/google_cloud_firestore/test/timestamp_test.dart
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@0e5e8ae). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #290   +/-   ##
=======================================
  Coverage        ?   75.19%           
=======================================
  Files           ?      105           
  Lines           ?     6873           
  Branches        ?        0           
=======================================
  Hits            ?     5168           
  Misses          ?     1705           
  Partials        ?        0           
Flag Coverage Δ
unittests 75.19% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@demolaf demolaf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should be in the firebase_functions package itself, specifically CallableResponse in callable.dart https://github.com/firebase/firebase-functions-dart/blob/main/lib/src/https/callable.dart#L419-L457.

Also, Timestamp.toJson() should not be used internally since the bundle serialization format requires a different set of map keys (seconds/nanos).

@OutdatedGuy

Copy link
Copy Markdown
Contributor Author

hi @demolaf, the Timestamp in node.js is natively encodable without depending on firebase-functions

import { Timestamp } from "firebase-admin/firestore";

const now = Timestamp.now();

console.log(JSON.stringify(now)); // {"_seconds":1782405004,"_nanoseconds":354000000}

Having a native toJson method would be better for custom dart backends not using firebase cloud functions.


Also, the encode method you linked seems to used only for streaming responses, not for CallableResult.

CallableResult uses dart:convert's jsonEncode method:

https://github.com/firebase/firebase-functions-dart/blob/bf539c4863b2ba90c8d6e0ef166119ce083dca0e/lib/src/https/callable.dart#L45-L49

@demolaf

demolaf commented Jun 26, 2026

Copy link
Copy Markdown
Member

@Lyokone @lahirumaramba can I get your thoughts on this?

my main concern here is Timestamp.toJson() if added can be called via jsonEncode which is a risk e.g. BundleMetadata https://github.com/firebase/firebase-admin-dart/blob/main/packages/google_cloud_firestore/lib/src/bundle.dart#L61-L72 uses Timestamp but in the future can be mistaken and Timestamp.toJson() used.

firebase functions node never had this concern because it doesn't declare a Timestamp.toJson().

@OutdatedGuy encode is not only for streaming responses, see node reference https://github.com/firebase/firebase-functions/blob/master/src/common/providers/https.ts#L919-L927 it's job here is to convert types into json compatible values/throws for incompatible json values and that's actually a bug that it is not being called in CallableResult.toResponse().

the idea here is to let firebase functions dart handle functions specific work instead of firebase admin since it can't defend against node's default behavior.

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.

Cannot return Timestamp from Cloud Functions as response

3 participants