Skip to content

fix(common): preserve sparse array length in default value pipe - #18551

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
Rio3210:fix/default-value-sparse-array-length
Nov 18, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
Rio3210:fix/default-value-sparse-array-length

Conversation

@Rio3210

@Rio3210 Rio3210 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #18548

PR Checklist

Please check if your PR fulfills the following requirements:

Documentation changes are not needed: this restores preservation of the configured default's array length without introducing an API change.

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

DefaultValuePipe loses trailing empty slots when copying a sparse array default. For example, new Array(7) becomes an empty array, changing the value passed to the handler.

Issue Number: #18548

What is the new behavior?

Restore the source array's length after copying its enumerable properties. This preserves sparse array length while retaining the existing allocation for dense arrays, as suggested in the issue discussion.

Adds regression tests for arrays containing only empty slots, a populated index followed by empty slots, and a sparse array nested inside a plain object. The tests also check that copies remain separate and empty slots are preserved.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Validated on Windows with Node.js 22.19.0:

  • npm run test -- packages/common/test with NO_COLOR unset: 88 files, 1,169 tests passed, including the three regressions.
  • Common TypeScript check (tsc --project packages/common/tsconfig.build.json --noEmit): passed.
  • Changed-file Prettier check and git diff --check: passed.
  • npm run lint:ci: exit 0, with an existing optional-chain warning in packages/websockets/socket-module.ts.

The full unit suite was attempted on Windows but exited 1. Running the unchanged packages/core/test/nest-application-context.spec.ts separately reproduces an unexpected Vitest worker exit in its shutdown-signal test. The full suite therefore has not passed cleanly in this environment. Integration tests have not been run.

@kamilmysliwiec
kamilmysliwiec merged commit e91297d into nestjs:master Nov 18, 2026
2 checks passed
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.

DefaultValuePipe truncates sparse array defaults when copying them

2 participants