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): repeated object references not allowed even if not a circular reference #14628

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 10 additions & 5 deletions packages/@aws-cdk/aws-stepfunctions/lib/json-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ interface FieldHandlers {

export function recurseObject(obj: object | undefined, handlers: FieldHandlers, visited: object[] = []): object | undefined {
if (obj === undefined) { return undefined; }
if (visited.includes(obj)) {
return {};
} else {
visited.push(obj);
}

// Avoiding infinite recursion
if (visited.includes(obj)) { return {}; }

// Marking current object as visited for the current recursion path
visited.push(obj);

const ret: any = {};
for (const [key, value] of Object.entries(obj)) {
Expand All @@ -106,6 +107,10 @@ export function recurseObject(obj: object | undefined, handlers: FieldHandlers,
}
}

// Removing from visited after leaving the current recursion path
// Allowing it to be visited again if it's not causing a recursion (circular reference)
visited.pop();

return ret;
}

Expand Down
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions/test/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,26 @@ describe('Fields', () => {
expect(FieldUtils.findReferencedPaths(paths))
.toStrictEqual(['$.listField', '$.numField', '$.stringField']);
});

test('repeated object references at different tree paths should not be considered as recursions', () => {
const repeatedObject = {
field: JsonPath.stringAt('$.stringField'),
numField: JsonPath.numberAt('$.numField'),
};
expect(FieldUtils.renderObject(
{
reference1: repeatedObject,
reference2: repeatedObject,
},
)).toStrictEqual({
reference1: {
'field.$': '$.stringField',
'numField.$': '$.numField',
},
reference2: {
'field.$': '$.stringField',
'numField.$': '$.numField',
},
});
});
});