feat(sfn): Map ItemReader with s3:listObjectsV2 (#3460)

* feat(sfn): support the s3:listObjectsV2 Distributed Map ItemReader

The Map state listed the resource as accepted at CreateStateMachine
but failed every execution with States.ItemReaderFailed. It now calls
the S3 service in process, reads every page under Prefix, and gives
each child the AWS item shape: Etag, Key, LastModified, Size and
StorageClass. Bucket and Prefix come from Parameters or JSONata
Arguments. An empty prefix succeeds with zero iterations and
ReaderConfig.MaxItems trims the list.

LastModified is epoch seconds. Real AWS renders it as a double in
JSONPath state machines and as an integer in JSONata ones, and the
emulator does the same.

Fixes #3409

* fix(sfn): evaluate ItemSelector in JSONata Map states

A JSONata Map state passed ItemSelector through the JSONPath resolver,
so every {% %} expression reached the child as a literal string. The
Map state now resolves ItemSelector as a JSONata template with
$states.input bound to the Map input and $states.context.Map.Item
carrying Value and Index, as on AWS.

Real AWS evaluates ItemSelector before it records MapIterationStarted.
A failing expression therefore fails the Map state without any event
for that iteration, in both query languages. The evaluation moved
ahead of the iteration event to match.

Verified against real Step Functions in us-east-1 on 2026-09-12.

* chore(sfn): simplify the listObjectsV2 reader and the ItemSelector failure path

The reader listed the bucket page by page although the S3 service
returns the whole sorted listing in one call. The Map state kept two
copies of the iteration failure handling. The tests reuse the shared
mapper and one JSONata Map definition.

* perf(sfn): cap the listObjectsV2 listing at MaxItems

ReaderConfig.MaxItems keeps the first keys in key order on AWS, which
is what the S3 service returns for maxKeys. The reader now passes
MaxItems to the listing instead of building every item and trimming
afterwards.

* chore(sfn): declare local types in the listObjectsV2 reader

Replace var with explicit types in the production code this branch added.

* chore(sfn): declare local types in the listObjectsV2 reader tests

Replace var with explicit types in the test code this branch added.

* fix(sfn): list every object when MaxItems is absent

S3Service now treats max keys 0 as a request for an empty page, as AWS
does for max-keys=0. The listObjectsV2 reader passed 0 when
ReaderConfig.MaxItems was not set, so the Map got no items. It now asks
for Integer.MAX_VALUE in that case, like the DynamoDB import path.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-15 21:48:54 -04:00
1 parent c317fa52d2
commit 5db516d9d5
3 files changed
+371 -26

No files matched your search

@@ -2407,15 +2407,22 @@ public class AslExecutor {
long startMs = hasResultWriter ? System.currentTimeMillis() : 0L;
JsonNode branchOutput;
// AWS evaluates ItemSelector before it records MapIterationStarted, so a failing
// expression fails the Map state without any event for that iteration.
boolean iterationStarted = false;
try {
if (!distributed) {
iterationChain.publish("MapIterationStarted", Map.of("name", name, "index", i));
}
JsonNode iterInput = item;
if (!batchedChild && itemTransform != null) {
// $ in ItemSelector resolves against the Map state's effective input, not the item.
iterInput = resolveParameters(itemTransform, mapInput, iterContext);
iterInput = jsonata
? jsonataEvaluator.resolveTemplate(itemTransform, "ItemSelector",
buildStatesVar(mapInput, null, iterContext), variables)
: resolveParameters(itemTransform, mapInput, iterContext);
}
if (!distributed) {
iterationChain.publish("MapIterationStarted", Map.of("name", name, "index", i));
}
iterationStarted = true;
if (hasResultWriter) {
childInputsByIndex[i] = iterInput;
}
@@ -2429,7 +2436,7 @@ public class AslExecutor {
} catch (FailStateException e) {
int failedSoFar = failedItems.addAndGet(itemsInChild);
failedExecutions.incrementAndGet();
if (!distributed && !e.isRuntimeError()) {
if (iterationStarted && !distributed && !e.isRuntimeError()) {
iterationChain.publishAside("MapIterationFailed", Map.of("name", name, "index", i));
}
if (!tolerated.declared()) {
@@ -3063,8 +3070,7 @@ public class AslExecutor {
ObjectNode variables) throws Exception {
String resource = itemReader.path("Resource").asText(null);
if ("arn:aws:states:::s3:listObjectsV2".equals(resource)) {
throw new FailStateException("States.ItemReaderFailed",
"ItemReader resource arn:aws:states:::s3:listObjectsV2 is not yet implemented by the emulator");
return resolveListObjectsItems(itemReader, input, context, jsonata, variables);
}
if (!"arn:aws:states:::s3:getObject".equals(resource)) {
throw new FailStateException("States.Runtime", "Unsupported ItemReader resource: " + resource);
@@ -3076,15 +3082,7 @@ public class AslExecutor {
"ItemReader InputType " + inputType + " is not yet implemented by the emulator");
}
JsonNode resolvedParameters;
if (jsonata && itemReader.has("Arguments")) {
JsonNode statesVar = buildStatesVar(input, null, context);
resolvedParameters = jsonataEvaluator.resolveTemplate(
itemReader.get("Arguments"), "ItemReader/Arguments", statesVar, variables);
} else {
JsonNode parameters = itemReader.path("Parameters");
resolvedParameters = resolveParameters(parameters, input, context);
}
JsonNode resolvedParameters = resolveItemReaderParameters(itemReader, input, context, jsonata, variables);
String bucket = resolvedParameters.path("Bucket").asText(null);
String key = resolvedParameters.path("Key").asText(null);
if (bucket == null || key == null) {
@@ -3114,6 +3112,56 @@ public class AslExecutor {
}
}
private JsonNode resolveItemReaderParameters(JsonNode itemReader, JsonNode input, JsonNode context,
boolean jsonata, ObjectNode variables) throws Exception {
if (jsonata && itemReader.has("Arguments")) {
JsonNode statesVar = buildStatesVar(input, null, context);
return jsonataEvaluator.resolveTemplate(
itemReader.get("Arguments"), "ItemReader/Arguments", statesVar, variables);
}
return resolveParameters(itemReader.path("Parameters"), input, context);
}
private ResolvedMapItems resolveListObjectsItems(JsonNode itemReader, JsonNode input, JsonNode context,
boolean jsonata, ObjectNode variables) throws Exception {
JsonNode parameters = resolveItemReaderParameters(itemReader, input, context, jsonata, variables);
String bucket = parameters.path("Bucket").asText(null);
if (bucket == null) {
throw new FailStateException("States.Runtime", "ItemReader Parameters must include Bucket");
}
String prefix = parameters.path("Prefix").asText(null);
ArrayNode items = objectMapper.createArrayNode();
int maxItems = maxItems(itemReader);
try {
// MaxItems keeps the first keys in order, so the listing itself is capped.
for (S3Object object : s3Service.listObjects(bucket, prefix, null,
maxItems > 0 ? maxItems : Integer.MAX_VALUE)) {
items.add(listObjectsItem(object, jsonata));
}
} catch (AwsException e) {
throw new FailStateException("States.ItemReaderFailed", e.getMessage());
}
return new ResolvedMapItems(items, MapItemsSource.ITEM_READER_ARRAY);
}
// AWS renders LastModified as epoch seconds: a double in JSONPath state machines and an
// integer in JSONata ones.
private ObjectNode listObjectsItem(S3Object object, boolean jsonata) {
ObjectNode item = objectMapper.createObjectNode();
item.put("Etag", object.getETag());
item.put("Key", object.getKey());
long lastModified = object.getLastModified().getEpochSecond();
if (jsonata) {
item.put("LastModified", lastModified);
} else {
item.put("LastModified", (double) lastModified);
}
item.put("Size", object.getSize());
item.put("StorageClass", object.getStorageClass());
return item;
}
private ArrayNode normalizeObjectItems(JsonNode items) {
ArrayNode normalized = objectMapper.createArrayNode();
items.fields().forEachRemaining(entry -> {
@@ -3139,8 +3187,12 @@ public class AslExecutor {
return pointedItems;
}
private int maxItems(JsonNode itemReader) {
return itemReader.path("ReaderConfig").path("MaxItems").asInt(0);
}
private JsonNode applyMaxItems(JsonNode itemReader, JsonNode items) {
int maxItems = itemReader.path("ReaderConfig").path("MaxItems").asInt(0);
int maxItems = maxItems(itemReader);
if (maxItems <= 0 || !items.isArray() || items.size() <= maxItems) {
return items;
}