firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

[Firestore emulator] `setDoc` issues a `CREATE` request but also a `UPDATE` request at the same time

Open opack opened this issue 1 year ago • 4 comments

[REQUIRED] Environment info

**firebase-tools:**13.1.0

**Platform:**Windows 10 Pro 22H2

[REQUIRED] Test case

Create a document using addDoc(), for example with:

const data = { id: 'some_id', name: 'name' }
const commentRef = await addDoc(collection(testFirestore, 'comments'), data)

Then create a document using setDoc(), for example with:

const commentRef = doc(collection(testFirestore, 'comments'), 'DBG')
const data = { id: commentRef.id, name: 'name' }
await setDoc(commentRef, data)

[REQUIRED] Steps to reproduce

Do the above to create a doc with setDoc() and one with addDoc()

[REQUIRED] Expected behavior

One CREATE request issued for both.

[REQUIRED] Actual behavior

When creating a document with setDoc() : 2 requests are issued: one CREATE and one UPDATE. The UPDATE one seems totally useless. The problem does not appear with addDoc(): only a CREATE request is performed.

This issue was in fact already reported on the Firebase SDK. It was closed because not in the right repo, but I did not find it in this repo.

This issue is really blocking because I want to create some security rules to allow the creation of a document only if it does not exist. The CREATE request is correctly denied but the UPDATE request is not, resulting in the document being overwritten (which is exactly why I wanted to avoid...). Here is my firestore.rules files:

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    match /comments/{document} {
      allow create: if false;
      allow update: if true;
    }
  }
}

(Note: I cannot deny all updates on this collection, as I want to allow them also in some cases. What I want is to enforce that creation is not performed if the document already exists.)

And here is what I have in the Requests tab of the emulator: image image image In fact I even have a third request, another UPDATE similar to the previous one: image

opack avatar Jan 26 '24 08:01 opack

Hey @opack, thanks for reporting this. The source code for the Firestore emulator doesn't actually live in this repo (firebase-tools just downloads the compiled binary at runtime), but I've forwarded this issue along to the Firestore team & they will take a look.

joehan avatar Jan 29 '24 19:01 joehan

Hey @opack, thanks for reporting this. The source code for the Firestore emulator doesn't actually live in this repo (firebase-tools just downloads the compiled binary at runtime), but I've forwarded this issue along to the Firestore team & they will take a look.

Many thanks @joehan! 🙏 I saw other emulator-related issues so I thought it was the right place, sorry. For my information, and so that I can post my next emulator-related issues, where should I post this kind of issue?

opack avatar Jan 30 '24 09:01 opack

No worries! This is the right place to report emulator issues.

joehan avatar Jan 30 '24 21:01 joehan

I also encountered this issue. After some time, I understood that it is indeed correct; setDoc is designed for both creation (when the document does not exist) and updating. Consequently, I adjusted my rules and opted to use updateDoc in my scenario.

krzysztofpniak avatar Mar 03 '24 17:03 krzysztofpniak