PHANTOM
🇮🇳 IN
Skip to content

[Fix] parse: fix error message to reflect arrayLimit as max index; remove extraneous comments#545

Merged
ljharb merged 1 commit intoljharb:mainfrom
kaigritun:fix/arraylimit-max-index
Feb 11, 2026
Merged

[Fix] parse: fix error message to reflect arrayLimit as max index; remove extraneous comments#545
ljharb merged 1 commit intoljharb:mainfrom
kaigritun:fix/arraylimit-max-index

Conversation

@kaigritun
Copy link

Summary

Fixes #540

The arrayLimit option is documented as limiting the maximum index in an array:

qs will also limit specifying indices in an array to a maximum index of 20.

However, it was incorrectly being applied to array length, causing inconsistent behavior between indexed (param[0]=val) and non-indexed (param[]=val) array parameters.

Example (from issue #540)

const settings = { arrayLimit: 3 };

// 4 elements with indices 0-3, max index = 3
const t1 = 'param[]=val&param[]=val&param[]=val&param[]=val';
const p1 = qs.parse(t1, settings);
// BEFORE: { param: { '0': 'val', ... } } // object - wrong!
// AFTER:  { param: ['val', ...] }        // array - correct!

const t2 = 'param[0]=val&param[1]=val&param[2]=val&param[3]=val';
const p2 = qs.parse(t2, settings);
// Both cases: { param: ['val', ...] }    // array - correct!

Changes

  • utils.js combine(): Check length - 1 > limit instead of length > limit
  • utils.js merge(): Apply arrayLimit when appending to arrays
  • parse.js parseArrayValue(): Use > instead of >= for throwOnLimitExceeded check

Test Updates

Updated test expectations to reflect correct behavior per documentation:

  • With arrayLimit: 3, arrays with max index 3 (4 elements) now stay as arrays
  • With arrayLimit: 0, arrays with max index 0 (1 element) now stay as arrays

…remove extraneous comments

The arrayLimit option is documented as limiting the 'maximum index'
    in an array, but was incorrectly being applied to the array length.
    This caused inconsistent behavior between indexed (param[0]=val) and
    non-indexed (param[]=val) array parameters.

With arrayLimit: 3:
 - BEFORE: 4 elements (indices 0-3) would convert to object since
      length 4 > limit 3
 - AFTER: 4 elements (indices 0-3) stays as array since max index
      3 <= limit 3

Changes:
 - utils.js combine(): Check 'length - 1 > limit' instead of 'length > limit'
 - utils.js merge(): Apply arrayLimit when appending to arrays
 - parse.js parseArrayValue(): Use '>' instead of '>=' for throwOnLimitExceeded check

Tests updated to reflect correct behavior per documentation.

Fixes ljharb#540.

Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Kai Gritun <kai@kaigritun.com>
@ljharb ljharb force-pushed the fix/arraylimit-max-index branch 2 times, most recently from 8b8fc99 to 0ba2bfd Compare February 10, 2026 21:33
@ljharb ljharb changed the title fix: arrayLimit applies to max index, not length [Fix] parse: fix error message to reflect arrayLimit as max index; remove extraneous comments Feb 10, 2026
Copy link
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reduced this to a more minimal implementation.

@ljharb ljharb force-pushed the fix/arraylimit-max-index branch 2 times, most recently from 6c2db2d to fbc5206 Compare February 11, 2026 00:09
@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.75%. Comparing base (fbc5206) to head (fbc5206).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #545   +/-   ##
=======================================
  Coverage   99.75%   99.75%           
=======================================
  Files           9        9           
  Lines        2048     2048           
  Branches      229      229           
=======================================
  Hits         2043     2043           
  Misses          5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arrayLimit applies to length instead of index for parameters without index

2 participants