Skip to content

Commit

Permalink
fix(stepfunctions): nested arrays are not serialized correctly (aws#2…
Browse files Browse the repository at this point in the history
…6055)

`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 aws#26045.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
lpizzinidev authored and lukey-aleios committed Jun 30, 2023
1 parent af98a79 commit 7268c8f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
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

0 comments on commit 7268c8f

Please sign in to comment.