Skip to content

FR: FirestoreDataConverter: don't pass data by reference #7188

@risalfajar

Description

@risalfajar

[REQUIRED] Describe your environment

  • Operating System version: Ubuntu 22.04
  • Browser version: Vivaldi 64 Bit 5.7.2921.65
  • Firebase SDK version: 9.17.1
  • Firebase Product: firestore

[REQUIRED] Describe the problem

When I modify data inside FirestoreDataConverter.toFirestore(), the source data is also modified, this causes error. Of course this could be fixed if I deep-copied the data first, but it's much simpler if the library handles this.

Steps to reproduce:

  1. Create an object to be sent to firestore
  2. Send the data/object to Firestore by calling set() on a document reference that have converter
  3. Modify the data inside converter's toFirestore(). For example: assign serverTimeStamp or delete unneded field.
  4. The source data would also be modified

Relevant Code:

export const salesConverter: FirestoreDataConverter<Sales> = {
        // this passed `data` should be deep copied so that it doesn't affect the original data
	toFirestore(data: PartialWithFieldValue<Sales>, options?: SetOptions): DocumentData {
		delete data.id
		if (!data.createdAt) data.createdAt = serverTimestamp()
		return data
	},
	fromFirestore(snap: QueryDocumentSnapshot, options: SnapshotOptions | undefined): Sales {
                // ....
	}
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions