Skip to content

Commit 00e6093

Browse files
authored
fix: handle listTasks status wire strings (#578)
What changed - Parse status in REST listTasks using TaskState.fromString - Return InvalidParamsError (422) for invalid status values - Add tests for valid wire strings and invalid status Why - REST API expects wire strings like working / input-required, but valueOf only accepts enum names, causing valid requests to return 500. Tests ```bash mvn -pl transport/rest -am test ``` --- # Description Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Follow the [`CONTRIBUTING` Guide](../CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - Important Prefixes for [release-please](https://github.com/googleapis/release-please): - `fix:` which represents bug fixes, and correlates to a [SemVer](https://semver.org/) patch. - `feat:` represents a new feature, and correlates to a SemVer minor. - `feat!:`, or `fix!:`, `refactor!:`, etc., which represent a breaking change (indicated by the `!`) and will result in a SemVer major. - [x] Ensure the tests pass - [x] Appropriate READMEs were updated (if necessary) Fixes #577 🦕
1 parent cf82353 commit 00e6093

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

transport/rest/src/main/java/io/a2a/transport/rest/handler/RestHandler.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
import java.time.Instant;
77
import java.time.format.DateTimeParseException;
8+
import java.util.Arrays;
89
import java.util.HashMap;
910
import java.util.Map;
1011
import java.util.concurrent.CompletableFuture;
1112
import java.util.concurrent.Executor;
1213
import java.util.concurrent.Flow;
1314
import java.util.logging.Level;
1415
import java.util.logging.Logger;
16+
import java.util.stream.Collectors;
1517

1618
import jakarta.enterprise.context.ApplicationScoped;
1719
import jakarta.enterprise.inject.Instance;
@@ -217,7 +219,21 @@ public HTTPRestResponse listTasks(@Nullable String contextId, @Nullable String s
217219
paramsBuilder.contextId(contextId);
218220
}
219221
if (status != null) {
220-
paramsBuilder.status(TaskState.valueOf(status));
222+
try {
223+
paramsBuilder.status(TaskState.fromString(status));
224+
} catch (IllegalArgumentException e) {
225+
try {
226+
paramsBuilder.status(TaskState.valueOf(status));
227+
} catch (IllegalArgumentException valueOfError) {
228+
String validStates = Arrays.stream(TaskState.values())
229+
.map(TaskState::asString)
230+
.collect(Collectors.joining(", "));
231+
Map<String, Object> errorData = new HashMap<>();
232+
errorData.put("parameter", "status");
233+
errorData.put("reason", "Must be one of: " + validStates);
234+
throw new InvalidParamsError(null, "Invalid params", errorData);
235+
}
236+
}
221237
}
222238
if (pageSize != null) {
223239
paramsBuilder.pageSize(pageSize);

transport/rest/src/test/java/io/a2a/transport/rest/handler/RestHandlerTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ public void testGetTaskNotFound() {
5858
Assertions.assertTrue(response.getBody().contains("TaskNotFoundError"));
5959
}
6060

61+
@Test
62+
public void testListTasksStatusWireString() {
63+
RestHandler handler = new RestHandler(CARD, requestHandler, internalExecutor);
64+
taskStore.save(MINIMAL_TASK);
65+
66+
RestHandler.HTTPRestResponse response = handler.listTasks(null, "submitted", null, null,
67+
null, null, null, "", callContext);
68+
69+
Assertions.assertEquals(200, response.getStatusCode());
70+
Assertions.assertEquals("application/json", response.getContentType());
71+
Assertions.assertTrue(response.getBody().contains(MINIMAL_TASK.id()));
72+
}
73+
74+
@Test
75+
public void testListTasksInvalidStatus() {
76+
RestHandler handler = new RestHandler(CARD, requestHandler, internalExecutor);
77+
78+
RestHandler.HTTPRestResponse response = handler.listTasks(null, "not-a-status", null, null,
79+
null, null, null, "", callContext);
80+
81+
Assertions.assertEquals(422, response.getStatusCode());
82+
Assertions.assertEquals("application/json", response.getContentType());
83+
Assertions.assertTrue(response.getBody().contains("InvalidParamsError"));
84+
}
85+
6186
@Test
6287
public void testSendMessage() throws InvalidProtocolBufferException {
6388
RestHandler handler = new RestHandler(CARD, requestHandler, internalExecutor);

0 commit comments

Comments
 (0)