Skip to content

Commit 1bf9f7a

Browse files
committed
[Fix] parse: properly account for strictNullHandling when allowEmptyArrays
Fixes #510
1 parent 7ebf48b commit 1bf9f7a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/parse.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ var parseObject = function (chain, val, options, valuesParsed) {
126126
var root = chain[i];
127127

128128
if (root === '[]' && options.parseArrays) {
129-
obj = options.allowEmptyArrays && leaf === '' ? [] : [].concat(leaf);
129+
obj = options.allowEmptyArrays && (leaf === '' || (options.strictNullHandling && leaf === null))
130+
? []
131+
: [].concat(leaf);
130132
} else {
131133
obj = options.plainObjects ? Object.create(null) : {};
132134
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;

test/parse.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ test('parse()', function (t) {
183183
st.end();
184184
});
185185

186+
t.test('allowEmptyArrays + strictNullHandling', function (st) {
187+
st.deepEqual(
188+
qs.parse('testEmptyArray[]', { strictNullHandling: true, allowEmptyArrays: true }),
189+
{ testEmptyArray: [] }
190+
);
191+
192+
st.end();
193+
});
194+
186195
t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
187196
t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
188197
t.deepEqual(

test/stringify.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ test('stringify()', function (t) {
315315
st.end();
316316
});
317317

318+
t.test('allowEmptyArrays + strictNullHandling', function (st) {
319+
st.equal(
320+
qs.stringify(
321+
{ testEmptyArray: [] },
322+
{ strictNullHandling: true, allowEmptyArrays: true }
323+
),
324+
'testEmptyArray[]'
325+
);
326+
327+
st.end();
328+
});
329+
318330
t.test('stringifies an array value with one item vs multiple items', function (st) {
319331
st.test('non-array item', function (s2t) {
320332
s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=c');

0 commit comments

Comments
 (0)