mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(stepfunctions): serialize Scheduler target input (#4168)
Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
1 parent
3cf6c76811
commit
1abc817c5d
4 files changed
+270
-1
No files matched your search
@@ -442,6 +442,8 @@ the wire and the task fails with `Sfn.StateMachineDoesNotExistException`.
|
||||
|
||||
Scheduler create and update tasks accept `StartDate` and `EndDate` as RFC 3339 strings, including
|
||||
offsets and fractional seconds. The direct Scheduler API continues to use numeric epoch seconds.
|
||||
Structured JSON values supplied as `Target.Input` are serialized once to the Scheduler API's string
|
||||
field. Textual JSON remains unchanged, and malformed text reaches the existing Scheduler validation.
|
||||
|
||||
`sendTaskSuccess` and `sendTaskFailure` resolve a token a `.waitForTaskToken` task is parked on. A
|
||||
token nobody is waiting for fails the calling task rather than reporting a delivery that never
|
||||
|
||||
@@ -1442,16 +1442,29 @@ public class AslExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
/** Converts the SDK's RFC 3339 timestamps to the epoch-second values used by the wire parser. */
|
||||
/** Converts SDK task values to the representations used by the Scheduler wire parser. */
|
||||
private JsonNode normalizeAwsSdkSchedulerInput(JsonNode input) {
|
||||
JsonNode normalized = input.deepCopy();
|
||||
if (normalized instanceof ObjectNode object) {
|
||||
normalizeAwsSdkSchedulerTimestamp(object, "StartDate");
|
||||
normalizeAwsSdkSchedulerTimestamp(object, "EndDate");
|
||||
normalizeAwsSdkSchedulerTargetInput(object);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private void normalizeAwsSdkSchedulerTargetInput(ObjectNode input) {
|
||||
JsonNode target = input.get("Target");
|
||||
if (!(target instanceof ObjectNode targetObject)) {
|
||||
return;
|
||||
}
|
||||
JsonNode value = targetObject.get("Input");
|
||||
if (value == null || value.isNull() || value.isTextual()) {
|
||||
return;
|
||||
}
|
||||
targetObject.put("Input", value.toString());
|
||||
}
|
||||
|
||||
private void normalizeAwsSdkSchedulerTimestamp(ObjectNode input, String field) {
|
||||
JsonNode value = input.get(field);
|
||||
if (value == null || value.isNull() || value.isNumber()) {
|
||||
|
||||
+53
@@ -163,6 +163,59 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
assertEquals(0, listExecutions(targetStateMachineArn).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkTaskStructuredInputReachesDueStateMachineTarget() {
|
||||
String targetStateMachineArn = createPassStateMachine();
|
||||
String scheduleName = uniqueName("sfn-sdk-input");
|
||||
String expectedInput = "{\"scheduleId\":\"schedule-123\",\"nested\":{\"quote\":\"a\\\"b\"}}";
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
String creatorStateMachineArn = createStateMachine(uniqueName("schedule-creator"), """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Create",
|
||||
"States": {
|
||||
"Create": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:scheduler:createSchedule",
|
||||
"Arguments": {
|
||||
"Name": "SCHEDULE_NAME",
|
||||
"ScheduleExpression": "SCHEDULE_EXPRESSION",
|
||||
"FlexibleTimeWindow": {"Mode": "OFF"},
|
||||
"Target": {
|
||||
"Arn": "TARGET_ARN",
|
||||
"RoleArn": "ROLE",
|
||||
"Input": {
|
||||
"scheduleId": "schedule-123",
|
||||
"nested": {"quote": "a\\\"b"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
.replace("SCHEDULE_NAME", scheduleName)
|
||||
.replace("SCHEDULE_EXPRESSION", "at(" + SCHEDULE_TIME.format(fireAt) + ")")
|
||||
.replace("TARGET_ARN", targetStateMachineArn)
|
||||
.replace("ROLE", ROLE_ARN));
|
||||
|
||||
Response creation = waitForTerminalExecution(startExecution(creatorStateMachineArn));
|
||||
assertEquals("SUCCEEDED", creation.jsonPath().getString("status"));
|
||||
scheduleNames.add(scheduleName);
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
assertEquals(expectedInput, schedule.getTarget().getInput());
|
||||
|
||||
dispatcherFor(scheduleName).tick(fireAt);
|
||||
|
||||
List<Map<String, Object>> executions = listExecutions(targetStateMachineArn);
|
||||
assertEquals(1, executions.size());
|
||||
Response targetExecution = waitForTerminalExecution((String) executions.get(0).get("executionArn"));
|
||||
assertEquals("SUCCEEDED", targetExecution.jsonPath().getString("status"));
|
||||
assertEquals(expectedInput, targetExecution.jsonPath().getString("input"));
|
||||
assertEquals(expectedInput, targetExecution.jsonPath().getString("output"));
|
||||
}
|
||||
|
||||
private String createPassStateMachine() {
|
||||
String name = uniqueName("scheduled-workflow");
|
||||
String definition = "{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}";
|
||||
|
||||
+201
@@ -645,6 +645,207 @@ class StepFunctionsAwsSdkTaskIntegrationTest {
|
||||
assertEquals("Scheduler.SerializationException", result.path("caughtError").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void createScheduleSerializesStructuredTargetInputFromJsonata() throws Exception {
|
||||
String targetStateMachineArn = createStateMachine("aws-sdk-structured-input-target", """
|
||||
{
|
||||
"StartAt": "Done",
|
||||
"States": {"Done": {"Type": "Pass", "End": true}}
|
||||
}
|
||||
""");
|
||||
String stateMachineArn = createStateMachine("aws-sdk-create-schedule-structured-input", """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Schedule",
|
||||
"States": {
|
||||
"Schedule": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:scheduler:createSchedule",
|
||||
"Arguments": {
|
||||
"Name": "payout-structured-input",
|
||||
"ScheduleExpression": "rate(1 day)",
|
||||
"FlexibleTimeWindow": {"Mode": "OFF"},
|
||||
"Target": {
|
||||
"Arn": "TARGET_ARN",
|
||||
"RoleArn": "ROLE",
|
||||
"Input": {
|
||||
"scheduleId": "schedule-123",
|
||||
"nested": {
|
||||
"items": [1, true, "a\\\"b"]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".replace("TARGET_ARN", targetStateMachineArn).replace("ROLE", ROLE_ARN));
|
||||
|
||||
succeedingOutputOf(stateMachineArn, "{}");
|
||||
|
||||
JsonNode schedule = mapper.readTree(given().when().get("/schedules/payout-structured-input")
|
||||
.then().statusCode(200).extract().body().asString());
|
||||
JsonNode expectedInput = mapper.readTree("""
|
||||
{"scheduleId":"schedule-123","nested":{"items":[1,true,"a\\\"b"]}}
|
||||
""");
|
||||
assertEquals(expectedInput, mapper.readTree(schedule.path("Target").path("Input").asText()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(24)
|
||||
void updateScheduleSerializesStructuredTargetInputFromJsonPath() throws Exception {
|
||||
String scheduleName = "payout-structured-input-update";
|
||||
String targetStateMachineArn = createStateMachine("aws-sdk-structured-update-target", """
|
||||
{
|
||||
"StartAt": "Done",
|
||||
"States": {"Done": {"Type": "Pass", "End": true}}
|
||||
}
|
||||
""");
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"ScheduleExpression": "rate(1 day)",
|
||||
"FlexibleTimeWindow": {"Mode": "OFF"},
|
||||
"Target": {"Arn": "TARGET_ARN", "RoleArn": "ROLE", "Input": "{}"}
|
||||
}
|
||||
""".replace("TARGET_ARN", targetStateMachineArn).replace("ROLE", ROLE_ARN))
|
||||
.when().post("/schedules/" + scheduleName)
|
||||
.then().statusCode(200);
|
||||
String stateMachineArn = createStateMachine("aws-sdk-update-schedule-structured-input", """
|
||||
{
|
||||
"StartAt": "Schedule",
|
||||
"States": {
|
||||
"Schedule": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:scheduler:updateSchedule",
|
||||
"Parameters": {
|
||||
"Name": "SCHEDULE_NAME",
|
||||
"ScheduleExpression": "rate(2 days)",
|
||||
"FlexibleTimeWindow": {"Mode": "OFF"},
|
||||
"Target": {
|
||||
"Arn": "TARGET_ARN",
|
||||
"RoleArn": "ROLE",
|
||||
"Input": {"enabled": true, "retries": 3, "tags": ["a", "b"]}
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
.replace("SCHEDULE_NAME", scheduleName)
|
||||
.replace("TARGET_ARN", targetStateMachineArn)
|
||||
.replace("ROLE", ROLE_ARN));
|
||||
|
||||
succeedingOutputOf(stateMachineArn, "{}");
|
||||
|
||||
JsonNode schedule = mapper.readTree(given().when().get("/schedules/" + scheduleName)
|
||||
.then().statusCode(200).extract().body().asString());
|
||||
JsonNode expectedInput = mapper.readTree("""
|
||||
{"enabled":true,"retries":3,"tags":["a","b"]}
|
||||
""");
|
||||
assertEquals(expectedInput, mapper.readTree(schedule.path("Target").path("Input").asText()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(25)
|
||||
void createScheduleSerializesJsonScalarsAndPreservesTextualJson() {
|
||||
String targetStateMachineArn = createStateMachine("aws-sdk-scalar-input-target", """
|
||||
{
|
||||
"StartAt": "Done",
|
||||
"States": {"Done": {"Type": "Pass", "End": true}}
|
||||
}
|
||||
""");
|
||||
String[][] cases = {
|
||||
{"array", "[1,false,{\"key\":\"value\"}]", "[1,false,{\"key\":\"value\"}]"},
|
||||
{"number", "42.5", "42.5"},
|
||||
{"boolean", "true", "true"},
|
||||
{"text", "\"{\\\"already\\\":true}\"", "{\"already\":true}"}
|
||||
};
|
||||
for (String[] inputCase : cases) {
|
||||
String scheduleName = "payout-input-" + inputCase[0];
|
||||
String stateMachineArn = createStateMachine("aws-sdk-create-input-" + inputCase[0], """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Schedule",
|
||||
"States": {
|
||||
"Schedule": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:scheduler:createSchedule",
|
||||
"Arguments": {
|
||||
"Name": "SCHEDULE_NAME",
|
||||
"ScheduleExpression": "rate(1 day)",
|
||||
"FlexibleTimeWindow": {"Mode": "OFF"},
|
||||
"Target": {
|
||||
"Arn": "TARGET_ARN",
|
||||
"RoleArn": "ROLE",
|
||||
"Input": INPUT_VALUE
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
.replace("SCHEDULE_NAME", scheduleName)
|
||||
.replace("TARGET_ARN", targetStateMachineArn)
|
||||
.replace("ROLE", ROLE_ARN)
|
||||
.replace("INPUT_VALUE", inputCase[1]));
|
||||
|
||||
succeedingOutputOf(stateMachineArn, "{}");
|
||||
|
||||
Response schedule = given().when().get("/schedules/" + scheduleName);
|
||||
schedule.then().statusCode(200);
|
||||
assertEquals(inputCase[2], schedule.jsonPath().getString("Target.Input"), inputCase[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(26)
|
||||
void createScheduleRejectsMalformedTextualTargetInput() throws Exception {
|
||||
String targetStateMachineArn = createStateMachine("aws-sdk-malformed-input-target", """
|
||||
{
|
||||
"StartAt": "Done",
|
||||
"States": {"Done": {"Type": "Pass", "End": true}}
|
||||
}
|
||||
""");
|
||||
String stateMachineArn = createStateMachine("aws-sdk-create-malformed-input", """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Schedule",
|
||||
"States": {
|
||||
"Schedule": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:scheduler:createSchedule",
|
||||
"Arguments": {
|
||||
"Name": "payout-malformed-input",
|
||||
"ScheduleExpression": "rate(1 day)",
|
||||
"FlexibleTimeWindow": {"Mode": "OFF"},
|
||||
"Target": {
|
||||
"Arn": "TARGET_ARN",
|
||||
"RoleArn": "ROLE",
|
||||
"Input": "{not-json"
|
||||
}
|
||||
},
|
||||
"Catch": [{
|
||||
"ErrorEquals": ["Scheduler.ValidationException"],
|
||||
"Next": "Recovered",
|
||||
"Output": {"caughtError": "{% $states.errorOutput.Error %}"}
|
||||
}],
|
||||
"End": true
|
||||
},
|
||||
"Recovered": {"Type": "Pass", "End": true}
|
||||
}
|
||||
}
|
||||
""".replace("TARGET_ARN", targetStateMachineArn).replace("ROLE", ROLE_ARN));
|
||||
|
||||
JsonNode result = mapper.readTree(succeedingOutputOf(stateMachineArn, "{}"));
|
||||
assertEquals("Scheduler.ValidationException", result.path("caughtError").asText());
|
||||
}
|
||||
|
||||
private static String deleteScheduleTask(String scheduleName, String groupName) {
|
||||
String groupArgument = groupName == null ? "" : ", \"GroupName\": \"" + groupName + "\"";
|
||||
return """
|
||||
|
||||
Reference in new issue
Block a user