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

JSON bug #2426

Closed
jwillmer opened this issue Oct 29, 2021 · 8 comments
Closed

JSON bug #2426

jwillmer opened this issue Oct 29, 2021 · 8 comments
Assignees
Labels
activity This issue relates to a workflows activity bug Something isn't working

Comments

@jwillmer
Copy link
Contributor

Problem:

  • Converting input to a JS object in JS activity and returning it
  • Using the output of the JS activity in a HTTP activity to send it as JSON
  • Sent request does not contain the right JSON.

image

JS activity called process

const raw = activities.webhook.Output().RawBody;
const body = JSON.parse(raw);
const items = body.Event.Control.Items;
let output = [];

for (const item of items) {
    for (const value of item.Values) {
        output.push({
            Item: item.Id,
            Values: {
                ListItem: value.Title,
                Value: value.Value
            }
        });
    }
}

return output;

Transfered data between the steps:

[
 {
  "Item": "e991f17a-246b-4d7d-a4b6-abbf54bd6911",
  "Values": {
   "ListItem": "Name",
   "Value": "Test"
  }
 },
 {
  "Item": "e991f17a-246b-4d7d-a4b6-abbf54bd6911",
  "Values": {
   "ListItem": "E-mail",
   "Value": "test@test.com"
  }
 }
}

POST content (generation) of the HTTP activity

return JSON.stringify(activities.process.Output());

Received data at my endpoint

[
  {
    "Item": "e991f17a-246b-4d7d-a4b6-abbf54bd6911",
    "Values": {
      "Count": 2,
      "IsReadOnly": true
    }
  },
  {
    "Item": "e991f17a-246b-4d7d-a4b6-abbf54bd6911",
    "Values": {
      "Count": 2,
      "IsReadOnly": true
    }
  }
]

Note: Stringifying the JS object in the JS activity and then just using it in the HTTP activity (return activities.process.Output();) returns the correct JSON.

@jwillmer jwillmer added bug Something isn't working activity This issue relates to a workflows activity labels Oct 29, 2021
@jwillmer jwillmer added this to Needs triage in Triage via automation Oct 29, 2021
@jwillmer jwillmer added this to Backlog in RC Backlog via automation Oct 29, 2021
@sfmskywalker
Copy link
Member

I can reproduce with the following (simplified) workflow:

image

JSON version of the workflow for testing:
js-serialization-workflow.zip

The issue lies with the serialization of the JavaScript object returned from the RunJavaScript activity.
Looking into that next.

@sfmskywalker
Copy link
Member

The problem is not the output of the first activity (which is an array of ExpandoObjects), but with json.Stringify being unable to properly stringify the list of expandos to JSON.

@sfmskywalker
Copy link
Member

Interestingly enough, Newtonsoft Json can properly serialize the expandos to JSON. Sample result:

[
{
   "Item": "e991f17a-246b-4d7d-a4b6-abbf54bd6911",
   "Values": {
      "ListItem": "Name",
      "Value": "Test"
    }
  },
  {
    "Item": "e991f17a-246b-4d7d-a4b6-abbf54bd6911",
    "Values": {
      "ListItem": "E-mail",
      "Value": "test@test.com"
    }
  }
]

What I suggest we do is add a custom serialization JS function that we use instead of JSON.stringify *which is provided by Jint, so maybe not a good idea to try and replace it).

Triage automation moved this from Needs triage to Closed Nov 1, 2021
RC Backlog automation moved this from Backlog to Done Nov 1, 2021
@sfmskywalker
Copy link
Member

The following JS expression can now be used:

return jsonEncode(activities.process.Output());

The result will look like this:

[{"Item":"e991f17a-246b-4d7d-a4b6-abbf54bd6911","Values":{"ListItem":"Name","Value":"Test"}},{"Item":"e991f17a-246b-4d7d-a4b6-abbf54bd6911","Values":{"ListItem":"E-mail","Value":"test@test.com"}}]

@jwillmer
Copy link
Contributor Author

jwillmer commented Nov 2, 2021

It works but with this solution you might want to interfere with JSON.stringify and automatically use your function (in the backend) when a user write this. Since it is not obvious that JSON is supported but stringify is broken.

@jwillmer jwillmer reopened this Nov 2, 2021
Triage automation moved this from Closed to Needs triage Nov 2, 2021
RC Backlog automation moved this from Done to In progress Nov 2, 2021
@sfmskywalker
Copy link
Member

sfmskywalker commented Nov 2, 2021

Yeah maybe. But, barring whether it's even possible or not, I don't know that JSON stringify is actually broken. For all I know it's expected behavior when serializing ExpandoObject. And if we replace it, we may inadvertently break some standard protocol implemented by it.

Would be good if someone could do a little investigation and look at Jint's JSON stringify implementation.

@jwillmer
Copy link
Contributor Author

jwillmer commented Nov 2, 2021

Tracking upstream: sebastienros/jint#995

@sfmskywalker
Copy link
Member

The ExpandoObject serialization issue was fixed in sebastienros/jint#996.

Triage automation moved this from Needs triage to Closed Nov 3, 2021
RC Backlog automation moved this from In progress to Done Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
activity This issue relates to a workflows activity bug Something isn't working
Projects
No open projects
Development

No branches or pull requests

3 participants