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
Firestore - Inconsistent behavior in key addressing #288
Comments
@suraj-qlogic, you have the same issue? Or what is happening? |
@Pirayya. Thanks for filling an issue.I am currently investigating on it if found any inconsistent behaviour, then i will open PR soon |
@suraj-qlogic, I am saying that there is inconsistent behavior in addressing keys, although it might've been implemented like that deliberately. Do you have an idea of what the 'correct' behavior is? I would like to see someone with authority to make a decision here, rather than just making a PR fixing it one way or the other. |
|
First, I want to point out that the code snippets above to very different things:
This deletes the field
This deletes the field That being said, the second code snippet should be supported and should not be rejected. @suraj-qlogic Is this something you can fix? |
@schmidt-sebastian, I just tested creating a JSON structure like: {
"users": {
"Pirayya": {
"color": "blue"
}
} And ran the following code (Kotlin) val key = "users.Pirayya.color"
val updates = mapOf(key to FieldValue.delete())
firestore.collection(collection).document(accountKey).update(updates) And the "color" field inside the object "Pirayya" was removed. So if the expected behavior is to remove the key I also think the issue regarding the second code example I showed in the ticket can be fixed by figuring out whether or not to use Line 163 in 80087a3
I believe each segment in java-firestore/google-cloud-firestore/src/main/java/com/google/cloud/firestore/BasePath.java Line 61 in 80087a3
|
As you stated, string keys are expanded (split on dots): Field keys created with FieldPath.of() are not: |
Ok, sorry, I might've not made myself clear. As a user of the Java Firestore SDK I expect
Somewhere along the way, all the keys except the first one is changed. So right before the update is made the FieldPath transforms and points to
|
Your understanding is correct. This looks like a bug. |
@suraj-qlogic Any chance you can take a look at a fix for this? |
@BenWhitehead ,Yes it's in progress.i have been investigated and found this is happen due to this code.I will open PR as soon as possible. |
Sounds great, thanks @suraj-qlogic! |
@suraj-qlogic @BenWhitehead, sorry, accidentally opened a PR here, closed it immediately though. But I have a suggestion for a fix: https://github.com/Pirayya/java-firestore/pull/1/commits/167443c88e1d33a02ce93882559d368b03ece5e9 What do you think? Edit: I haven't tested this approach, it is only somewhat of a suggestion (mostly implemented in a way to not break anything) Edit2: I've tried two scenarios: firestore.collection("my-collection")
.document("my-document")
.update(FieldPath.of("user", "properties", "meta", "favorite.color"), "blue"); This will update the field: user->properties->meta->favorite.color to 'blue'. But the second scenario (deleting): firestore.collection("my-collection")
.document("my-document")
.update(FieldPath.of("user", "properties", "meta", "favorite.color"), FieldValue.delete()); Fails because of the java-firestore/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UpdateBuilder.java Line 502 in b91c57c
The fields object contains one key with a FieldPath pointing to: users->properties->meta->favorite.color but before we call Line 100 in b91c57c
So the AppendOptions might fix this issue but might introduce new issues 😨 |
@suraj-qlogic I will look at this in a bit (in a meeting now). |
So the problem is in UserDataConverter in line 164. To process the nested updated, we call The "ugly" fix is:
We should find a way to make this pretty. |
@schmidt-sebastian, the fix I made in the forked project has another - well, maybe not so beautiful - fix but it is another suggestion. But as I've stated before, I know too little of the intricacies of this project to know all the paths in to encodeValue so I don't know if my proposed fix would be useful. Although if you thought it to be good, instead of me spending a lot of time signing the CLA, getting to know what tests to fix/add etc. maybe someone else can copy it and create a real PR? :P (Sorry for being lazy) |
@Pirayya Sorry, I missed the link to the fork in the earlier comment. Yours is a much cleaner version of what I have. @suraj-qlogic Can we take https://github.com/Pirayya/java-firestore/commit/167443c88e1d33a02ce93882559d368b03ece5e9 and add a test? |
@schmidt-sebastian, no problem! But all credit should go to @dmitry-fa, he came with a better suggestion than my first one! 👍 |
@schmidt-sebastian ,Ok i will do that. |
@schmidt-sebastian Is there anything else we need to address this issue other than what was merged in #295? If not I'll plan on performing a release with this fix early next week. |
No - we are good to go. |
Nice, thank you all for helping me solve this so quickly! :) @BenWhitehead, @schmidt-sebastian, @suraj-qlogic and @dmitry-fa! |
OS Type: Its Java - version 11
Firestore Java SDK version (1.33.0, but I can see that the problem still exists in the code on master)
Using field keys with dots (.), i.e.
my.key: "My value"
is allowed, albeit a bit frowned upon, has inconsistent behavior when creating compared to deleting. It is perfectly fine to create a key with dots but cannot be deleted (unless it exists on the top level of the document).can be removed, but:
cannot.
That's not all! Using the JavaScript SDK for Firestore, deleting is not a problem.
It is always debatable whether or not to use dots in keys, but I think the inconsistency is something that has to be fixed.
Steps to reproduce
or
The first code block will succeed without any change, but the second will result in a failed Precondition.
Code example
See steps to reproduce
Stack trace
Will fail right here:
java-firestore/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UserDataConverter.java
Line 101 in 80087a3
If you don't have time to fix this issue(? don't know if you regard this as an issue), I can definitely contribute!
Tell me if anything is unclear!
The text was updated successfully, but these errors were encountered: