Skip to content
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

Firestore: CollectionReference.withConverter(converter).add(data) invokes converter.toFirestore twice #2173

Open
CaptainCodeman opened this issue Aug 19, 2024 · 0 comments
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@CaptainCodeman
Copy link

The toFirestore method of converter is called multiple times when adding a new document to a collection which can cause issues if the object is mutated as part of the conversion (it seems reasonable to assume it should only be called once).

Similar to this client-side issue: firebase/firebase-js-sdk#3742

Environment details

  • OS: macOS 14.6.1
  • Node.js version: 22.4.0
  • npm version: pnpm 9.7.1
  • @google-cloud/firestore version: 7.9.0

Steps to reproduce

Use the .add method on a collection ref with a converter, mutating the object:

const testConverter = {
  toFirestore(test) {
    test.hash = Blob.fromBase64String(test.hash)
    return test
  },
  fromFirestore(snapshot, options) {
    const data = snapshot.data(options)!;
    data.hash = data.hash.toBase64()
    return data
  }
};

const ref = await firestore
		.collection(`test`)
		.withConverter(testConverter)
		.add({ name: 'test', hash: 'some-base64-string-that-we-want-to-store-as-a-blob' })

The converter would work fine for regular set, and update methods, but .add causes .toFirestore to be called twice. Although it's a good idea for the converters to use immutable patterns which would avoid this, it's an easy mistake to just mutate the existing object which can cause runtime errors or data corruption and if nothing else is wasted extra work.

@CaptainCodeman CaptainCodeman added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Aug 19, 2024
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Aug 19, 2024
@wu-hui wu-hui self-assigned this Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants