Repository navigation
Don't cast .sv to String blindly - #1269
rootkiller6788 wants to merge 2 commits into
Conversation
resolveDeferredValue assumed the value under ".sv" is always a string like
"timestamp". A compound server value such as {".sv": {"increment": 1}}
makes that cast blow up with a ClassCastException while the write is being
prepared, so the value never makes it to the server. Leave compound values
untouched instead - the server knows what to do with them. Same thing for the
priority path.
Covers resolving "timestamp", letting compound and unknown .sv values through untouched, and walking the children of a snapshot.
There was a problem hiding this comment.
Code Review
This pull request updates ServerValues.java to safely handle compound server values (such as increment operations) by checking if the server value key is a String before resolving it, preventing potential ClassCastExceptions. It also adds a new test suite in ServerValuesTest.java to verify these behaviors. The review feedback highlights an issue in resolveDeferredValueSnapshot where unknown server values could be incorrectly cleared to null, and suggests checking if the key exists in serverValues before resolving, matching the logic used in resolveDeferredValue.
| if (serverValueKey instanceof String) { | ||
| priorityVal = serverValues.get(serverValueKey); | ||
| } |
There was a problem hiding this comment.
If serverValueKey is an unknown server value (i.e., not present in serverValues), calling serverValues.get(serverValueKey) will return null. This will cause priorityVal to become null, effectively clearing the priority and converting it to an empty priority. To match the behavior of resolveDeferredValue and preserve unknown server values so they can be sent to the server, we should only resolve the priority if serverValues actually contains the key.
| if (serverValueKey instanceof String) { | |
| priorityVal = serverValues.get(serverValueKey); | |
| } | |
| if (serverValueKey instanceof String && serverValues.containsKey(serverValueKey)) { | |
| priorityVal = serverValues.get(serverValueKey); | |
| } |
Fixes #962.
The cast in resolveDeferredValue assumes whatever is under ".sv" is a string, so a compound server value like {".sv": {"increment": 1}} blows up with a ClassCastException while setValue is being prepared. Nothing catches it at that point, it just ends up in the runloop's uncaught handler and the write is dropped.
There's nothing to resolve there anyway - the client has no local answer for "increment", and the server understands the compound form on its own, so the right thing is to leave it alone and let it through. I gave the priority path the same guard since it had the same cast.
Tested with a new ServerValuesTest (timestamp resolution, compound/unknown .sv pass-through, snapshot children) and the whole com.google.firebase.database test package still passes - 401 tests green.