Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stepfunctions): nested arrays are not serialized correctly #26055

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/private/json-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,25 @@ function recurseArray(key: string, arr: any[], handlers: FieldHandlers, visited:
}

return {
[key]: arr.map(value => {
if ((typeof value === 'string' && jsonPathString(value) !== undefined)
[key]: resolveArray(arr, handlers, visited),
};
}

function resolveArray(arr: any[], handlers: FieldHandlers, visited: object[] = []): any[] {
return arr.map(value => {
if ((typeof value === 'string' && jsonPathString(value) !== undefined)
|| (typeof value === 'number' && jsonPathNumber(value) !== undefined)
|| (isStringArray(value) && jsonPathStringList(value) !== undefined)) {
throw new Error('Cannot use JsonPath fields in an array, they must be used in objects');
}
if (typeof value === 'object' && value !== null) {
return recurseObject(value, handlers, visited);
}
return value;
}),
};
throw new Error('Cannot use JsonPath fields in an array, they must be used in objects');
}
if (Array.isArray(value)) {
return resolveArray(value, handlers, visited);
}
if (typeof value === 'object' && value !== null) {
return recurseObject(value, handlers, visited);
}
return value;
});
}

function isStringArray(x: any): x is string[] {
Expand Down
42 changes: 42 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/test/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,48 @@ describe('intrinsics constructors', () => {
'Field.$': 'States.JsonToString($.Obj)',
});
});

test('correctly serialize a nested array', () => {
expect(
FieldUtils.renderObject({
nestedArray: [
[
[123, 123],
[456, 456],
],
],
}),
).toStrictEqual({
nestedArray: [
[
[123, 123],
[456, 456],
],
],
});
});

test('deep replace correctly handles fields in nested arrays', () => {
expect(
FieldUtils.renderObject({
deep: [
[
{
deepField: JsonPath.numberAt('$.numField'),
},
],
],
}),
).toStrictEqual({
deep: [
[
{
'deepField.$': '$.numField',
},
],
],
});
});
});

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