Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Body helper enhancement - Stringify #22

Merged
merged 4 commits into from
Mar 22, 2021
Merged

Body helper enhancement - Stringify #22

merged 4 commits into from
Mar 22, 2021

Conversation

mauriceackel
Copy link
Contributor

Adds a new parameter to the body helper.

If it is set to true and the value is a primitive, the helper returns the stringified version of the value.
This fixes an issue where string values where not correctly escaped when being returned by a body helper. The reason for going with an extra parameter here is to 1. ensure backwards compatibility and 2. still enable user to use the unescaped values as the input to other helpers like concat.

Parent issue

Closes mockoon/mockoon#397
(needs to be closed manually)

Technical implementation details
There is a third parameter for the body helper now. It is false by default and you can set the value by invoking it via {{ body 'foo.bar' undefined true }}. If the parameter is set to true, the value referenced by the path is processed by JSON.stringifybefore it is returned. If parameter is false, primitive values (i.e. null, integer, string, boolean) will not be processed by JSON.stringify. However, objects and arrays will always be stringified, no matter which value the user passes for the parameter.

…e value is a primitive, the helper returns the stringified version of the value
@mauriceackel mauriceackel requested a review from 255kb as a code owner March 5, 2021 16:17
@mauriceackel
Copy link
Contributor Author

@255kb One thing that might need to be discussed is whether or not the default value should also be stringified if the parameter is set to true.

@255kb
Copy link
Member

255kb commented Mar 9, 2021

Concerning the default value, I would say yes it should be stringified too if the option is at true, what do you think?
Also I think there is a case you didn't account for. If stringify is not provided, it will be the last param containing the Handlebars object, which is then a truthy value. You may want to test for the typeof stringify at the top of the function.

@mauriceackel
Copy link
Contributor Author

@255kb Good catch! I fixed the issue and added a test case. I also incorporated the change that the default value gets stringified.

@255kb
Copy link
Member

255kb commented Mar 22, 2021

Thanks for the changes @mauriceackel, it looks great!

One heads up concerning this repo: I am currently working on some big architecture refactoring linked to how Electron expect things to work. The result is that I splitted mockoon/commons in two, creating a new mockoon/commons-server with the helpers code. So I will move everything there in the coming days.

@255kb 255kb merged commit 3281d89 into mockoon:main Mar 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants