Skip to content

DefaultValuePipe truncates sparse array defaults when copying them #18548

Closed

Description

@Rio3210

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When DefaultValuePipe supplies a sparse array default, the returned copy loses trailing empty slots.

For example, a default created with new Array(7) produces an array of length 0 instead of 7. A length-7 array with only index 1 populated produces length 2. Nested sparse arrays are also affected.

This silently changes the value; Nest itself does not throw an exception. The reproduction asserts that the length should be preserved and fails with:

Configured default length: 7
Returned default length: 0
Returned default: []

AssertionError [ERR_ASSERTION]: DefaultValuePipe should preserve the configured array length

0 !== 7

Seven is just an example; this also affects other lengths with trailing empty slots.

Minimum reproduction code

https://github.com/Rio3210/nest-default-value-repro

Steps to reproduce

git clone https://github.com/Rio3210/nest-default-value-repro.git
cd nest-default-value-repro
npm ci
npm start

Expected behavior

The copied default array should preserve the original array's length, populated indices, and empty slots while remaining a separate array instance.

For new Array(7), the returned array should have length 7. The same preservation should apply to sparse arrays nested inside plain-object defaults.

NestJS version

12.5.1

Packages versions

{
  "@nestjs/common": "12.5.1",
  "reflect-metadata": "0.2.2",
  "rxjs": "7.8.2"
}

Node.js version

22.19.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

Proposed fix:

In packages/common/pipes/default-value.pipe.ts, copyDefaultValue() starts the array copy with [] and uses Object.assign. Array length is nonenumerable, so it is not copied. Only populated indices extend the destination length.

Initialize the destination with new Array(value.length) instead of [] while retaining the existing copying logic.

I have a local fix with three regression tests covering:

  • Arrays containing only empty slots.
  • Partially populated arrays with trailing empty slots.
  • Nested sparse arrays.

These tests fail before the fix and pass afterward. All 1,169 common tests pass with NO_COLOR unset, along with the common TypeScript check and changed-file lint. The full repository/integration suite has not been run.

I also verified the behavior using @nestjs/core and @nestjs/platform-fastify 12.5.1 through Fastify's in-process request injection.

I would be happy to submit a PR with the fix and regression tests.

Activity

  1. added theissue type on Nov 17, 2026
  2. added
    needs triage
    on Nov 17, 2026
  3. micalevisk commented on Nov 17, 2026

    @micalevisk
    Member

    Thanks for the report and the runnable repro — confirmed on 12.5.1 and on master.

    This is a regression from the change that made DefaultValuePipe copy array/object defaults (931007e, released in 12.2.0, and backported to 11.2.8). Before that the default was returned by reference, so its length was preserved. Object.assign([], value) only carries the populated indices, so trailing empty slots are dropped (leading and middle ones survive because a later index extends the length past them).

    A PR is welcome. One note on the fix: new Array(value.length) works, but it makes every copy a holey array in V8, including the common dense case (new DefaultValuePipe([])). Restoring the length after the copy avoids that and gives the same result:

    const copy = Object.assign(
      isArray ? [] : Object.create(Object.getPrototypeOf(value)),
      value,
    );
    if (isArray) {
      copy.length = value.length;
    }

    Please target master and add the three cases you listed (only empty slots, trailing empty slots after a populated index, sparse array nested in a plain object) to packages/common/test/pipes/default-value.pipe.spec.ts.

    Whether this also gets backported to v11 is up to @kamilmysliwiec.

  4. Rio3210 commented on Nov 17, 2026

    @Rio3210
    ContributorAuthor

    Thanks for confirming and for the implementation suggestion!

    I’ve opened PR #18551 targeting master. It restores copy.length after Object.assign, as recommended, and includes all three requested regression cases: arrays containing only empty slots, trailing empty slots after a populated index, and a sparse array nested in a plain object.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triage

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions