From 98126ef02b76dbdd1905d210c71d39a049809327 Mon Sep 17 00:00:00 2001 From: Chris Dyson Date: Mon, 17 Sep 2018 10:45:31 +1200 Subject: [PATCH] [Fix] `stringify`: fix a crash with `strictNullHandling` and a custom `filter`/`serializeDate` --- lib/stringify.js | 4 +++- test/stringify.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/stringify.js b/lib/stringify.js index 4015b785..6c8c4e88 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -55,7 +55,9 @@ var stringify = function stringify( // eslint-disable-line func-name-matching obj = filter(prefix, obj); } else if (obj instanceof Date) { obj = serializeDate(obj); - } else if (obj === null) { + } + + if (obj === null) { if (strictNullHandling) { return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset) : prefix; } diff --git a/test/stringify.js b/test/stringify.js index c54c0df3..7901ea00 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -625,5 +625,25 @@ test('stringify()', function (t) { st.end(); }); + t.test('strictNullHandling works with custom filter', function (st) { + var filter = function (prefix, value) { + return value; + }; + + var options = { strictNullHandling: true, filter: filter }; + st.equal(qs.stringify({ key: null }, options), 'key'); + st.end(); + }); + + t.test('strictNullHandling works with null serializeDate', function (st) { + var serializeDate = function () { + return null; + }; + var options = { strictNullHandling: true, serializeDate: serializeDate }; + var date = new Date(); + st.equal(qs.stringify({ key: date }, options), 'key'); + st.end(); + }); + t.end(); });