Skip to content

Commit c144d8d

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Account for nulls in EventActions.Builder.stateDelta
PiperOrigin-RevId: 911414129
1 parent 8f20d56 commit c144d8d

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

core/src/main/java/com/google/adk/events/EventActions.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,15 @@ public Builder skipSummarization(@Nullable Boolean skipSummarization) {
289289
@CanIgnoreReturnValue
290290
@JsonProperty("stateDelta")
291291
public Builder stateDelta(@Nullable Map<String, Object> value) {
292-
if (value == null) {
293-
this.stateDelta = new ConcurrentHashMap<>();
294-
} else {
295-
this.stateDelta = new ConcurrentHashMap<>(value);
292+
this.stateDelta = new ConcurrentHashMap<>();
293+
if (value != null) {
294+
value
295+
.entrySet()
296+
.forEach(
297+
entry -> {
298+
stateDelta.put(
299+
entry.getKey(), Optional.ofNullable(entry.getValue()).orElse(State.REMOVED));
300+
});
296301
}
297302
return this;
298303
}

core/src/test/java/com/google/adk/events/EventActionsTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.common.collect.ImmutableSet;
2525
import com.google.genai.types.Content;
2626
import com.google.genai.types.Part;
27+
import java.util.HashMap;
2728
import java.util.Map;
2829
import java.util.concurrent.ConcurrentHashMap;
2930
import java.util.concurrent.ConcurrentMap;
@@ -129,6 +130,33 @@ public void removeStateByKey_marksKeyAsRemoved() {
129130
assertThat(eventActions.stateDelta()).containsExactly("key1", State.REMOVED);
130131
}
131132

133+
@Test
134+
public void builderStateDelta_withNullMap_initializesEmptyMap() {
135+
EventActions eventActions = EventActions.builder().stateDelta(null).build();
136+
137+
assertThat(eventActions.stateDelta()).isEmpty();
138+
}
139+
140+
@Test
141+
public void builderStateDelta_withNullValue_marksKeyAsRemoved() {
142+
Map<String, Object> inputDelta = new HashMap<>();
143+
inputDelta.put("key1", "value1");
144+
inputDelta.put("key2", null);
145+
146+
EventActions eventActions = EventActions.builder().stateDelta(inputDelta).build();
147+
148+
assertThat(eventActions.stateDelta()).containsExactly("key1", "value1", "key2", State.REMOVED);
149+
}
150+
151+
@Test
152+
public void jsonDeserialization_withNullValueInStateDelta_deserializesAsRemoved()
153+
throws Exception {
154+
String json = "{\"stateDelta\":{\"key1\":\"value1\",\"key2\":null}}";
155+
EventActions deserialized = EventActions.fromJsonString(json, EventActions.class);
156+
157+
assertThat(deserialized.stateDelta()).containsExactly("key1", "value1", "key2", State.REMOVED);
158+
}
159+
132160
@Test
133161
public void jsonSerialization_works() throws Exception {
134162
EventActions eventActions =

0 commit comments

Comments
 (0)