Skip to content

Commit 6135284

Browse files
committed
[Fix] parse: when a custom decoder returns null for a key, ignore that key
1 parent 32dcc63 commit 6135284

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

lib/parse.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,18 @@ var parseValues = function parseQueryStringValues(str, options) {
108108
} else {
109109
key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key');
110110

111-
val = utils.maybeMap(
112-
parseArrayValue(
113-
part.slice(pos + 1),
114-
options,
115-
isArray(obj[key]) ? obj[key].length : 0
116-
),
117-
function (encodedVal) {
118-
return options.decoder(encodedVal, defaults.decoder, charset, 'value');
119-
}
120-
);
111+
if (key !== null) {
112+
val = utils.maybeMap(
113+
parseArrayValue(
114+
part.slice(pos + 1),
115+
options,
116+
isArray(obj[key]) ? obj[key].length : 0
117+
),
118+
function (encodedVal) {
119+
return options.decoder(encodedVal, defaults.decoder, charset, 'value');
120+
}
121+
);
122+
}
121123
}
122124

123125
if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
@@ -128,11 +130,13 @@ var parseValues = function parseQueryStringValues(str, options) {
128130
val = isArray(val) ? [val] : val;
129131
}
130132

131-
var existing = has.call(obj, key);
132-
if (existing && options.duplicates === 'combine') {
133-
obj[key] = utils.combine(obj[key], val);
134-
} else if (!existing || options.duplicates === 'last') {
135-
obj[key] = val;
133+
if (key !== null) {
134+
var existing = has.call(obj, key);
135+
if (existing && options.duplicates === 'combine') {
136+
obj[key] = utils.combine(obj[key], val);
137+
} else if (!existing || options.duplicates === 'last') {
138+
obj[key] = val;
139+
}
136140
}
137141
}
138142

test/parse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,20 @@ test('parse()', function (t) {
996996
st.end();
997997
});
998998

999+
t.test('handles a custom decoder returning `null`, with a string key of `null`', function (st) {
1000+
st.deepEqual(
1001+
qs.parse('null=1&ToNull=2', {
1002+
decoder: function (str, defaultDecoder, charset) {
1003+
return str === 'ToNull' ? null : defaultDecoder(str, defaultDecoder, charset);
1004+
}
1005+
}),
1006+
{ 'null': '1' },
1007+
'"null" key is not overridden by `null` decoder result'
1008+
);
1009+
1010+
st.end();
1011+
});
1012+
9991013
t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
10001014
st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '☺' });
10011015
st.end();

0 commit comments

Comments
 (0)