Thanks to visit codestin.com
Credit goes to github.com

Skip to content

parseArrays: false does not prevent parsing as arrays #543

@krzysdz

Description

@krzysdz

With duplicates set to 'combine' (default) parseArrays: false can parse some query strings as arrays. The readme says:

qs/README.md

Lines 313 to 318 in 6bdfaf5

To disable array parsing entirely, set `parseArrays` to `false`.
```javascript
var noParsingArrays = qs.parse('a[]=b', { parseArrays: false });
assert.deepEqual(noParsingArrays, { a: { '0': 'b' } });
```

To me it suggests that with this option disabled I should never get an array. Other interpretation could be that this applies only to a[] and a[0] notations, since the "Parsing arrays" section mentions only those two notations and parsing repeating keys as arrays is mentioned only in the duplicates section (counter point: the test for a=b&a=c was literally called a simple array test in one of the first commits to this repository - 622da20).

On the current main (6bdfaf5) (should be same in 16.4.1) and 16.4.0 I see the following behaviour:

query arrayLimit parseArrays result - 6bdfaf5 result - 16.4.0
a=b&a=c Infinity false { a: [ 'b', 'c' ] } { a: [ 'b', 'c' ] }
a[]=b&a[]=c Infinity false { a: { '0': [ 'b', 'c' ] } } { a: { '0': [ 'b', 'c' ] } }
a=b&a=c Infinity true { a: [ 'b', 'c' ] } { a: [ 'b', 'c' ] }
a[]=b&a[]=c Infinity true { a: [ 'b', 'c' ] } { a: [ 'b', 'c' ] }
a=b&a=c -1 false { a: { '0': 'b', '1': 'c' } } { a: [ 'b', 'c' ] }
a[]=b&a[]=c -1 false { a: { '0': { '0': 'b', '1': 'c' } } } { a: { '0': [ 'b', 'c' ] } }
a=b&a=c -1 true { a: { '0': 'b', '1': 'c' } } { a: [ 'b', 'c' ] }
a[]=b&a[]=c -1 true { a: { '0': 'b', '1': 'c' } } { a: [ 'b', 'c' ] }

This is not what I would expect based on the documentation (readme), but I think I may know the historical reason for this behaviour. arrayLimit was intended (until 16.4.1) to restrict only arrays with explicit indexes (see 4667c41, 8c5cda3, #294 and a longer analysis in #537 (comment)) and I believe parseArrays: false was meant to replace arrayLimit: -1 (#60, cc735a5) and affect the notation with indices, not repetitions (8e299ca).

This is related also to #260 and 2b94ea7, which extended parseArrays from indexed-only to also apply to [] notation, without index.

I think this is a documentation "bug" that requires clarification or maybe a combination of documentation and code problems (especially with changes to 6.14.1), but this requires answering some questions:

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions