Skip to content

Commit 1c85118

Browse files
committed
fix: Sending an invalid message to the server raises an InternalServerError
* When the json is converted to an Object, internal validation may throw IllegalArgumentException which should be converted in a proper error. This fixes issue#512 Signed-off-by: Emmanuel Hugonnet <[email protected]>
1 parent 10bb13b commit 1c85118

File tree

7 files changed

+258
-97
lines changed

7 files changed

+258
-97
lines changed

http-client/src/main/java/io/a2a/client/http/A2ACardResolver.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import io.a2a.grpc.utils.JSONRPCUtils;
1010
import io.a2a.grpc.utils.ProtoUtils;
11+
import io.a2a.json.JsonProcessingException;
1112
import io.a2a.spec.A2AClientError;
1213
import io.a2a.spec.A2AClientJSONError;
1314
import io.a2a.spec.AgentCard;
@@ -105,13 +106,10 @@ public AgentCard getAgentCard() throws A2AClientError, A2AClientJSONError {
105106

106107
try {
107108
io.a2a.grpc.AgentCard.Builder agentCardBuilder = io.a2a.grpc.AgentCard.newBuilder();
108-
JSONRPCUtils.parseJsonString(body, agentCardBuilder);
109+
JSONRPCUtils.parseJsonString(body, agentCardBuilder, null);
109110
return ProtoUtils.FromProto.agentCard(agentCardBuilder);
110-
} catch (JSONRPCError e) {
111+
} catch (JSONRPCError | JsonProcessingException e) {
111112
throw new A2AClientJSONError("Could not unmarshal agent card response", e);
112113
}
113-
114114
}
115-
116-
117115
}

http-client/src/test/java/io/a2a/client/http/A2ACardResolverTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.google.protobuf.util.JsonFormat;
1313
import io.a2a.grpc.utils.JSONRPCUtils;
1414
import io.a2a.grpc.utils.ProtoUtils;
15+
import io.a2a.json.JsonProcessingException;
1516
import io.a2a.spec.A2AClientError;
1617
import io.a2a.spec.A2AClientJSONError;
1718
import io.a2a.spec.AgentCard;
@@ -79,9 +80,9 @@ public void testGetAgentCardSuccess() throws Exception {
7980
assertEquals(expected, requestCardString);
8081
}
8182

82-
private AgentCard unmarshalFrom(String body) {
83+
private AgentCard unmarshalFrom(String body) throws JsonProcessingException {
8384
io.a2a.grpc.AgentCard.Builder agentCardBuilder = io.a2a.grpc.AgentCard.newBuilder();
84-
JSONRPCUtils.parseJsonString(body, agentCardBuilder);
85+
JSONRPCUtils.parseJsonString(body, agentCardBuilder, null);
8586
return ProtoUtils.FromProto.agentCard(agentCardBuilder);
8687
}
8788

reference/jsonrpc/src/main/java/io/a2a/server/apps/quarkus/A2AServerRoutes.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
77
import static jakarta.ws.rs.core.MediaType.SERVER_SENT_EVENTS;
88

9+
import com.google.gson.JsonSyntaxException;
910
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Map;
@@ -39,6 +40,7 @@
3940
import io.a2a.spec.GetTaskPushNotificationConfigResponse;
4041
import io.a2a.spec.GetTaskRequest;
4142
import io.a2a.spec.GetTaskResponse;
43+
import io.a2a.spec.IdJsonMappingException;
4244
import io.a2a.spec.InternalError;
4345
import io.a2a.spec.InvalidParamsJsonMappingException;
4446
import io.a2a.spec.JSONParseError;
@@ -113,12 +115,12 @@ public void invokeJSONRPCHandler(@Body String body, RoutingContext rc) {
113115
error = new JSONRPCErrorResponse(e.getId(), new io.a2a.spec.InvalidParamsError(null, e.getMessage(), null));
114116
} catch (MethodNotFoundJsonMappingException e) {
115117
error = new JSONRPCErrorResponse(e.getId(), new io.a2a.spec.MethodNotFoundError(null, e.getMessage(), null));
116-
} catch (io.a2a.spec.IdJsonMappingException e) {
118+
} catch (IdJsonMappingException e) {
117119
error = new JSONRPCErrorResponse(e.getId(), new io.a2a.spec.InvalidRequestError(null, e.getMessage(), null));
118120
} catch (JsonMappingException e) {
119121
// General JsonMappingException - treat as InvalidRequest
120122
error = new JSONRPCErrorResponse(new io.a2a.spec.InvalidRequestError(null, e.getMessage(), null));
121-
} catch (com.google.gson.JsonSyntaxException e) {
123+
} catch (JsonSyntaxException e) {
122124
error = new JSONRPCErrorResponse(new JSONParseError(e.getMessage()));
123125
} catch (JsonProcessingException e) {
124126
error = new JSONRPCErrorResponse(new JSONParseError(e.getMessage()));

0 commit comments

Comments
 (0)