Skip to content
New issue

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

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

Already on GitHub? # to your account

[aws-stepfunctions]: Nested arrays are not serialized correctly on input #14599

Closed
marciocarmona opened this issue May 8, 2021 · 2 comments
Closed
Labels
@aws-cdk/aws-stepfunctions Related to AWS StepFunctions bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/small Small work item – less than a day of effort p1

Comments

@marciocarmona
Copy link
Contributor

marciocarmona commented May 8, 2021

If part of a Step Function task contains a nested array, part of the array is serialized as an object with the indexes as their string keys.

The issue seems to be inside the TaskInput.fromObject call, more specifically in the recurseArray calls.

Reproduction Steps

        let test = {
            x: [["Hello", "world"], ["this", "is", "bugged"]]
        }

        console.log(util.inspect(renderObject(test), {showHidden: false, depth: null}))

What did you expect to happen?

This should have been returned:

{
  x: [
    ['Hello', 'world'],
    ['this', 'is', 'bugged']
  ]
}

What actually happened?

It returned this:

{
  x: [
    { '0': 'Hello', '1': 'world' },
    { '0': 'this', '1': 'is', '2': 'bugged' }
  ]
}

Environment

  • CDK CLI Version : 1.98.0 (build 79f4512)
  • Framework Version: ?
  • Node.js Version: v12.22.1
  • OS : Linux Red Hat 7.2.1-2
  • Language (Version): TypeScript (4.0.5)

Other

After doing some research I noticed that for deeply nested arrays, every other array ends up mapped to an object. e.g.

        let test = {
            x: [[[[[[[[[[["Hi!"]]]]]]]]]]]
        }

        console.log(util.inspect(renderObject(test), {showHidden: false, depth: null}))

Output:

{
  x: [
    {
      '0': [
        {
          '0': [
            {
              '0': [
                {
                  '0': [ { '0': [ 'Hi!' ] } ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

The problem seems to come from this recurseArray method:

function recurseArray(key: string, arr: any[], handlers: FieldHandlers, visited: object[] = []): {[key: string]: any[] | string} {
if (isStringArray(arr)) {
const path = jsonPathStringList(arr);
if (path !== undefined) {
return handlers.handleList(key, arr);
}
// Fall through to correctly reject encoded strings inside an array.
// They cannot be represented because there is no key to append a '.$' to.
}
return {
[key]: 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;
}),
};
}

Notice that it checks if value is an object before checking if it's an array, since arrays are actually considered objects by Javascript, it ends up calling the recurseObject method.

Recommendation:

  • Check if value is an array first and call the recurseArray method instead.

This is 🐛 Bug Report

@marciocarmona marciocarmona added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 8, 2021
@github-actions github-actions bot added the @aws-cdk/aws-stepfunctions Related to AWS StepFunctions label May 8, 2021
marciocarmona pushed a commit to marciocarmona/aws-cdk that referenced this issue May 10, 2021
marciocarmona pushed a commit to marciocarmona/aws-cdk that referenced this issue May 10, 2021
@ericzbeard ericzbeard assigned BenChaimberg and unassigned shivlaks Jun 18, 2021
@BenChaimberg BenChaimberg added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Jun 21, 2021
@BenChaimberg BenChaimberg removed their assignment Jul 12, 2021
@github-actions
Copy link

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Jul 12, 2022
@markmansur
Copy link
Contributor

This is still an issue fyi

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
@aws-cdk/aws-stepfunctions Related to AWS StepFunctions bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants