Skip to content

Commit 2448503

Browse files
authored
Throw if single element array of empty string
1 parent 571a16a commit 2448503

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/lib/params-serializer.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ test('serializes array params with many values', (t) => {
5252
paramsSerializer({ foo: 1, bar: ['null', '2', 'undefined'] }),
5353
'bar=null&bar=2&bar=undefined&foo=1',
5454
)
55+
t.is(paramsSerializer({ foo: 1, bar: ['', '', ''] }), 'bar=&bar=&bar=&foo=1')
56+
t.is(
57+
paramsSerializer({ foo: 1, bar: ['', 'a', '2'] }),
58+
'bar=&bar=a&bar=2&foo=1',
59+
)
60+
t.is(
61+
paramsSerializer({ foo: 1, bar: ['', 'a', ''] }),
62+
'bar=&bar=a&bar=&foo=1',
63+
)
64+
})
65+
66+
test('cannot serialize single element array params with empty string', (t) => {
67+
t.throws(() => paramsSerializer({ foo: [''] }), {
68+
instanceOf: UnserializableParamError,
69+
})
5570
})
5671

5772
test('cannot serialize unserializable values', (t) => {

src/lib/params-serializer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export const paramsSerializer: CustomParamsSerializer = (params) => {
88

99
if (Array.isArray(value)) {
1010
if (value.length === 0) searchParams.set(name, '')
11+
if (value.length === 1 && value[0] === '') {
12+
throw new UnserializableParamError(
13+
name,
14+
`is a single element array containing the empty string which is unsupported because it serializes to the empty array`,
15+
)
16+
}
1117
for (const v of value) {
1218
throwIfUnserializable(name, v)
1319
searchParams.append(name, v)

0 commit comments

Comments
 (0)