Skip to content

fix(firestore): call toFirestore() once in CollectionReference.add() - #9924

Open
babu-ch wants to merge 2 commits into
googleapis:mainfrom
babu-ch:fix/firestore-add-converter-once
Open

babu-ch wants to merge 2 commits into
googleapis:mainfrom
babu-ch:fix/firestore-add-converter-once

Conversation

@babu-ch

@babu-ch babu-ch commented Nov 20, 2026

Copy link
Copy Markdown

Fixes #7450 🦕

CollectionReference.add() called converter.toFirestore() to validate the data, then passed the original data to DocumentReference.create(), which calls toFirestore() again in WriteBatch.create(). A converter that mutates its input (as in the issue) breaks on the second call.

WriteBatch.create() already runs the same validateDocumentData('data', ...) on the converted data synchronously, so this change removes the conversion and validation from add(). add() still throws the same error synchronously for invalid data.

The second commit only applies the formatting the repository linter requires for collection-reference.ts (it fails prettier/prettier on main once the file is changed).

Tests

  • calls toFirestore() once for CollectionReference.withConverter().add(): fails without this change (expected 2 to equal 1)

  • validates the converted data in CollectionReference.withConverter().add(): checks that invalid converted data is still rejected synchronously with the same message (passes before and after)

  • Make sure to open an issue as a bug/issue before writing your code

  • Ensure the tests and linter pass

  • Code coverage does not decrease (if any source code was changed)

  • Appropriate docs were updated (if necessary)

@product-auto-label product-auto-label Bot added the api: firestore Issues related to the Firestore API. label Nov 20, 2026

@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 removes redundant validation and conversion logic from CollectionReference.add() to prevent toFirestore() from being called twice, delegating the operation to documentRef.create(data). It also adds unit tests to verify that toFirestore() is called only once and that validation still behaves as expected. The feedback suggests importing DocumentData from the local source path (../src) instead of @google-cloud/firestore in the test file to ensure tests run against the local development code rather than the installed package in node_modules.

Comment on lines +19 to 21
import {DocumentData} from '@google-cloud/firestore';

import {DocumentReference, Firestore, setLogFunction} from '../src';

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.

medium

Importing DocumentData directly from @google-cloud/firestore in the test file can cause the tests to run against the installed/published version of the package in node_modules instead of the local development source code. This can lead to stale code being tested or unexpected test behavior. Please import DocumentData from the local ../src path instead, combining it with the existing imports.

Suggested change
import {DocumentData} from '@google-cloud/firestore';
import {DocumentReference, Firestore, setLogFunction} from '../src';
import {DocumentData, DocumentReference, Firestore, setLogFunction} from '../src';

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

../src does not export DocumentData, so this suggestion fails to compile (TS2614: Module '"../src"' has no exported member 'DocumentData'). The import is type-only and is erased from the compiled test, and @google-cloud/firestore resolves to this package itself. Other tests in dev/test (e.g. query.ts, write-batch.ts, bulk-writer.ts) use the same import, so I kept it as is.

@babu-ch
babu-ch marked this pull request as ready for review November 20, 2026 10:32
@babu-ch
babu-ch requested review from a team as code owners November 20, 2026 10:32

This branch has not been deployed

No deployments
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 Firestore API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant