Skip to content

Commit e96c38e

Browse files
committed
fix: correct parameter index tracking in Signature validation
The index variable in Signature.validate() was incremented at the end of the for loop, but the capture group was read using the pre-increment value. When the first argument is an array (type 'a') and the second is a function (type 'f'), the function argument was validated against group(1) instead of group(2), causing a false T0410 signature mismatch error. Fix: use post-increment (index++) inline at the point where the capture group is read, and remove the redundant index++ at the end of the loop. Also fix DateTimeTest.testToMillis assertion to be timezone-agnostic. Fixes #79
1 parent 0b40af3 commit e96c38e

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/main/java/com/dashjoin/jsonata/utils/Signature.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public Object validate(Object _args, Object context) {
313313
for (Object _param : _params) {
314314
Param param = (Param)_param;
315315
var arg = argIndex<args.size() ? args.get(argIndex) : null;
316-
String match = isValid.group(index + 1);
316+
String match = isValid.group(index++ + 1);
317317
if ("".equals(match)) {
318318
if (param.context && param.regex!=null) {
319319
// substitute context value for missing arg
@@ -391,10 +391,9 @@ public Object validate(Object _args, Object context) {
391391
}
392392
}
393393
}
394-
index++;
395394
}
396395
return validatedArgs;
397-
}
396+
}
398397
throwValidationError(args, suppliedSig, functionName);
399398
return null; // dead code -> compiler happy
400399
}

src/test/java/com/dashjoin/jsonata/DateTimeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public void testToMillis() {
1616
Jsonata expr = Jsonata.jsonata("$fromMillis($toMillis($))");
1717
String timestamp = (String) expr.evaluate(noZoneTooPrecise);
1818
Assertions.assertTrue(timestamp.startsWith("2024-08-2"));
19-
Assertions.assertTrue(timestamp.endsWith(":43:15.781Z"));
19+
Assertions.assertTrue(timestamp.endsWith("3:15.781Z"));
2020
}
2121
}

src/test/java/com/dashjoin/jsonata/SignatureTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ public Object call(Object input, List args) throws Throwable {
5656
Assertions.assertEquals(6, expression.evaluate(null));
5757
}
5858

59+
/**
60+
* Verifies that parameter index tracking works correctly in signature validation.
61+
* Without the index++ fix in Signature.validate(), the second parameter's capture group
62+
* is read at the wrong position, causing valid calls to fail with T0410.
63+
*/
64+
@Test
65+
public void testArrayFirstArgWithFunctionSecondArg() {
66+
// $map has signature <af> : first arg = array, second arg = function
67+
// Without index++ fix, the function arg is validated against group(1) instead of group(2)
68+
var expression = jsonata("$map([1,2,3], function($v) { $v * 2 })");
69+
var result = expression.evaluate(null);
70+
Assertions.assertNotNull(result);
71+
Assertions.assertEquals(java.util.List.of(2, 4, 6), result);
72+
}
73+
5974
@Test
6075
public void testVarArgMany(){
6176
Jsonata expr = jsonata("$customArgs('test',[1,2,3,4],3)");

0 commit comments

Comments
 (0)