Skip to content

fix: do not drop largeArraySize when it is 0 - #922

Open
eknathdj wants to merge 2 commits into
fastify:mainfrom
eknathdj:fix/large-array-size-zero
Open

eknathdj wants to merge 2 commits into
fastify:mainfrom
eknathdj:fix/large-array-size-zero

Conversation

@eknathdj

@eknathdj eknathdj commented Nov 9, 2026

Copy link
Copy Markdown

Description

largeArraySize is read behind a truthiness guard, so the value 0 is silently ignored and the default 20000 is kept:

if (options.largeArraySize) {

0 should be a valid value for this option. The README documents it as accepting "integer-like values", the validation branch just below the guard happily accepts integers, and largeArraySize: 0 is the natural way to say "treat every array as large" so that largeArrayMechanism: 'json-stringify' applies to all of them. Today the setting just does nothing — while the string form '0' works, because '0' is truthy. The same value behaves differently depending on its type.

With { largeArrayMechanism: 'json-stringify' } and the schema { type: 'array', items: { type: 'string' } }, serializing [1, 2]:

largeArraySize output expected
omitted ["1","2"] yes — below the default threshold, so items are coerced
0 (number) ["1","2"] no — the setting had no effect
'0' (string) [1,2] yes — the mechanism applies

After this change the number form behaves like the string form.

The guard now treats only undefined as "not set", which is the same convention options.maxDepth already uses a few lines above. null still throws: it reaches the validation branch and is not integer-like. I left the other truthiness guards (options.rounding, options.largeArrayMechanism, options.objectMechanism) alone — none of their falsy values is meaningful, so changing them would only make previously-ignored nonsense values throw.

Notes

  • The new test in test/array.test.js pins all three cases from the table above: 0, '0', and the omitted default.

The option was read behind a truthiness check, so the integer 0 was silently ignored and the default of 20000 kept, while the string form of 0 worked. Treat only undefined as unset, matching how options.maxDepth is handled a few lines above.
@SameRainbows

Copy link
Copy Markdown

Tested locally on Node 24 and verified across the test suite.

Key points verified:

  • Previously, \if (options.largeArraySize)\ evaluated to \ alse\ when numeric \largeArraySize: 0\ was passed, silently falling back to the default 20000 threshold.
  • The downstream validation \Number.isInteger(parsedNumber) && parsedNumber >= 0\ already allowed \

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lgtm.

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR, the fix is correct and the description is clear.

One detail on null: the description says it "still throws", but on main largeArraySize: null is accepted and ignored, like false, '' and NaN. So after this change these four values start to throw Unsupported large array size.

For false, '' and NaN that is consistent with the existing validation. For null both choices have a precedent: maxDepth rejects it, while rounding, largeArrayMechanism and objectMechanism accept it.

Since this is a fix, I would keep null as "not set", so that nobody gets a new error from a patch release:

if (options.largeArraySize != null) {

If you prefer to keep it strict, that is fine for me too: in that case please add null to the test, so the behavior is pinned.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants