Skip to content

Commit ec67fea

Browse files
committed
Fix encoded separator incorrectly splitting single values into arrays
Fixes #336
1 parent 2e1f45a commit ec67fea

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

base.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ function parserForArrayFormat(options) {
202202
case 'separator': {
203203
return (key, value, accumulator) => {
204204
const isArray = typeof value === 'string' && value.includes(options.arrayFormatSeparator);
205-
const isEncodedArray = (typeof value === 'string' && !isArray && decode(value, options).includes(options.arrayFormatSeparator));
206-
value = isEncodedArray ? decode(value, options) : value;
207-
const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : (value === null ? value : decode(value, options));
205+
const newValue = isArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : (value === null ? value : decode(value, options));
208206
accumulator[key] = newValue;
209207
};
210208
}

test/parse.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ test('query strings having pipe separated arrays and format option as `separator
162162
}), {foo: ['bar', 'baz']});
163163
});
164164

165+
test('single value with encoded separator should not be split into array', t => {
166+
// Test for issue #336 - encoded separators should not cause array splitting
167+
const value = encodeURIComponent('a|b'); // 'a%7Cb'
168+
t.deepEqual(queryString.parse(`foo=${value}`, {
169+
arrayFormat: 'separator',
170+
arrayFormatSeparator: '|',
171+
}), {foo: 'a|b'});
172+
173+
// Multiple values with encoded separators in them
174+
const value1 = encodeURIComponent('a|b');
175+
const value2 = encodeURIComponent('c|d');
176+
t.deepEqual(queryString.parse(`foo=${value1}|${value2}`, {
177+
arrayFormat: 'separator',
178+
arrayFormatSeparator: '|',
179+
}), {foo: ['a|b', 'c|d']});
180+
});
181+
165182
test('query strings having brackets arrays with null and format option as `bracket`', t => {
166183
t.deepEqual(queryString.parse('bar[]&foo[]=a&foo[]&foo[]=', {
167184
arrayFormat: 'bracket',
@@ -407,7 +424,7 @@ test('value should not be decoded twice with `arrayFormat` option set as `separa
407424
test('value separated by encoded comma will not be parsed as array with `arrayFormat` option set to `comma`', t => {
408425
const value = '1,2,3';
409426
t.deepEqual(queryString.parse(`id=${encodeURIComponent(value)}`, {arrayFormat: 'comma', parseNumbers: true}), {
410-
id: [1, 2, 3],
427+
id: '1,2,3',
411428
});
412429
});
413430

0 commit comments

Comments
 (0)