ljharb / qs

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

When an array has more than 21 items, it is parsed from a url string into an object, instead of array #449

Closed anuragsachdeva28 closed 2 years ago

anuragsachdeva28 commented 2 years ago

The following issue has been raised and closed before. In that case, the issue was with qs-middleware used on server side, but in my case I am not using qs-middleware on my express server. I think there is something internal parsing happening from express. Can anyone help?

Note: This is happening to me, but with qs.stringify, not with parse and I can see there's no arrayLimit option for the stringify method.

Originally posted by @anuragsachdeva28 in https://github.com/ljharb/qs/issues/324#issuecomment-1200104069

ljharb commented 2 years ago

This seems like a duplicate of #324.

Can you elaborate about what exactly is happening? Specifically, can you provide an input string, the stringify options you're using, and the output you're getting vs expecting?

anuragsachdeva28 commented 2 years ago

As mentioned in the official documentation, qs will also limit specifying indices in an array to a maximum index of 20. Any array members with an index of greater than 20 will instead be converted to an object with the index as the key. The solution provided in the documentation was to pass the array limit option while parsing.

But in my case, I am just using qs on client-side(React) to stringify my query params and on the node-server, it is automatically getting parsed. When I receive the parsed query params, my array is converted into object, if the length exceeds 21.

In #324, the final solution provided was by James, where he was using qs-middleware on his node server. In my case, I am not even using that.

Please find the sample input and output below: Input: { "A": [ "11051", "10333", "5902", "11009", "5598" ], "B": [ "4410", "5028", "4611", "4306", "8417", "12122", "8430", "8456", "8304", "8926", "8925", "4405", "10607", "2601", "10532", "12511", "8605", "7901", "8406", "10546", "2606", "8451", "4602" ] } Output: { "A": [ "11051", "10333", "5902", "11009", "5598" ], "B": { "0": "4410", "1": "5028", "2": "4611", "3": "4306", "4": "8417", "5": "12122", "6": "8430", "7": "8456", "8": "8304", "9": "8926", "10": "8925", "11": "4405", "12": "10607", "13": "2601", "14": "10532", "15": "12511", "16": "8605", "17": "7901", "18": "8406", "19": "10546", "20": "2606", "21": "8451", "22": "4602" } }

In the above example, the second key, i.e. B, has a length of more than 21, and thus just for B, instead of an array, I am receiving it as an object

ljharb commented 2 years ago

Ok, so what’s doing the parsing on the server?

anuragsachdeva28 commented 2 years ago

I am not using any external library for doing parsing on my node-server. My guess is, express server is internally parsing the query params.

ljharb commented 2 years ago

Then it seems like you may need to file an issue on express? I’m not sure if they expose qs options.

anuragsachdeva28 commented 2 years ago

I found the solution to this. On the client side, while using stringify function, we just need to pass { arrayFormat: 'brackets' }, as an option.