-
Notifications
You must be signed in to change notification settings - Fork 96
fix: Encapsulate the payload of Push notifications #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package io.a2a.server.tasks; | ||
|
|
||
| import static io.a2a.client.http.A2AHttpClient.APPLICATION_JSON; | ||
| import static io.a2a.client.http.A2AHttpClient.APPLICATION_JSON; | ||
| import static io.a2a.client.http.A2AHttpClient.CONTENT_TYPE; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
|
@@ -16,17 +16,18 @@ | |
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.a2a.client.http.A2AHttpClient; | ||
| import io.a2a.client.http.A2AHttpResponse; | ||
| import io.a2a.common.A2AHeaders; | ||
| import io.a2a.util.Utils; | ||
| import io.a2a.spec.PushNotificationConfig; | ||
| import io.a2a.spec.Task; | ||
| import io.a2a.spec.TaskState; | ||
| import io.a2a.spec.TaskStatus; | ||
| import io.a2a.util.Utils; | ||
|
|
||
| public class PushNotificationSenderTest { | ||
|
|
||
|
|
@@ -77,7 +78,11 @@ public A2AHttpResponse post() throws IOException, InterruptedException { | |
| } | ||
|
|
||
| try { | ||
| Task task = Utils.OBJECT_MAPPER.readValue(body, Task.TYPE_REFERENCE); | ||
| JsonNode root = Utils.OBJECT_MAPPER.readTree(body); | ||
| // This assumes there is always one field in the outer JSON object. | ||
| // This will need to be updated for #490 to unmarshall based on the kind of payload | ||
| JsonNode taskNode = root.elements().next(); | ||
| Task task = Utils.OBJECT_MAPPER.treeToValue(taskNode, Task.TYPE_REFERENCE); | ||
|
Comment on lines
80
to
+85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code for deserializing the wrapped task has a couple of areas for improvement:
To address both points and improve maintainability, you could extract this logic into a new, more robust helper method in // In Utils.java
public static <T> T unmarshalWrappedFrom(String data, TypeReference<T> typeRef) throws JsonProcessingException {
JsonNode root = OBJECT_MAPPER.readTree(data);
if (root.size() != 1) {
throw new com.fasterxml.jackson.databind.exc.MismatchedInputException(null, "Expected a single field in the JSON payload, but found " + root.size());
}
JsonNode payloadNode = root.elements().next();
return OBJECT_MAPPER.treeToValue(payloadNode, typeRef);
}Then you could simplify this block to just: tasks.add(Utils.unmarshalWrappedFrom(body, Task.TYPE_REFERENCE));
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idem, will be refined for #490 |
||
| tasks.add(task); | ||
| urls.add(url); | ||
| headers.add(new java.util.HashMap<>(requestHeaders)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code for deserializing the wrapped task has a couple of areas for improvement:
Robustness:
root.elements().next()will throw aNoSuchElementExceptionif the JSON object is empty. It's better to validate that the object contains exactly one field, as per the specification, for example by checkingroot.size().Code Duplication: This logic is duplicated in
PushNotificationSenderTest.java.To address both points and improve maintainability, you could extract this logic into a new, more robust helper method in
io.a2a.util.Utils. This method would be the inverse ofmarshalFrom. For example:Then you could simplify this block to just:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code will be refined for #490 when additional kind of payload will be supported and will follow these guidelines