Skip to content

Commit a39afea

Browse files
malladinagarjuna2kabirclaude
authored
fix(grpc): transport validation and error message (#1026)
* fixed the transport validation and error message * refactor: use CollectionCopies.unmodifiableShallowList for DataPart list copying Add unmodifiableShallowList() to CollectionCopies to mirror the existing unmodifiableShallowMap() method, and use it in DataPart.defensivelyCopy() instead of inlining the pattern. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Kabir Khan <kkhan@redhat.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 81cba3b commit a39afea

4 files changed

Lines changed: 109 additions & 12 deletions

File tree

‎client/transport/grpc/src/main/java/org/a2aproject/sdk/client/transport/grpc/GrpcTransport.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,7 @@ public TaskPushNotificationConfig getTaskPushNotificationConfiguration(GetTaskPu
252252
@Nullable ClientCallContext context) throws A2AClientException {
253253
checkNotNullParam("request", request);
254254
checkNotNullParam("taskId", request.taskId());
255-
if(request.id() == null) {
256-
throw new IllegalArgumentException("Id must not be null");
257-
}
255+
checkNotNullParam("id", request.id());
258256

259257
org.a2aproject.sdk.grpc.GetTaskPushNotificationConfigRequest grpcRequest = org.a2aproject.sdk.grpc.GetTaskPushNotificationConfigRequest.newBuilder()
260258
.setTaskId(request.taskId())
@@ -333,7 +331,7 @@ public void subscribeToTask(TaskIdParams request, Consumer<StreamingEventKind> e
333331
A2AServiceStub stubWithMetadata = createAsyncStubWithMetadata(context, payloadAndHeaders);
334332
stubWithMetadata.subscribeToTask(grpcRequest, streamObserver);
335333
} catch (StatusRuntimeException e) {
336-
throw GrpcErrorMapper.mapGrpcError(e, "Failed to subscribe task push notification config: ");
334+
throw GrpcErrorMapper.mapGrpcError(e, "Failed to subscribe to task: ");
337335
}
338336
}
339337

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.gson.ToNumberPolicy;
88
import org.a2aproject.sdk.util.Assert;
99
import org.a2aproject.sdk.spec.util.CollectionCopies;
10+
import java.util.List;
1011
import java.util.Map;
1112
import org.jspecify.annotations.Nullable;
1213

@@ -23,7 +24,6 @@
2324
* <li>JSON objects: {@code Map<String, Object>}</li>
2425
* <li>JSON arrays: {@code List<Object>}</li>
2526
* <li>Primitives: {@code String}, {@code Number}, {@code Boolean}</li>
26-
* <li>Null values: {@code null}</li>
2727
* </ul>
2828
* <p>
2929
* Example usage:
@@ -42,7 +42,7 @@
4242
* DataPart primitive = new DataPart(42);
4343
* }</pre>
4444
*
45-
* @param data the structured data (required, supports JSON objects, arrays, primitives, and null)
45+
* @param data the structured data (required, supports JSON objects, arrays, and primitives)
4646
* @param metadata additional metadata for the part
4747
* @see Part
4848
* @see Message
@@ -59,24 +59,25 @@ public record DataPart(Object data, @Nullable Map<String, Object> metadata) impl
5959
public static final String DATA = "data";
6060

6161
/**
62-
* Compact constructor with validation.
62+
* Compact constructor with validation and defensive copying.
6363
* <p>
64-
* Note: For mutable data types (Map, List), callers should ensure immutability
65-
* by using {@code Map.copyOf()} or {@code List.copyOf()} before passing to this constructor.
64+
* For mutable data types ({@code Map} and {@code List}), an unmodifiable defensive
65+
* copy is created. Primitives and other immutable values are stored as-is.
6666
*
67-
* @param data the structured data (supports JSON objects, arrays, primitives, and null)
67+
* @param data the structured data (required, supports JSON objects, arrays, and primitives)
68+
* @param metadata additional metadata for the part
6869
* @throws IllegalArgumentException if data is null
6970
*/
7071
public DataPart (Object data, @Nullable Map<String, Object> metadata) {
7172
Assert.checkNotNullParam("data", data);
7273
this.metadata = CollectionCopies.unmodifiableNullableShallowMap(metadata);
73-
this.data = data;
74+
this.data = defensivelyCopy(data);
7475
}
7576

7677
/**
7778
* Constructor.
7879
*
79-
* @param data the structured data (supports JSON objects, arrays, primitives, and not null)
80+
* @param data the structured data (required, supports JSON objects, arrays, and primitives)
8081
* @throws IllegalArgumentException if data is null
8182
*/
8283
public DataPart(Object data) {
@@ -137,4 +138,24 @@ public static DataPart fromJson(String json, @Nullable Map<String, Object> metad
137138
private static final Gson JSON_PARSER = new GsonBuilder()
138139
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
139140
.create();
141+
142+
/**
143+
* Creates a defensive copy of mutable collection types to ensure immutability.
144+
* <p>
145+
* For {@code Map} and {@code List} instances, returns an unmodifiable copy preserving
146+
* null elements. For all other types (primitives, Strings, immutable objects), returns
147+
* the value as-is.
148+
*
149+
* @param data the data value to potentially copy
150+
* @return an unmodifiable copy for collections, or the original value for immutable types
151+
*/
152+
private static Object defensivelyCopy(Object data) {
153+
if (data instanceof Map<?, ?> map) {
154+
return CollectionCopies.unmodifiableShallowMap(map);
155+
}
156+
if (data instanceof List<?> list) {
157+
return CollectionCopies.unmodifiableShallowList(list);
158+
}
159+
return data;
160+
}
140161
}

‎spec/src/main/java/org/a2aproject/sdk/spec/util/CollectionCopies.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ public static <K, V> Map<K, V> unmodifiableShallowMap(Map<K, V> map) {
135135
return map == null ? null : unmodifiableShallowMap(map);
136136
}
137137

138+
/**
139+
* Creates an unmodifiable shallow defensive copy of a non-null list while preserving null elements.
140+
* <p>
141+
* This method is intended for JSON-like lists where null elements may be valid protocol data.
142+
* It intentionally does not use {@link List#copyOf(java.util.Collection)}, because
143+
* {@code List.copyOf} throws {@link NullPointerException} when the source list contains null elements.
144+
* <p>
145+
* The returned list cannot be structurally modified, but this method does not deep-copy
146+
* mutable elements.
147+
*
148+
* @param list the source list, must not be {@code null}
149+
* @param <E> the element type
150+
* @return an unmodifiable shallow copy preserving null elements
151+
*/
152+
public static <E> List<E> unmodifiableShallowList(List<E> list) {
153+
return Collections.unmodifiableList(new ArrayList<>(list));
154+
}
155+
138156
/**
139157
* Creates a mutable defensive copy of a nullable list.
140158
* <p>

‎spec/src/test/java/org/a2aproject/sdk/spec/DataPartTest.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
55
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertSame;
67
import static org.junit.jupiter.api.Assertions.assertThrows;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
89

10+
import java.util.ArrayList;
911
import java.util.HashMap;
1012
import java.util.List;
1113
import java.util.Map;
@@ -108,4 +110,62 @@ void testFromJson_nullLiteralThrows() {
108110
void testFromJson_invalidJsonThrows() {
109111
assertThrows(IllegalArgumentException.class, () -> DataPart.fromJson("{invalid}"));
110112
}
113+
114+
@SuppressWarnings("unchecked")
115+
@Test
116+
void testDataMapIsDefensivelyCopiedAndImmutable() {
117+
Map<String, Object> data = new HashMap<>();
118+
data.put("temperature", 22.5);
119+
120+
DataPart part = new DataPart(data);
121+
122+
assertThrows(UnsupportedOperationException.class, () -> ((Map<String, Object>) part.data()).put("humidity", 65));
123+
data.put("humidity", 65);
124+
assertEquals(Map.of("temperature", 22.5), part.data());
125+
}
126+
127+
@Test
128+
void testDataListIsDefensivelyCopiedAndImmutable() {
129+
List<Object> data = new ArrayList<>();
130+
data.add("a");
131+
132+
DataPart part = new DataPart(data);
133+
134+
assertThrows(UnsupportedOperationException.class, () -> ((List<Object>) part.data()).add("b"));
135+
data.add("b");
136+
assertEquals(List.of("a"), part.data());
137+
}
138+
139+
@Test
140+
void testDataMapPreservesNullValues() {
141+
Map<String, Object> data = new HashMap<>();
142+
data.put("source", null);
143+
144+
DataPart part = new DataPart(data);
145+
146+
assertTrue(part.data() instanceof Map);
147+
assertTrue(((Map<?, ?>) part.data()).containsKey("source"));
148+
assertNull(((Map<?, ?>) part.data()).get("source"));
149+
}
150+
151+
@Test
152+
void testDataListPreservesNullElements() {
153+
List<Object> data = new ArrayList<>();
154+
data.add(null);
155+
156+
DataPart part = new DataPart(data);
157+
158+
assertTrue(part.data() instanceof List);
159+
assertEquals(1, ((List<?>) part.data()).size());
160+
assertNull(((List<?>) part.data()).get(0));
161+
}
162+
163+
@Test
164+
void testDataPrimitiveStoredAsIs() {
165+
Integer data = 42;
166+
167+
DataPart part = new DataPart(data);
168+
169+
assertSame(data, part.data());
170+
}
111171
}

0 commit comments

Comments
 (0)