Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
leoyli opened this issue May 13, 2018 · 2 comments
Closed
Assignees

Comments

@leoyli
Copy link

leoyli commented May 13, 2018

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.)

@ljharb
Copy link
Owner

ljharb commented May 13, 2018

Regarding bug 1: parseArrays: false should work like this example in the readme:

var noParsingArrays = qs.parse('a[]=b', { parseArrays: false });
assert.deepEqual(noParsingArrays, { a: { '0': 'b' } });

Indeed, this is a bug; I've added it to the tests and will fix it shortly.

Regarding bug 2: can you file that separately, and we can discuss there?

@leoyli leoyli changed the title Bugs in qs.parse when parsing string to Array meet empty indexes []. Bug Report: qs.parse when parsing a string with empty indexes [] in a key. May 13, 2018
@leoyli
Copy link
Author

leoyli commented May 13, 2018

Filed another bug in #262.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants