ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.5k stars 729 forks source link

Object with "number" keys are stringyfied as arrays #411

Open richarddd opened 3 years ago

richarddd commented 3 years ago

If i run this code i would expect the resulting object to be {params: {"2": "1"}}

qs.parse(qs.stringify({params: {"2": 1}}))

Qs parses it as: {params: ["1"]}

ljharb commented 3 years ago

Since qs.stringify({params: {"2": 1}}, { encodeValuesOnly: true }) produces 'params[2]=1', this seems like an issue with parse.

However, while qs.parse('params[2]=1') produces { params: [ '1' ] }, qs.parse('params[2]=1', { allowSparse: true }) produces { params: [ <2 empty items>, '1' ] }, so i think the thing you're looking for is the allowSparse option?

dessskris commented 3 years ago

Hi, we are having the same issue, however we only observe it on numbers up to 20.

So for example, this will work correctly:

{params: {"21": "1"}}

but not this:

{params: {"20": "1"}}

I don't have the allowSparse option (FYI we are using qs version 6.7.0)

Any ideas? Thanks.

ljharb commented 3 years ago

@dessskris thats because the arrayLimit option defaults to 20.