You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this parameter string [where][and][0][valid]=true&filter[where][and][1][or][0][name]=test&filter[where][and][1][or][1][deleted]=false
gets parsed as:
{"and":[{"valid":true},{"or":[{"[name]":"test"},{"[deleted]":false}]}]} note the square brackets around name and deleted
We found this behavior in Loopback, which uses this module for parsing parameters. @raymondfeng suggested to open here an issue, and kindly provided a snippet to demonstrate the problem:
varassert=require('assert');varqs=require('qs');varjson={filter: {where: {"and":[{"valid":true},{"or":[{"name":"test"},{"deleted":false}]}]}}};varx=qs.stringify(json,{encode: false});vary=qs.parse(x);console.log('%j %j',json,y);assert.deepEqual(json,y);// will throw
qs.parse has a default depth option of 5 (documented in the readme) - if you do var y = qs.parse(x, { depth: 10 });, for example, you'll get the following:
which only leaves the booleans on the front end of the pipeline ending up as strings on the back end.
Parsing booleans is #91 (comment) - since everything that comes in via a querystring is a string, there's no safe way to automatically parse the strings "true" and "false" into booleans without breaking places with the actual string true or false. However, you can pass an explicit decoder option to qs.parse that handles this.
Note the following, with changes:
varassert=require('assert');varqs=require('qs');varjson={filter: {where: {"and":[{"valid":true},{"or":[{"name":"test"},{"deleted":false}]}]}}};varx=qs.stringify(json,{encode: false});vary=qs.parse(x,{depth: 10,decoder: function(x){if(x==='true'){returntrue;}elseif(x==='false'){returnfalse;}returnx;}});console.log('%j %j',json,y);assert.deepEqual(json,y);// will NOT throw
this parameter string
[where][and][0][valid]=true&filter[where][and][1][or][0][name]=test&filter[where][and][1][or][1][deleted]=false
gets parsed as:
{"and":[{"valid":true},{"or":[{"[name]":"test"},{"[deleted]":false}]}]}
note the square brackets around name and deleted
We found this behavior in Loopback, which uses this module for parsing parameters. @raymondfeng suggested to open here an issue, and kindly provided a snippet to demonstrate the problem:
Here you can find the original issue,
The text was updated successfully, but these errors were encountered: