Skip to content

Commit 19693d3

Browse files
committed
feat: harden spec immutability
1 parent 8a101ca commit 19693d3

31 files changed

Lines changed: 930 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.a2aproject.sdk.compat03.conversion.mappers.domain;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import org.a2aproject.sdk.compat03.spec.TaskState_v0_3;
10+
import org.a2aproject.sdk.compat03.spec.TaskStatusUpdateEvent_v0_3;
11+
import org.a2aproject.sdk.compat03.spec.TaskStatus_v0_3;
12+
import org.a2aproject.sdk.spec.TaskState;
13+
import org.a2aproject.sdk.spec.TaskStatus;
14+
import org.a2aproject.sdk.spec.TaskStatusUpdateEvent;
15+
import org.junit.jupiter.api.Test;
16+
17+
class TaskStatusUpdateEventMapper_v0_3_Test {
18+
19+
@Test
20+
void toV10_preservesMetadataAndDefensiveCopy() {
21+
Map<String, Object> mutableMetadata = new HashMap<>();
22+
mutableMetadata.put("source", "sensor");
23+
24+
TaskStatusUpdateEvent_v0_3 v03 = new TaskStatusUpdateEvent_v0_3.Builder()
25+
.taskId("task-123")
26+
.status(new TaskStatus_v0_3(TaskState_v0_3.WORKING))
27+
.contextId("context-456")
28+
.isFinal(false)
29+
.metadata(mutableMetadata)
30+
.build();
31+
32+
TaskStatusUpdateEvent v10 = TaskStatusUpdateEventMapper_v0_3.INSTANCE.toV10(v03);
33+
mutableMetadata.put("write", "should-not-leak");
34+
35+
assertEquals("task-123", v10.taskId());
36+
assertEquals(TaskState.TASK_STATE_WORKING, v10.status().state());
37+
assertEquals("context-456", v10.contextId());
38+
assertEquals(Map.of("source", "sensor"), v10.metadata());
39+
assertThrows(UnsupportedOperationException.class, () -> v10.metadata().put("another", "value"));
40+
}
41+
42+
@Test
43+
void fromV10_preservesMetadata() {
44+
TaskStatusUpdateEvent v10 = new TaskStatusUpdateEvent(
45+
"task-123",
46+
new TaskStatus(TaskState.TASK_STATE_WORKING),
47+
"context-456",
48+
Map.of("source", "sensor"));
49+
50+
TaskStatusUpdateEvent_v0_3 v03 = TaskStatusUpdateEventMapper_v0_3.INSTANCE.fromV10(v10);
51+
52+
assertEquals("task-123", v03.taskId());
53+
assertEquals(TaskState_v0_3.WORKING, v03.status().state());
54+
assertEquals("context-456", v03.contextId());
55+
assertEquals(Map.of("source", "sensor"), v03.metadata());
56+
}
57+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package org.a2aproject.sdk.server.tasks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertSame;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import org.a2aproject.sdk.spec.Artifact;
12+
import org.a2aproject.sdk.spec.ListTasksParams;
13+
import org.a2aproject.sdk.spec.Message;
14+
import org.a2aproject.sdk.spec.Task;
15+
import org.a2aproject.sdk.spec.TaskState;
16+
import org.a2aproject.sdk.spec.TaskStatus;
17+
import org.a2aproject.sdk.spec.TextPart;
18+
import org.junit.jupiter.api.Test;
19+
20+
public class InMemoryTaskStoreTest {
21+
22+
@Test
23+
public void testSaveAndGet() {
24+
InMemoryTaskStore store = new InMemoryTaskStore();
25+
Task task = sampleTask("task-abc");
26+
27+
store.save(task, false);
28+
29+
Task retrieved = store.get(task.id());
30+
assertSame(task, retrieved);
31+
}
32+
33+
@Test
34+
public void testGetNonExistent() {
35+
InMemoryTaskStore store = new InMemoryTaskStore();
36+
37+
Task retrieved = store.get("nonexistent");
38+
assertNull(retrieved);
39+
}
40+
41+
@Test
42+
public void testDelete() {
43+
InMemoryTaskStore store = new InMemoryTaskStore();
44+
Task task = sampleTask("task-abc");
45+
46+
store.save(task, false);
47+
store.delete(task.id());
48+
49+
Task retrieved = store.get(task.id());
50+
assertNull(retrieved);
51+
}
52+
53+
@Test
54+
public void testDeleteNonExistent() {
55+
InMemoryTaskStore store = new InMemoryTaskStore();
56+
57+
store.delete("non-existent");
58+
}
59+
60+
@Test
61+
public void testListTransformsHistoryAndArtifacts() {
62+
InMemoryTaskStore store = new InMemoryTaskStore();
63+
Task task = Task.builder()
64+
.id("task-abc")
65+
.contextId("session-xyz")
66+
.status(new TaskStatus(TaskState.TASK_STATE_WORKING))
67+
.history(List.of(sampleMessage("msg-1"), sampleMessage("msg-2")))
68+
.artifacts(List.of(sampleArtifact("artifact-1")))
69+
.metadata(Map.of("origin", "test"))
70+
.build();
71+
72+
store.save(task, false);
73+
74+
ListTasksParams params = ListTasksParams.builder().build();
75+
List<Task> tasks = store.list(params, null).tasks();
76+
77+
assertEquals(1, tasks.size());
78+
Task listed = tasks.get(0);
79+
assertEquals(task.id(), listed.id());
80+
assertEquals(task.contextId(), listed.contextId());
81+
assertTrue(listed.history().isEmpty(), "Default list() should omit history");
82+
assertTrue(listed.artifacts().isEmpty(), "Default list() should omit artifacts");
83+
assertEquals(task.metadata(), listed.metadata());
84+
}
85+
86+
private static Task sampleTask(String id) {
87+
return Task.builder()
88+
.id(id)
89+
.contextId("session-xyz")
90+
.status(new TaskStatus(TaskState.TASK_STATE_SUBMITTED))
91+
.build();
92+
}
93+
94+
private static Message sampleMessage(String messageId) {
95+
return Message.builder()
96+
.role(Message.Role.ROLE_USER)
97+
.parts(List.of(new TextPart("content")))
98+
.messageId(messageId)
99+
.build();
100+
}
101+
102+
private static Artifact sampleArtifact(String artifactId) {
103+
return Artifact.builder()
104+
.artifactId(artifactId)
105+
.parts(List.of(new TextPart("artifact content")))
106+
.build();
107+
}
108+
}
109+

‎server-common/src/test/java/org/a2aproject/sdk/server/tasks/TaskManagerTest.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ public void testSaveTaskEventStatusUpdate() throws A2AServerException {
9595

9696
assertEquals(initialTask.id(), updated.id());
9797
assertEquals(initialTask.contextId(), updated.contextId());
98-
// TODO type does not get unmarshalled
99-
//assertEquals(initialTask.getType(), updated.getType());
10098
assertSame(newStatus, updated.status());
10199
}
102100

‎spec/src/main/java/org/a2aproject/sdk/spec/AgentCapabilities.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.a2aproject.sdk.spec;
22

33
import java.util.List;
4+
45
import org.jspecify.annotations.Nullable;
56

67
/**
@@ -35,6 +36,10 @@ public record AgentCapabilities(boolean streaming,
3536
boolean extendedAgentCard,
3637
@Nullable List<AgentExtension> extensions) {
3738

39+
public AgentCapabilities {
40+
extensions = extensions == null ? null : List.copyOf(extensions);
41+
}
42+
3843
/**
3944
* Create a new Builder
4045
*

‎spec/src/main/java/org/a2aproject/sdk/spec/AgentCard.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.a2aproject.sdk.spec;
22

3-
import java.util.ArrayList;
4-
import java.util.Collections;
5-
import java.util.List;
6-
import java.util.Map;
3+
import java.util.*;
74

85
import org.a2aproject.sdk.util.Assert;
96
import org.jspecify.annotations.Nullable;
@@ -89,6 +86,14 @@ public record AgentCard(
8986
Assert.checkNotNullParam("skills", skills);
9087
Assert.checkNotNullParam("supportedInterfaces", supportedInterfaces);
9188
Assert.checkNotNullParam("version", version);
89+
defaultInputModes = List.copyOf(defaultInputModes);
90+
defaultOutputModes = List.copyOf(defaultOutputModes);
91+
skills = List.copyOf(skills);
92+
securitySchemes = securitySchemes == null ? null : Collections.unmodifiableMap(new HashMap<>(securitySchemes));
93+
securityRequirements = securityRequirements == null ? null : List.copyOf(securityRequirements);
94+
supportedInterfaces = List.copyOf(supportedInterfaces);
95+
signatures = signatures == null ? null : List.copyOf(signatures);
96+
additionalInterfaces = additionalInterfaces == null ? null : List.copyOf(additionalInterfaces);
9297
}
9398

9499
/**

‎spec/src/main/java/org/a2aproject/sdk/spec/AgentCardSignature.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.a2aproject.sdk.spec;
22

3+
import java.util.Collections;
4+
import java.util.HashMap;
35
import java.util.Map;
46

57
import com.google.gson.annotations.SerializedName;
@@ -46,6 +48,7 @@ public record AgentCardSignature(@Nullable Map<String, Object> header, @Serializ
4648
public AgentCardSignature {
4749
Assert.checkNotNullParam("protectedHeader", protectedHeader);
4850
Assert.checkNotNullParam("signature", signature);
51+
header = header == null ? null : Collections.unmodifiableMap(new HashMap<>(header));
4952
}
5053

5154
/**

‎spec/src/main/java/org/a2aproject/sdk/spec/AgentExtension.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.a2aproject.sdk.spec;
22

3+
import java.util.Collections;
4+
import java.util.HashMap;
35
import java.util.Map;
46

57
import org.a2aproject.sdk.util.Assert;
@@ -37,6 +39,7 @@ public record AgentExtension (@Nullable String description, @Nullable Map<String
3739
*/
3840
public AgentExtension {
3941
Assert.checkNotNullParam("uri", uri);
42+
params = params == null ? null : Collections.unmodifiableMap(new HashMap<>(params));
4043
}
4144

4245
/**

‎spec/src/main/java/org/a2aproject/sdk/spec/AgentSkill.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public record AgentSkill(String id, String name, String description, List<String
6161
Assert.checkNotNullParam("name", name);
6262
Assert.checkNotNullParam("description", description);
6363
Assert.checkNotNullParam("tags", tags);
64+
tags = List.copyOf(tags);
65+
examples = examples == null ? null : List.copyOf(examples);
66+
inputModes = inputModes == null ? null : List.copyOf(inputModes);
67+
outputModes = outputModes == null ? null : List.copyOf(outputModes);
68+
securityRequirements = securityRequirements == null ? null : List.copyOf(securityRequirements);
6469
}
6570

6671
/**

‎spec/src/main/java/org/a2aproject/sdk/spec/Artifact.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.a2aproject.sdk.spec;
22

3+
import java.util.Collections;
4+
import java.util.HashMap;
35
import java.util.List;
46
import java.util.Map;
57

@@ -50,6 +52,9 @@ public record Artifact(String artifactId, @Nullable String name, @Nullable Strin
5052
if (parts.isEmpty()) {
5153
throw new IllegalArgumentException("Parts cannot be empty");
5254
}
55+
parts = List.copyOf(parts);
56+
metadata = metadata == null ? null : Collections.unmodifiableMap(new HashMap<>(metadata));
57+
extensions = extensions == null ? null : List.copyOf(extensions);
5358
}
5459

5560
/**

‎spec/src/main/java/org/a2aproject/sdk/spec/AuthorizationCodeOAuthFlow.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ public record AuthorizationCodeOAuthFlow(String authorizationUrl, String refresh
4242
Assert.checkNotNullParam("authorizationUrl", authorizationUrl);
4343
Assert.checkNotNullParam("scopes", scopes);
4444
Assert.checkNotNullParam("tokenUrl", tokenUrl);
45+
scopes = Map.copyOf(scopes);
4546
}
4647
}

0 commit comments

Comments
 (0)