Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public static Object resolveDeferredValue(Object value, Map<String, Object> serv
if (value instanceof Map) {
Map mapValue = (Map) value;
if (mapValue.containsKey(NAME_SUBKEY_SERVERVALUE)) {
String serverValueKey = (String) mapValue.get(NAME_SUBKEY_SERVERVALUE);
if (serverValues.containsKey(serverValueKey)) {
Object serverValueKey = mapValue.get(NAME_SUBKEY_SERVERVALUE);
// A compound server value (e.g. {".sv": {"increment": 1}}) can't be resolved here.
// Leave it as-is so it gets sent to the server, which understands it.
if (serverValueKey instanceof String && serverValues.containsKey(serverValueKey)) {
return serverValues.get(serverValueKey);
}
}
Expand Down Expand Up @@ -70,8 +72,10 @@ public static Node resolveDeferredValueSnapshot(
if (priorityVal instanceof Map) {
Map priorityMapValue = (Map) priorityVal;
if (priorityMapValue.containsKey(NAME_SUBKEY_SERVERVALUE)) {
String serverValueKey = (String) priorityMapValue.get(NAME_SUBKEY_SERVERVALUE);
priorityVal = serverValues.get(serverValueKey);
Object serverValueKey = priorityMapValue.get(NAME_SUBKEY_SERVERVALUE);
if (serverValueKey instanceof String) {
priorityVal = serverValues.get(serverValueKey);
}
Comment on lines +76 to +78

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
if (serverValueKey instanceof String) {
priorityVal = serverValues.get(serverValueKey);
}
if (serverValueKey instanceof String && serverValues.containsKey(serverValueKey)) {
priorityVal = serverValues.get(serverValueKey);
}

}
}
Node priority = PriorityUtilities.parsePriority(priorityVal);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.database.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.firebase.database.snapshot.Node;
import com.google.firebase.database.snapshot.NodeUtilities;
import com.google.firebase.database.utilities.Clock;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;

public class ServerValuesTest {

private static final Clock CLOCK = () -> 42L;

private static Map<String, Object> serverValues() {
return ServerValues.generateServerValues(CLOCK);
}

private static Map<String, Object> valueWithServerValue(Object serverValue) {
Map<String, Object> value = new HashMap<>();
value.put(ServerValues.NAME_SUBKEY_SERVERVALUE, serverValue);
return value;
}

@Test
public void resolveDeferredValueResolvesTimestamp() {
Map<String, Object> value = valueWithServerValue("timestamp");
assertEquals(42L, ServerValues.resolveDeferredValue(value, serverValues()));
}

@Test
public void resolveDeferredValueLeavesCompoundServerValueAlone() {
Map<String, Object> increment = new HashMap<>();
increment.put("increment", 5L);

Map<String, Object> value = valueWithServerValue(increment);
assertEquals(value, ServerValues.resolveDeferredValue(value, serverValues()));
}

@Test
public void resolveDeferredValueLeavesUnknownServerValueAlone() {
Map<String, Object> value = valueWithServerValue("somethingElse");
assertEquals(value, ServerValues.resolveDeferredValue(value, serverValues()));
}

@Test
public void resolveDeferredValueSnapshotWithCompoundServerValue() {
Map<String, Object> increment = new HashMap<>();
increment.put("increment", 5L);

Node node = NodeUtilities.NodeFromJSON(valueWithServerValue(increment));
Node resolved = ServerValues.resolveDeferredValueSnapshot(node, serverValues());

assertFalse(resolved.isEmpty());
assertEquals(node, resolved);
}

@Test
public void resolveDeferredValueSnapshotWithTimestamp() {
Node node = NodeUtilities.NodeFromJSON(valueWithServerValue("timestamp"));
Node resolved = ServerValues.resolveDeferredValueSnapshot(node, serverValues());

assertTrue(resolved.isLeafNode());
assertEquals(42L, resolved.getValue());
}

@Test
public void resolveDeferredValueSnapshotWalksChildren() {
Map<String, Object> value = new HashMap<>();
value.put("updatedAt", valueWithServerValue("timestamp"));
value.put("keep", "as-is");

Node node = NodeUtilities.NodeFromJSON(value);
Node resolved = ServerValues.resolveDeferredValueSnapshot(node, serverValues());

assertEquals(42L, resolved.getChild(new Path("updatedAt")).getValue());
assertEquals("as-is", resolved.getChild(new Path("keep")).getValue());
}
}
Loading