Skip to content

Commit f9d4573

Browse files
authored
fix(stepfunctions): nested arrays are not serialized correctly (#26055)
`FieldUtils.renderObject` was not serializing nested arrays correctly. For example: ``` {"myNestedArray":[[[123,123],[456,456]]]} ``` Was serialized to: ``` {"myNestedArray":[{"0":[123,123],"1":[456,456]}]} ``` This fix should solve the problem. Closes #26045. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c51e8c7 commit f9d4573

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

packages/aws-cdk-lib/aws-stepfunctions/lib/private/json-path.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,25 @@ function recurseArray(key: string, arr: any[], handlers: FieldHandlers, visited:
183183
}
184184

185185
return {
186-
[key]: arr.map(value => {
187-
if ((typeof value === 'string' && jsonPathString(value) !== undefined)
186+
[key]: resolveArray(arr, handlers, visited),
187+
};
188+
}
189+
190+
function resolveArray(arr: any[], handlers: FieldHandlers, visited: object[] = []): any[] {
191+
return arr.map(value => {
192+
if ((typeof value === 'string' && jsonPathString(value) !== undefined)
188193
|| (typeof value === 'number' && jsonPathNumber(value) !== undefined)
189194
|| (isStringArray(value) && jsonPathStringList(value) !== undefined)) {
190-
throw new Error('Cannot use JsonPath fields in an array, they must be used in objects');
191-
}
192-
if (typeof value === 'object' && value !== null) {
193-
return recurseObject(value, handlers, visited);
194-
}
195-
return value;
196-
}),
197-
};
195+
throw new Error('Cannot use JsonPath fields in an array, they must be used in objects');
196+
}
197+
if (Array.isArray(value)) {
198+
return resolveArray(value, handlers, visited);
199+
}
200+
if (typeof value === 'object' && value !== null) {
201+
return recurseObject(value, handlers, visited);
202+
}
203+
return value;
204+
});
198205
}
199206

200207
function isStringArray(x: any): x is string[] {

packages/aws-cdk-lib/aws-stepfunctions/test/fields.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,48 @@ describe('intrinsics constructors', () => {
470470
'Field.$': 'States.JsonToString($.Obj)',
471471
});
472472
});
473+
474+
test('correctly serialize a nested array', () => {
475+
expect(
476+
FieldUtils.renderObject({
477+
nestedArray: [
478+
[
479+
[123, 123],
480+
[456, 456],
481+
],
482+
],
483+
}),
484+
).toStrictEqual({
485+
nestedArray: [
486+
[
487+
[123, 123],
488+
[456, 456],
489+
],
490+
],
491+
});
492+
});
493+
494+
test('deep replace correctly handles fields in nested arrays', () => {
495+
expect(
496+
FieldUtils.renderObject({
497+
deep: [
498+
[
499+
{
500+
deepField: JsonPath.numberAt('$.numField'),
501+
},
502+
],
503+
],
504+
}),
505+
).toStrictEqual({
506+
deep: [
507+
[
508+
{
509+
'deepField.$': '$.numField',
510+
},
511+
],
512+
],
513+
});
514+
});
473515
});
474516

475517
test('find task token even if nested in intrinsic functions', () => {

0 commit comments

Comments
 (0)