Skip to content

Commit

Permalink
[Fix] stringify: avoid exception on repeated object values
Browse files Browse the repository at this point in the history
Fixes #402
  • Loading branch information
ljharb committed Mar 22, 2021
1 parent 7c1fcc5 commit e77ca2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/stringify.js
Expand Up @@ -137,6 +137,7 @@ var stringify = function stringify(
: prefix + (allowDots ? '.' + key : '[' + key + ']');

sideChannel.set(object, true);
var valueSideChannel = getSideChannel();
pushToArray(values, stringify(
value,
keyPrefix,
Expand All @@ -152,7 +153,7 @@ var stringify = function stringify(
formatter,
encodeValuesOnly,
charset,
sideChannel
valueSideChannel
));
}

Expand Down
22 changes: 22 additions & 0 deletions test/stringify.js
Expand Up @@ -465,6 +465,28 @@ test('stringify()', function (t) {
st.end();
});

t.test('non-circular duplicated references can still work', function (st) {
var hourOfDay = {
'function': 'hour_of_day'
};

var p1 = {
'function': 'gte',
arguments: [hourOfDay, 0]
};
var p2 = {
'function': 'lte',
arguments: [hourOfDay, 23]
};

st.equal(
qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true }),
'filters[$and][0][function]=gte&filters[$and][0][arguments][0][function]=hour_of_day&filters[$and][0][arguments][1]=0&filters[$and][1][function]=lte&filters[$and][1][arguments][0][function]=hour_of_day&filters[$and][1][arguments][1]=23'
);

st.end();
});

t.test('selects properties when filter=array', function (st) {
st.equal(qs.stringify({ a: 'b' }, { filter: ['a'] }), 'a=b');
st.equal(qs.stringify({ a: 1 }, { filter: [] }), '');
Expand Down

0 comments on commit e77ca2c

Please sign in to comment.