From e77ca2c471f3d581e2e029d22343fc67ccce7a14 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 21 Mar 2021 20:00:46 -0700 Subject: [PATCH] [Fix] `stringify`: avoid exception on repeated object values Fixes #402 --- lib/stringify.js | 3 ++- test/stringify.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/stringify.js b/lib/stringify.js index ab7a37e2..b8cee4bc 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -137,6 +137,7 @@ var stringify = function stringify( : prefix + (allowDots ? '.' + key : '[' + key + ']'); sideChannel.set(object, true); + var valueSideChannel = getSideChannel(); pushToArray(values, stringify( value, keyPrefix, @@ -152,7 +153,7 @@ var stringify = function stringify( formatter, encodeValuesOnly, charset, - sideChannel + valueSideChannel )); } diff --git a/test/stringify.js b/test/stringify.js index b2b3f4b5..931ac0dd 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -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: [] }), '');