Skip to content
Open
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
14 changes: 10 additions & 4 deletions core/src/main/java/com/google/adk/events/EventActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,16 @@ public Builder skipSummarization(@Nullable Boolean skipSummarization) {
@CanIgnoreReturnValue
@JsonProperty("stateDelta")
public Builder stateDelta(@Nullable Map<String, Object> value) {
if (value == null) {
this.stateDelta = new ConcurrentHashMap<>();
} else {
this.stateDelta = new ConcurrentHashMap<>(value);
this.stateDelta = new ConcurrentHashMap<>();
if (value != null) {
// Convert null values to State.REMOVED to avoid NPEs.
value
.entrySet()
.forEach(
entry -> {
stateDelta.put(
entry.getKey(), Optional.ofNullable(entry.getValue()).orElse(State.REMOVED));
});
}
return this;
}
Expand Down
29 changes: 19 additions & 10 deletions core/src/main/java/com/google/adk/sessions/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -46,16 +47,24 @@ public State(Map<String, Object> state) {

public State(Map<String, Object> state, @Nullable Map<String, Object> delta) {
Objects.requireNonNull(state, "state is null");
this.state =
state instanceof ConcurrentMap
? (ConcurrentMap<String, Object>) state
: new ConcurrentHashMap<>(state);
this.delta =
delta == null
? new ConcurrentHashMap<>()
: delta instanceof ConcurrentMap
? (ConcurrentMap<String, Object>) delta
: new ConcurrentHashMap<>(delta);
this.state = toConcurrentMap(state);
this.delta = delta == null ? new ConcurrentHashMap<>() : toConcurrentMap(delta);
}

/**
* Converts a map to a concurrent map. Null values are converted to {@link #REMOVED} to avoid
* NPEs.
*
* <p>If the map is already a concurrent map, it is returned as is. Otherwise, a new concurrent
* map is created and returned.
*/
private static ConcurrentMap<String, Object> toConcurrentMap(Map<String, Object> map) {
if (map instanceof ConcurrentMap) {
return (ConcurrentMap<String, Object>) map;
}
ConcurrentMap<String, Object> concurrentMap = new ConcurrentHashMap<>();
map.forEach((key, value) -> concurrentMap.put(key, Optional.ofNullable(value).orElse(REMOVED)));
return concurrentMap;
}

@Override
Expand Down
28 changes: 28 additions & 0 deletions core/src/test/java/com/google/adk/events/EventActionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -129,6 +130,33 @@ public void removeStateByKey_marksKeyAsRemoved() {
assertThat(eventActions.stateDelta()).containsExactly("key1", State.REMOVED);
}

@Test
public void builderStateDelta_withNullMap_initializesEmptyMap() {
EventActions eventActions = EventActions.builder().stateDelta(null).build();

assertThat(eventActions.stateDelta()).isEmpty();
}

@Test
public void builderStateDelta_withNullValue_marksKeyAsRemoved() {
Map<String, Object> inputDelta = new HashMap<>();
inputDelta.put("key1", "value1");
inputDelta.put("key2", null);

EventActions eventActions = EventActions.builder().stateDelta(inputDelta).build();

assertThat(eventActions.stateDelta()).containsExactly("key1", "value1", "key2", State.REMOVED);
}

@Test
public void jsonDeserialization_withNullValueInStateDelta_deserializesAsRemoved()
throws Exception {
String json = "{\"stateDelta\":{\"key1\":\"value1\",\"key2\":null}}";
EventActions deserialized = EventActions.fromJsonString(json, EventActions.class);

assertThat(deserialized.stateDelta()).containsExactly("key1", "value1", "key2", State.REMOVED);
}

@Test
public void jsonSerialization_works() throws Exception {
EventActions eventActions =
Expand Down
16 changes: 10 additions & 6 deletions core/src/test/java/com/google/adk/sessions/StateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -22,11 +21,6 @@ public void constructor_nullDelta_createsEmptyConcurrentHashMap() {
assertThat(state.hasDelta()).isTrue();
}

@Test
public void constructor_nullState_throwsException() {
Assert.assertThrows(NullPointerException.class, () -> new State(null, new HashMap<>()));
}

@Test
public void constructor_regularMapState() {
Map<String, Object> stateMap = new HashMap<>();
Expand All @@ -47,4 +41,14 @@ public void constructor_singleArgument() {
state.put("key", "value");
assertThat(state.hasDelta()).isTrue();
}

@Test
public void constructor_stateMapWithNullValues_replacesWithRemoved() {
Map<String, Object> stateMap = new HashMap<>();
stateMap.put("key1", "value1");
stateMap.put("key2", null);
State state = new State(stateMap);
assertThat(state).containsEntry("key1", "value1");
assertThat(state).containsEntry("key2", State.REMOVED);
}
}