Skip to content

Commit 1151c80

Browse files
committed
feat: harden spec immutability
1 parent 8a101ca commit 1151c80

31 files changed

Lines changed: 789 additions & 3 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ public record AgentCard(
8989
Assert.checkNotNullParam("skills", skills);
9090
Assert.checkNotNullParam("supportedInterfaces", supportedInterfaces);
9191
Assert.checkNotNullParam("version", version);
92+
defaultInputModes = List.copyOf(defaultInputModes);
93+
defaultOutputModes = List.copyOf(defaultOutputModes);
94+
skills = List.copyOf(skills);
95+
securitySchemes = securitySchemes == null ? null : Map.copyOf(securitySchemes);
96+
securityRequirements = securityRequirements == null ? null : List.copyOf(securityRequirements);
97+
supportedInterfaces = List.copyOf(supportedInterfaces);
98+
signatures = signatures == null ? null : List.copyOf(signatures);
99+
additionalInterfaces = additionalInterfaces == null ? null : List.copyOf(additionalInterfaces);
92100
}
93101

94102
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public record AgentCardSignature(@Nullable Map<String, Object> header, @Serializ
4646
public AgentCardSignature {
4747
Assert.checkNotNullParam("protectedHeader", protectedHeader);
4848
Assert.checkNotNullParam("signature", signature);
49+
header = header == null ? null : Map.copyOf(header);
4950
}
5051

5152
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public record AgentExtension (@Nullable String description, @Nullable Map<String
3737
*/
3838
public AgentExtension {
3939
Assert.checkNotNullParam("uri", uri);
40+
params = params == null ? null : Map.copyOf(params);
4041
}
4142

4243
/**

‎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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public record Artifact(String artifactId, @Nullable String name, @Nullable Strin
5050
if (parts.isEmpty()) {
5151
throw new IllegalArgumentException("Parts cannot be empty");
5252
}
53+
parts = List.copyOf(parts);
54+
metadata = metadata == null ? null : Map.copyOf(metadata);
55+
extensions = extensions == null ? null : List.copyOf(extensions);
5356
}
5457

5558
/**

‎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)