Skip to content

[Feat] Firestore SSR to/from JSON for data types #8928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

DellaBitta
Copy link
Contributor

@DellaBitta DellaBitta commented Apr 15, 2025

Discussion

Add support for toJSON and fromJSON for Firestore data types. This is part of the Firestore SSR data serialization feature, and this PR points to that feature branch, not main.

Testing

Added unit tests for all of the new methods.

API Changes

Added toJSON() and fromJSON() methods to the following classes:

  • Bytes
  • DocumentReference
  • GeoPoint
  • Timestamp
  • VectorValue

Copy link

changeset-bot bot commented Apr 15, 2025

⚠️ No Changeset found

Latest commit: e487f40

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

Vertex AI Mock Responses Check ⚠️

A newer major version of the mock responses for Vertex AI unit tests is available. update_vertexai_responses.sh should be updated to clone the latest version of the responses: v10.0

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 15, 2025

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 15, 2025

@DellaBitta DellaBitta marked this pull request as ready for review April 17, 2025 17:53
@DellaBitta DellaBitta requested review from a team as code owners April 17, 2025 17:53
@DellaBitta DellaBitta changed the title [Feat] WIP - SSR sub feature - Firestore to/from JSON DataTypes [Feat] Firestore SSR to/from JSON for data types Apr 18, 2025
Copy link
Contributor

@MarkDuckworth MarkDuckworth left a comment

Choose a reason for hiding this comment

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

Generally I think this looks okay. I gave some specific comments. I'm also a little concerned about the redundancy in fromJSON. I think we could abstract that out. I have a snippet of code that I will share over chat.

@@ -91,4 +91,40 @@ export class Bytes {
isEqual(other: Bytes): boolean {
return this._byteString.isEqual(other._byteString);
}

/** Returns a JSON-serializable representation of this `Bytes` instance. */
Copy link
Contributor

Choose a reason for hiding this comment

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

These descriptions look good, but do we also need an @returns ... tag?

error = `json missing required field: ${key}`;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const value = (json as any)[key];
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of const value = (json as any)[key];

You could just do

      if (!(key in json)) {
        error = `json missing required field: ${key}`;
      } else {
        json[key] ...
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, this still produces a compilation error:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.ts(7053)

Gemini suggested other ways to fix this but they seemed even more compilcated.

if (typeof value !== 'string') {
error = `json field 'type' must be a string.`;
break;
} else if (value !== 'firestore/geopoint/1.0') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we should have this json have a version number independent of the SDK version.

If they are independent, that means JSON can be deserialized and reserialized across SDK versions... Maybe that's okay.

But if they are the same, that means the same SDK must be used for serialization and deserialization, which would also free us up to make changes more easily

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We spoke about this offline.

@DellaBitta
Copy link
Contributor Author

Accidentally pushed these changes directly into the Feature PR. Closing this for now.

@DellaBitta DellaBitta closed this Apr 24, 2025
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.

3 participants