Skip to content

Bug Report: qs.parse when parsing a string with empty indexes [] in a key. #260

@leoyli

Description

@leoyli

I think there is a bug in parseArrays: false option:

normal case with index

qs.parse('a[0]=b', { arrayLimit: 0 }) === { a: [ 'b' ] } // true  <-- expected
qs.parse('a[0]=b', { parseArrays: false }) === { a: { '0': 'b' } } // true  <-- expected

buggy case with empty index

qs.parse('a[]=b', { arrayLimit: 0 }) === '{ a: [ 'b' ] }' // true  <-- expected
qs.parse('a[]=b', { parseArrays: false }) === '{ a: [ 'b' ] }' // true  <-- unexpected

Instead, I will expect if I set parseArrays: false, I will receive no Array:

qs.parse('a[]=b', { parseArrays: false }) === '{ 'a': { '0': 'b' } }' // true

That is because the array parsing is only preserving the order, not by their true indexes in the query, according to the doc:

qs will compact a sparse array to only the existing values preserving their order

Also it is consistent with when other option being set (i.e. treat [] as [0], see below.)

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions