Repository navigation
Conversation
There was a problem hiding this comment.
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.
| import {DocumentData} from '@google-cloud/firestore'; | ||
|
|
||
| import {DocumentReference, Firestore, setLogFunction} from '../src'; |
There was a problem hiding this comment.
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.
| import {DocumentData} from '@google-cloud/firestore'; | |
| import {DocumentReference, Firestore, setLogFunction} from '../src'; | |
| import {DocumentData, DocumentReference, Firestore, setLogFunction} from '../src'; |
There was a problem hiding this comment.
../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.
Fixes #7450 🦕
CollectionReference.add()calledconverter.toFirestore()to validate the data, then passed the original data toDocumentReference.create(), which callstoFirestore()again inWriteBatch.create(). A converter that mutates its input (as in the issue) breaks on the second call.WriteBatch.create()already runs the samevalidateDocumentData('data', ...)on the converted data synchronously, so this change removes the conversion and validation fromadd().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 failsprettier/prettieronmainonce 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)