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 interpolation with Objects and Arrays #1923

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Its-treason
Copy link
Member

@Its-treason Its-treason commented Mar 25, 2024

Description

Fixes: #1910 & #1635

Update how variable interpolation works, so the behavior with objects and arrays is more as expected.

  • Interpolation of objects and arrays now works
  • Try to use the default toString-Method if available
    • Except for the default Object and arrays

Test collection: https://cdn.discordapp.com/attachments/693228572286124085/1221951301404065873/json-testing_2.zip?ex=661471d5&is=6601fcd5&hm=277420a68a493567de437403f133622d186642f689e761d0438306b7579497ad&

Before:

image

After:

image

Pre-Build binary with the fix: https://github.com/Its-treason/bruno/releases/tag/nightly

Interpolate will now fallback to the original object to collect
variable values. Objets will automaticly json encoded. And if
intererpolate is executed for the json body all variables will
be json encoded so strings get double quoted.

This should fix:
- usebruno#1910
- usebruno#1635
Now only stringify objects
Now the invalid JSON will be written into the body instead of the
pre interpolated body.
For example with Moment the object with JSON.stringify became:
`"Mon Mar 25 2024 22:52:03 GMT+0100"` when using just `toString`
the moment object becomes `Mon Mar 25 2024 22:52:03 GMT+0100`
(without double quotes). I think the should yield more expected
results.
@mjhcorporate
Copy link
Contributor

Hmm... I'm a bit confused that for string interpolation, you have to put double-quotes into the string yourself ("{{my_string}}"), but for arrays and objects, you don't have to put [ ] and { } around them.

Of course, fixing this by removing the need to put " around the string would break backwards-compatibility in a really bad way, and it would break otherwise needed functionality where you just want to plop a raw string into some body. (I sometimes need to send invalid JSON as part of my tests).

The more I think about this: Trying to solve serialization for the user is really tricky. Whatever we do, it should be as simple as possible, so that the user can have a good mental-model of what is going on.

@Its-treason
Copy link
Member Author

Its-treason commented Mar 27, 2024

Hmm... I'm a bit confused that for string interpolation, you have to put double-quotes into the string yourself ("{{my_string}}"), but for arrays and objects, you don't have to put [ ] and { } around them.

Of course, fixing this by removing the need to put " around the string would break backwards-compatibility in a really bad way, and it would break otherwise needed functionality where you just want to plop a raw string into some body. (I sometimes need to send invalid JSON as part of my tests).

The more I think about this: Trying to solve serialization for the user is really tricky. Whatever we do, it should be as simple as possible, so that the user can have a good mental-model of what is going on.

Yeah, I also thought about this and the first implementation was like this (Nothing must be quoted). But then I saw dw-0 and other wrapping strings with double quotes. In the end it's probably Anoops decision on how this should look like.

function serializeObject(obj: Object) {
// Check if the object has a `toString` method like `Moment`
// Don't do it with arrays they serialize weirdly
if (typeof obj.toString === 'function' && !Array.isArray(obj)) {
Copy link
Contributor

@mjhcorporate mjhcorporate Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can lead to really misleading behavior. Consider the case where an object has a custom toString method: It would be called, but only, if the object is at the top level. If it is nested, then the default JSON.stringify() method is used, which leads to a different result.

Example:

// object on top-level
const today = moment();
console.log(today.toString());
// -> Wed Mar 27 2024 09:43:06 GMT+0100

// object nested:
const nestedExample = {"nestedExample": today}
console.log(JSON.stringify(nestedExample))
// -> {"nestedExample":"2024-03-27T08:43:06.249Z"}

See https://jsfiddle.net/17Lzbjwm/1/

Proposal for simpler logic for {{thing}} replacement:

  • if thing is a string, do a literal replacement
  • if thing is anything else, use JSON.stringify(thing)
    • in case the user does not like the result, they have to do it themselves in prerequest or postrequest scripts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the whole point of calling toString was, because JSON.stringify would add double quotes around strings. I'll look into it later the day.

@dw-0
Copy link
Contributor

dw-0 commented Sep 21, 2024

Any update here? I think the issue is still present?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Var used in POST's request body (JSON body) not being identified and evaluated correctly
5 participants