Skip to content

Commit ce36bdd

Browse files
chore: Use the same validation for all JSON RPC Request class (#530)
This fixes #522. --------- Signed-off-by: Jeff Mesnil <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 8ffef35 commit ce36bdd

16 files changed

+116
-282
lines changed

client/transport/jsonrpc/src/main/java/io/a2a/client/transport/jsonrpc/JSONRPCTransport.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ public AgentCard getAgentCard(@Nullable ClientCallContext context) throws A2ACli
303303

304304
GetAuthenticatedExtendedCardRequest getExtendedAgentCardRequest = new GetAuthenticatedExtendedCardRequest.Builder()
305305
.jsonrpc(JSONRPCMessage.JSONRPC_VERSION)
306-
.method(GetAuthenticatedExtendedCardRequest.METHOD)
307306
.build(); // id will be randomly generated
308307

309308
PayloadAndHeaders payloadAndHeaders = applyInterceptors(GetAuthenticatedExtendedCardRequest.METHOD,

spec-grpc/src/main/java/io/a2a/grpc/utils/JSONRPCUtils.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,55 +185,55 @@ private static JSONRPCRequest<?> parseMethodRequest(String version, Object id, S
185185
case GetTaskRequest.METHOD -> {
186186
io.a2a.grpc.GetTaskRequest.Builder builder = io.a2a.grpc.GetTaskRequest.newBuilder();
187187
parseRequestBody(paramsNode, builder);
188-
return new GetTaskRequest(version, id, method, ProtoUtils.FromProto.taskQueryParams(builder));
188+
return new GetTaskRequest(version, id, ProtoUtils.FromProto.taskQueryParams(builder));
189189
}
190190
case CancelTaskRequest.METHOD -> {
191191
io.a2a.grpc.CancelTaskRequest.Builder builder = io.a2a.grpc.CancelTaskRequest.newBuilder();
192192
parseRequestBody(paramsNode, builder);
193-
return new CancelTaskRequest(version, id, method, ProtoUtils.FromProto.taskIdParams(builder));
193+
return new CancelTaskRequest(version, id, ProtoUtils.FromProto.taskIdParams(builder));
194194
}
195195
case ListTasksRequest.METHOD -> {
196196
io.a2a.grpc.ListTasksRequest.Builder builder = io.a2a.grpc.ListTasksRequest.newBuilder();
197197
parseRequestBody(paramsNode, builder);
198-
return new ListTasksRequest(version, id, method, ProtoUtils.FromProto.listTasksParams(builder));
198+
return new ListTasksRequest(version, id, ProtoUtils.FromProto.listTasksParams(builder));
199199
}
200200
case SetTaskPushNotificationConfigRequest.METHOD -> {
201201
io.a2a.grpc.SetTaskPushNotificationConfigRequest.Builder builder = io.a2a.grpc.SetTaskPushNotificationConfigRequest.newBuilder();
202202
parseRequestBody(paramsNode, builder);
203-
return new SetTaskPushNotificationConfigRequest(version, id, method, ProtoUtils.FromProto.setTaskPushNotificationConfig(builder));
203+
return new SetTaskPushNotificationConfigRequest(version, id, ProtoUtils.FromProto.setTaskPushNotificationConfig(builder));
204204
}
205205
case GetTaskPushNotificationConfigRequest.METHOD -> {
206206
io.a2a.grpc.GetTaskPushNotificationConfigRequest.Builder builder = io.a2a.grpc.GetTaskPushNotificationConfigRequest.newBuilder();
207207
parseRequestBody(paramsNode, builder);
208-
return new GetTaskPushNotificationConfigRequest(version, id, method, ProtoUtils.FromProto.getTaskPushNotificationConfigParams(builder));
208+
return new GetTaskPushNotificationConfigRequest(version, id, ProtoUtils.FromProto.getTaskPushNotificationConfigParams(builder));
209209
}
210210
case SendMessageRequest.METHOD -> {
211211
io.a2a.grpc.SendMessageRequest.Builder builder = io.a2a.grpc.SendMessageRequest.newBuilder();
212212
parseRequestBody(paramsNode, builder);
213-
return new SendMessageRequest(version, id, method, ProtoUtils.FromProto.messageSendParams(builder));
213+
return new SendMessageRequest(version, id, ProtoUtils.FromProto.messageSendParams(builder));
214214
}
215215
case ListTaskPushNotificationConfigRequest.METHOD -> {
216216
io.a2a.grpc.ListTaskPushNotificationConfigRequest.Builder builder = io.a2a.grpc.ListTaskPushNotificationConfigRequest.newBuilder();
217217
parseRequestBody(paramsNode, builder);
218-
return new ListTaskPushNotificationConfigRequest(version, id, method, ProtoUtils.FromProto.listTaskPushNotificationConfigParams(builder));
218+
return new ListTaskPushNotificationConfigRequest(version, id, ProtoUtils.FromProto.listTaskPushNotificationConfigParams(builder));
219219
}
220220
case DeleteTaskPushNotificationConfigRequest.METHOD -> {
221221
io.a2a.grpc.DeleteTaskPushNotificationConfigRequest.Builder builder = io.a2a.grpc.DeleteTaskPushNotificationConfigRequest.newBuilder();
222222
parseRequestBody(paramsNode, builder);
223-
return new DeleteTaskPushNotificationConfigRequest(version, id, method, ProtoUtils.FromProto.deleteTaskPushNotificationConfigParams(builder));
223+
return new DeleteTaskPushNotificationConfigRequest(version, id, ProtoUtils.FromProto.deleteTaskPushNotificationConfigParams(builder));
224224
}
225225
case GetAuthenticatedExtendedCardRequest.METHOD -> {
226-
return new GetAuthenticatedExtendedCardRequest(version, id, method, null);
226+
return new GetAuthenticatedExtendedCardRequest(version, id);
227227
}
228228
case SendStreamingMessageRequest.METHOD -> {
229229
io.a2a.grpc.SendMessageRequest.Builder builder = io.a2a.grpc.SendMessageRequest.newBuilder();
230230
parseRequestBody(paramsNode, builder);
231-
return new SendStreamingMessageRequest(version, id, method, ProtoUtils.FromProto.messageSendParams(builder));
231+
return new SendStreamingMessageRequest(version, id, ProtoUtils.FromProto.messageSendParams(builder));
232232
}
233233
case SubscribeToTaskRequest.METHOD -> {
234234
io.a2a.grpc.SubscribeToTaskRequest.Builder builder = io.a2a.grpc.SubscribeToTaskRequest.newBuilder();
235235
parseRequestBody(paramsNode, builder);
236-
return new SubscribeToTaskRequest(version, id, method, ProtoUtils.FromProto.taskIdParams(builder));
236+
return new SubscribeToTaskRequest(version, id, ProtoUtils.FromProto.taskIdParams(builder));
237237
}
238238
default ->
239239
throw new MethodNotFoundJsonMappingException("Unsupported JSON-RPC method: '" + method + "'", id);

spec/src/main/java/io/a2a/spec/CancelTaskRequest.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package io.a2a.spec;
22

3-
import static io.a2a.util.Utils.defaultIfNull;
4-
53
import java.util.UUID;
64

7-
8-
import io.a2a.util.Assert;
9-
105
/**
116
* JSON-RPC request to cancel an in-progress task.
127
* <p>
@@ -34,24 +29,11 @@ public final class CancelTaskRequest extends NonStreamingJSONRPCRequest<TaskIdPa
3429
*
3530
* @param jsonrpc the JSON-RPC version (defaults to "2.0" if null)
3631
* @param id the request identifier (string, integer, or null)
37-
* @param method the method name (must be "CancelTask")
3832
* @param params the request parameters containing the task ID
3933
* @throws IllegalArgumentException if jsonrpc version is invalid, method is not "CancelTask", params is null, or id is not a String/Integer/null
4034
*/
41-
public CancelTaskRequest(String jsonrpc, Object id, String method, TaskIdParams params) {
42-
if (jsonrpc != null && ! JSONRPC_VERSION.equals(jsonrpc)) {
43-
throw new IllegalArgumentException("Invalid JSON-RPC protocol version");
44-
}
45-
Assert.checkNotNullParam("method", method);
46-
if (! method.equals(METHOD)) {
47-
throw new IllegalArgumentException("Invalid CancelTaskRequest method");
48-
}
49-
Assert.checkNotNullParam("params", params);
50-
Assert.isNullOrStringOrInteger(id);
51-
this.jsonrpc = defaultIfNull(jsonrpc, JSONRPC_VERSION);
52-
this.id = id;
53-
this.method = method;
54-
this.params = params;
35+
public CancelTaskRequest(String jsonrpc, Object id, TaskIdParams params) {
36+
super(jsonrpc, METHOD, id, params);
5537
}
5638

5739
/**
@@ -62,7 +44,7 @@ public CancelTaskRequest(String jsonrpc, Object id, String method, TaskIdParams
6244
* @throws IllegalArgumentException if params is null or id is not a string/integer/null
6345
*/
6446
public CancelTaskRequest(Object id, TaskIdParams params) {
65-
this(null, id, METHOD, params);
47+
this(null, id, params);
6648
}
6749

6850
/**
@@ -74,7 +56,6 @@ public CancelTaskRequest(Object id, TaskIdParams params) {
7456
public static class Builder {
7557
private String jsonrpc;
7658
private Object id;
77-
private String method = METHOD;
7859
private TaskIdParams params;
7960

8061
/**
@@ -104,9 +85,9 @@ public CancelTaskRequest.Builder id(Object id) {
10485
*
10586
* @param method the method name (should be "CancelTask")
10687
* @return this builder for method chaining
88+
* @deprecated
10789
*/
10890
public CancelTaskRequest.Builder method(String method) {
109-
this.method = method;
11091
return this;
11192
}
11293

@@ -133,7 +114,7 @@ public CancelTaskRequest build() {
133114
if (id == null) {
134115
id = UUID.randomUUID().toString();
135116
}
136-
return new CancelTaskRequest(jsonrpc, id, method, params);
117+
return new CancelTaskRequest(jsonrpc, id, params);
137118
}
138119
}
139120
}

spec/src/main/java/io/a2a/spec/DeleteTaskPushNotificationConfigRequest.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import java.util.UUID;
44

5-
import io.a2a.util.Assert;
6-
import io.a2a.util.Utils;
7-
85
/**
96
* JSON-RPC request to delete a push notification configuration from a task.
107
* <p>
@@ -28,23 +25,11 @@ public final class DeleteTaskPushNotificationConfigRequest extends NonStreamingJ
2825
*
2926
* @param jsonrpc the JSON-RPC version (defaults to "2.0" if null)
3027
* @param id the request identifier (string, integer, or null)
31-
* @param method the method name (must be "DeleteTaskPushNotificationConfig")
3228
* @param params the request parameters containing task and config IDs
3329
* @throws IllegalArgumentException if jsonrpc version is invalid, method is not "DeleteTaskPushNotificationConfig", or id is not a string/integer/null
3430
*/
35-
public DeleteTaskPushNotificationConfigRequest(String jsonrpc, Object id, String method, DeleteTaskPushNotificationConfigParams params) {
36-
if (jsonrpc != null && ! jsonrpc.equals(JSONRPC_VERSION)) {
37-
throw new IllegalArgumentException("Invalid JSON-RPC protocol version");
38-
}
39-
Assert.checkNotNullParam("method", method);
40-
if (! method.equals(METHOD)) {
41-
throw new IllegalArgumentException("Invalid DeleteTaskPushNotificationConfigRequest method");
42-
}
43-
Assert.isNullOrStringOrInteger(id);
44-
this.jsonrpc = Utils.defaultIfNull(jsonrpc, JSONRPC_VERSION);
45-
this.id = id;
46-
this.method = method;
47-
this.params = params;
31+
public DeleteTaskPushNotificationConfigRequest(String jsonrpc, Object id, DeleteTaskPushNotificationConfigParams params) {
32+
super(jsonrpc, METHOD, id, params);
4833
}
4934

5035
/**
@@ -55,7 +40,7 @@ public DeleteTaskPushNotificationConfigRequest(String jsonrpc, Object id, String
5540
* @throws IllegalArgumentException if id is not a string/integer/null
5641
*/
5742
public DeleteTaskPushNotificationConfigRequest(String id, DeleteTaskPushNotificationConfigParams params) {
58-
this(null, id, METHOD, params);
43+
this(null, id, params);
5944
}
6045

6146
/**
@@ -67,7 +52,6 @@ public DeleteTaskPushNotificationConfigRequest(String id, DeleteTaskPushNotifica
6752
public static class Builder {
6853
private String jsonrpc;
6954
private Object id;
70-
private String method;
7155
private DeleteTaskPushNotificationConfigParams params;
7256

7357
/**
@@ -97,9 +81,10 @@ public Builder id(Object id) {
9781
*
9882
* @param method the method name (should be "DeleteTaskPushNotificationConfig")
9983
* @return this builder for method chaining
84+
85+
* @deprecated
10086
*/
10187
public Builder method(String method) {
102-
this.method = method;
10388
return this;
10489
}
10590

@@ -126,7 +111,7 @@ public DeleteTaskPushNotificationConfigRequest build() {
126111
if (id == null) {
127112
id = UUID.randomUUID().toString();
128113
}
129-
return new DeleteTaskPushNotificationConfigRequest(jsonrpc, id, method, params);
114+
return new DeleteTaskPushNotificationConfigRequest(jsonrpc, id, params);
130115
}
131116
}
132117
}

spec/src/main/java/io/a2a/spec/GetAuthenticatedExtendedCardRequest.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
import java.util.UUID;
44

5-
6-
import io.a2a.util.Assert;
7-
import io.a2a.util.Utils;
8-
95
/**
106
* JSON-RPC request to retrieve an agent's extended card with authenticated details.
117
* <p>
@@ -34,29 +30,17 @@ public final class GetAuthenticatedExtendedCardRequest extends NonStreamingJSONR
3430

3531
public static final String METHOD = "GetExtendedAgentCard";
3632

37-
public GetAuthenticatedExtendedCardRequest(String jsonrpc, Object id, String method, Void params) {
38-
if (jsonrpc != null && ! jsonrpc.equals(JSONRPC_VERSION)) {
39-
throw new IllegalArgumentException("Invalid JSON-RPC protocol version");
40-
}
41-
Assert.checkNotNullParam("method", method);
42-
if (! method.equals(METHOD)) {
43-
throw new IllegalArgumentException("Invalid GetAuthenticatedExtendedCardRequest method");
44-
}
45-
Assert.isNullOrStringOrInteger(id);
46-
this.jsonrpc = Utils.defaultIfNull(jsonrpc, JSONRPC_VERSION);
47-
this.id = id;
48-
this.method = method;
49-
this.params = params;
33+
public GetAuthenticatedExtendedCardRequest(String jsonrpc, Object id) {
34+
super(jsonrpc, METHOD, id);
5035
}
5136

5237
public GetAuthenticatedExtendedCardRequest(String id) {
53-
this(null, id, METHOD, null);
38+
this(null, id);
5439
}
5540

5641
public static class Builder {
5742
private String jsonrpc;
5843
private Object id;
59-
private String method;
6044

6145
public GetAuthenticatedExtendedCardRequest.Builder jsonrpc(String jsonrpc) {
6246
this.jsonrpc = jsonrpc;
@@ -68,16 +52,18 @@ public GetAuthenticatedExtendedCardRequest.Builder id(Object id) {
6852
return this;
6953
}
7054

55+
/**
56+
* @deprecated
57+
*/
7158
public GetAuthenticatedExtendedCardRequest.Builder method(String method) {
72-
this.method = method;
7359
return this;
7460
}
7561

7662
public GetAuthenticatedExtendedCardRequest build() {
7763
if (id == null) {
7864
id = UUID.randomUUID().toString();
7965
}
80-
return new GetAuthenticatedExtendedCardRequest(jsonrpc, id, method, null);
66+
return new GetAuthenticatedExtendedCardRequest(jsonrpc, id);
8167
}
8268
}
8369
}

spec/src/main/java/io/a2a/spec/GetTaskPushNotificationConfigRequest.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.a2a.spec;
22

3-
import io.a2a.util.Assert;
4-
import io.a2a.util.Utils;
5-
63
import java.util.UUID;
74

85
/**
@@ -22,29 +19,17 @@ public final class GetTaskPushNotificationConfigRequest extends NonStreamingJSON
2219

2320
public static final String METHOD = "GetTaskPushNotificationConfig";
2421

25-
public GetTaskPushNotificationConfigRequest( String jsonrpc, Object id, String method, GetTaskPushNotificationConfigParams params) {
26-
if (jsonrpc != null && ! jsonrpc.equals(JSONRPC_VERSION)) {
27-
throw new IllegalArgumentException("Invalid JSON-RPC protocol version");
28-
}
29-
Assert.checkNotNullParam("method", method);
30-
if (! method.equals(METHOD)) {
31-
throw new IllegalArgumentException("Invalid GetTaskPushNotificationRequest method");
32-
}
33-
Assert.isNullOrStringOrInteger(id);
34-
this.jsonrpc = Utils.defaultIfNull(jsonrpc, JSONRPC_VERSION);
35-
this.id = id;
36-
this.method = method;
37-
this.params = params;
22+
public GetTaskPushNotificationConfigRequest(String jsonrpc, Object id, GetTaskPushNotificationConfigParams params) {
23+
super(jsonrpc, METHOD, id, params);
3824
}
3925

4026
public GetTaskPushNotificationConfigRequest(String id, GetTaskPushNotificationConfigParams params) {
41-
this(null, id, METHOD, params);
27+
this(null, id, params);
4228
}
4329

4430
public static class Builder {
4531
private String jsonrpc;
4632
private Object id;
47-
private String method;
4833
private GetTaskPushNotificationConfigParams params;
4934

5035
public GetTaskPushNotificationConfigRequest.Builder jsonrpc(String jsonrpc) {
@@ -57,8 +42,10 @@ public GetTaskPushNotificationConfigRequest.Builder id(Object id) {
5742
return this;
5843
}
5944

45+
/**
46+
* @deprecated
47+
*/
6048
public GetTaskPushNotificationConfigRequest.Builder method(String method) {
61-
this.method = method;
6249
return this;
6350
}
6451

@@ -71,7 +58,7 @@ public GetTaskPushNotificationConfigRequest build() {
7158
if (id == null) {
7259
id = UUID.randomUUID().toString();
7360
}
74-
return new GetTaskPushNotificationConfigRequest(jsonrpc, id, method, params);
61+
return new GetTaskPushNotificationConfigRequest(jsonrpc, id, params);
7562
}
7663
}
7764
}

0 commit comments

Comments
 (0)