diff --git a/lib/parse.js b/lib/parse.js index 8eedf63d..0c6a6668 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -126,7 +126,9 @@ var parseObject = function (chain, val, options, valuesParsed) { var root = chain[i]; if (root === '[]' && options.parseArrays) { - obj = options.allowEmptyArrays && leaf === '' ? [] : [].concat(leaf); + obj = options.allowEmptyArrays && (leaf === '' || (options.strictNullHandling && leaf === null)) + ? [] + : [].concat(leaf); } else { obj = options.plainObjects ? Object.create(null) : {}; var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root; diff --git a/test/parse.js b/test/parse.js index 8724b2cc..2bf4c411 100644 --- a/test/parse.js +++ b/test/parse.js @@ -183,6 +183,15 @@ test('parse()', function (t) { st.end(); }); + t.test('allowEmptyArrays + strictNullHandling', function (st) { + st.deepEqual( + qs.parse('testEmptyArray[]', { strictNullHandling: true, allowEmptyArrays: true }), + { testEmptyArray: [] } + ); + + st.end(); + }); + t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string'); t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string'); t.deepEqual( diff --git a/test/stringify.js b/test/stringify.js index 22fcfda2..12b1c71d 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -315,6 +315,18 @@ test('stringify()', function (t) { st.end(); }); + t.test('allowEmptyArrays + strictNullHandling', function (st) { + st.equal( + qs.stringify( + { testEmptyArray: [] }, + { strictNullHandling: true, allowEmptyArrays: true } + ), + 'testEmptyArray[]' + ); + + st.end(); + }); + t.test('stringifies an array value with one item vs multiple items', function (st) { st.test('non-array item', function (s2t) { s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=c');