-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: main
Are you sure you want to change the base?
Fix interpolation with Objects and Arrays #1923
Conversation
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.
Hmm... I'm a bit confused that for string interpolation, you have to put double-quotes into the string yourself ( Of course, fixing this by removing the need to put 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)) { |
There was a problem hiding this comment.
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, useJSON.stringify(thing)
- in case the user does not like the result, they have to do it themselves in prerequest or postrequest scripts.
There was a problem hiding this comment.
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.
Any update here? I think the issue is still present? |
Description
Fixes: #1910 & #1635
Update how variable interpolation works, so the behavior with objects and arrays is more as expected.
toString
-Method if availableTest collection: https://cdn.discordapp.com/attachments/693228572286124085/1221951301404065873/json-testing_2.zip?ex=661471d5&is=6601fcd5&hm=277420a68a493567de437403f133622d186642f689e761d0438306b7579497ad&
Before:
After:
Pre-Build binary with the fix: https://github.com/Its-treason/bruno/releases/tag/nightly