fix(sfn): preserve undefined indexed constructed-array paths (#6613) (#8621)

This commit is contained in:
jwoa95 authored and GitHub committed 2026-11-16 16:08:14 -04:00
1 parent 16b0b516a0
commit 70921dea2c
8 files changed
+238 -40

No files matched your search

@@ -0,0 +1,60 @@
package io.floci.aws.services.stepfunctions;
import com.dashjoin.jsonata.Jsonata;
import com.dashjoin.jsonata.Parser;
import com.dashjoin.jsonata.Utils;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static io.floci.aws.services.stepfunctions.JsonataSymbolReader.read;
/** Keeps indexed constructed-array paths consistent with JSONata 2.0.6's missing-value semantics. */
final class JsonataConstructedArrayPaths {
private JsonataConstructedArrayPaths() {
}
static void install(Jsonata.Frame frame) {
Set<Parser.Symbol> firstSteps = Collections.newSetFromMap(new IdentityHashMap<>());
Jsonata.EntryCallback entry = (Jsonata.EntryCallback) frame.lookup("__evaluate_entry");
Jsonata.ExitCallback exit = (Jsonata.ExitCallback) frame.lookup("__evaluate_exit");
frame.setEvaluateEntryCallback((expression, input, environment) -> {
entry.callback(expression, input, environment);
Parser.Symbol first = firstConstructedStep(expression);
if (first != null) {
firstSteps.add(first);
}
});
frame.setEvaluateExitCallback((expression, input, environment, result) -> {
exit.callback(expression, input, environment, result);
if (firstSteps.contains(expression) && result instanceof Utils.JList<?> sequence
&& sequence.sequence && !sequence.keepSingleton && sequence.size() == 1
&& hasNoLength(sequence.getFirst())) {
// The reference path iterates the reduced object's absent length and returns
// nothing. Dashjoin instead casts that object to List. Empty only this first
// step's sequence before reduction, preserving grouped and forced-array paths.
sequence.clear();
}
});
}
private static boolean hasNoLength(Object value) {
if (value instanceof Map<?, ?> object) {
return !object.containsKey("length");
}
return value instanceof Number || value instanceof Boolean;
}
private static Parser.Symbol firstConstructedStep(Parser.Symbol expression) {
if ("path".equals(read(expression, "type")) && read(expression, "steps") instanceof List<?> steps
&& steps.size() > 1 && steps.getFirst() instanceof Parser.Symbol first
&& first.consarray && !(boolean) read(first, "keepArray")) {
return first;
}
return null;
}
}
@@ -218,6 +218,7 @@ public class JsonataEvaluator {
frame.setRuntimeBounds(evaluationTimeoutMillis, maxEvaluationDepth);
nesting.trackOn(frame);
boundHeldMemory(frame);
JsonataConstructedArrayPaths.install(frame);
stepFunctionsExtensions.forEach(frame::bind);
// AWS disables $eval, and the library ships it. An unbound name called as a function is
// the library's own T1006, which is the code AWS answers. $parse is the replacement.
@@ -0,0 +1,50 @@
package io.floci.aws.services.stepfunctions;
import com.dashjoin.jsonata.Parser;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
* Reads the nonpublic JSONata parse-tree fields shared by validation and evaluation.
* Quarkus loads application classes and dependency jars with different class loaders, so sharing
* the parser's package would raise IllegalAccessError. Resolve fields once so a dependency upgrade
* that changes the tree fails loudly, and register their native-image reflection in one place.
*/
@RegisterForReflection(targets = Parser.Symbol.class, fields = true)
final class JsonataSymbolReader {
private static final Map<String, Field> TREE_FIELDS = resolveTreeFields(
"type", "value", "steps", "lhs", "rhs", "expression", "expressions",
"procedure", "arguments", "body", "condition", "then", "_else", "keepArray");
private JsonataSymbolReader() {
}
static Object read(Parser.Symbol node, String field) {
try {
return Objects.requireNonNull(TREE_FIELDS.get(field), field).get(node);
} catch (IllegalAccessException e) {
throw new IllegalStateException(
"com.dashjoin:jsonata no longer allows reading Parser.Symbol." + field, e);
}
}
private static Map<String, Field> resolveTreeFields(String... names) {
Map<String, Field> fields = new LinkedHashMap<>();
for (String name : names) {
try {
Field field = Parser.Symbol.class.getDeclaredField(name);
field.setAccessible(true);
fields.put(name, field);
} catch (NoSuchFieldException e) {
throw new IllegalStateException("com.dashjoin:jsonata no longer declares "
+ "Parser.Symbol." + name + ", so its parse-tree readers must be updated", e);
}
}
return Map.copyOf(fields);
}
}
@@ -1,17 +1,14 @@
package io.floci.aws.services.stepfunctions;
import com.dashjoin.jsonata.Parser;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import static io.floci.aws.services.stepfunctions.JsonataSymbolReader.read;
/**
* Parses a Step Functions JSONata expression once and reports either the message the parser refused
* it with, or every reference it makes against the top-level context.
@@ -29,14 +26,8 @@ import java.util.Set;
* {@code $map($states.input.a, function($x){ b })} does name {@code b} at the top level. Both
* halves are measured against AWS in {@code JsonataTopLevelReferencesTest}.
*
* <p>{@link Parser#parse(String)} is public and returns a public {@link Parser.Symbol}, but every
* field carrying the tree is package-private, and Quarkus loads application classes and dependency
* jars with different class loaders, so sharing the package raises {@code IllegalAccessError}
* instead. The fields are therefore read reflectively, and the cost is paid up front: all of them
* are resolved once in the static initializer, so a {@code com.dashjoin:jsonata} upgrade that
* renames or removes one fails loudly on the first call instead of quietly naming no reference.
* <p>Nonpublic parse-tree fields are read through {@link JsonataSymbolReader}.
*/
@RegisterForReflection(targets = Parser.Symbol.class, fields = true)
final class JsonataTopLevelReferences {
/** The name AWS prints for {@code $}, the context item itself. */
@@ -53,10 +44,6 @@ final class JsonataTopLevelReferences {
*/
static final String STATES_ERROR_OUTPUT = "$states.errorOutput";
private static final Map<String, Field> TREE_FIELDS = resolveTreeFields(
"type", "value", "steps", "lhs", "rhs", "expression", "expressions",
"procedure", "arguments", "body", "condition", "then", "_else");
private JsonataTopLevelReferences() {
}
@@ -207,28 +194,4 @@ final class JsonataTopLevelReferences {
return nodes == null || nodes.isEmpty() ? null : nodes.get(0);
}
private static Object read(Parser.Symbol node, String field) {
try {
return Objects.requireNonNull(TREE_FIELDS.get(field), field).get(node);
} catch (IllegalAccessException e) {
throw new IllegalStateException(
"com.dashjoin:jsonata no longer allows reading Parser.Symbol." + field, e);
}
}
private static Map<String, Field> resolveTreeFields(String... names) {
Map<String, Field> fields = new LinkedHashMap<>();
for (String name : names) {
try {
Field field = Parser.Symbol.class.getDeclaredField(name);
field.setAccessible(true);
fields.put(name, field);
} catch (NoSuchFieldException e) {
throw new IllegalStateException("com.dashjoin:jsonata no longer declares "
+ "Parser.Symbol." + name + ", so the top-level reference check has to be "
+ "rewritten against the new parse tree", e);
}
}
return Map.copyOf(fields);
}
}