Skip to content

Call toFirestore() only once #3755

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

Merged
merged 6 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-donuts-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore': patch
---

Fixed a bug where CollectionReference.add() called FirestoreDataConverter.toFirestore() twice intead of once (#3742).
3 changes: 2 additions & 1 deletion packages/firestore/exp/src/api/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import { Transaction as InternalTransaction } from '../../../src/core/transactio
import { validateReference } from '../../../lite/src/api/write_batch';
import { getDatastore } from '../../../lite/src/api/components';

export class Transaction extends LiteTransaction
export class Transaction
extends LiteTransaction
implements firestore.Transaction {
// This class implements the same logic as the Transaction API in the Lite SDK
// but is subclassed in order to return its own DocumentSnapshot types.
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/lite/src/api/field_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { ParseContext } from '../../../src/api/user_data_reader';
import { FieldTransform } from '../../../src/model/mutation';

/** The public FieldValue class of the lite API. */
export abstract class FieldValue extends SerializableFieldValue
export abstract class FieldValue
extends SerializableFieldValue
implements firestore.FieldValue {}

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2379,8 +2379,18 @@ export class CollectionReference<T = DocumentData>
? this._converter.toFirestore(value)
: value;
validateArgType('CollectionReference.add', 'object', 1, convertedValue);

const docRef = this.doc();
return docRef.set(value).then(() => docRef);

// Call set() with the converted value directly to avoid calling toFirestore() a second time.
// Cast to unknown in order to access the _key property.
return new DocumentReference(
((docRef as unknown) as DocumentReference)._key,
this.firestore,
null
)
.set(convertedValue)
.then(() => docRef);
}

withConverter<U>(
Expand Down