fix(engine): unwrap scalar PSObject during collection binding by daksh-goyal · Pull Request #27970 · PowerShell/PowerShell · GitHub
Skip to content

fix(engine): unwrap scalar PSObject during collection binding - #27970

Open
Daksh Goyal (daksh-goyal) wants to merge 1 commit into
PowerShell:masterfrom
daksh-goyal:master
Open

fix(engine): unwrap scalar PSObject during collection binding#27970
Daksh Goyal (daksh-goyal) wants to merge 1 commit into
PowerShell:masterfrom
daksh-goyal:master

Conversation

@daksh-goyal

Copy link
Copy Markdown

PR Summary

Unwrap scalar values before adding them to collection parameters, matching the existing behavior for list elements. This prevents StringCollection parameters from attempting to cast a PSObject directly to a string.

Add regression coverage for a single pipeline-produced value.

PR Context

fixes #27969

PR Checklist

Unwrap scalar values before adding them to collection parameters, matching the existing behavior for list elements. This prevents StringCollection parameters from attempting to cast a PSObject directly to a string.

Add regression coverage for a single pipeline-produced value.
@daksh-goyal
Daksh Goyal (daksh-goyal) requested a review from a team as a code owner September 3, 2026 21:28
Copilot AI lite review requested due to automatic review settings September 3, 2026 21:28
@azure-pipelines

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The binding fix is targeted and low-risk, and it is backed by a focused regression test covering the reported failure scenario.

Pull request overview

This PR fixes a parameter-binding edge case where a scalar PSObject (wrapping a string) was added directly to non-generic IList-backed collection parameters (e.g., StringCollection), causing invalid casts; it now unwraps the scalar value first, matching existing behavior for list elements.

Changes:

  • Unwrap scalar values via PSObject.Base(...) before adding them to collection parameters in ParameterBinderBase.EncodeCollection.
  • Add a regression Pester test that reproduces the failure shape using a StringCollection parameter and a single pipeline-produced scalar value.
File summaries
File Description
test/powershell/Language/Parser/ParameterBinding.Tests.ps1 Adds regression coverage for binding a scalar pipeline-produced PSObject to StringCollection.
src/System.Management.Automation/engine/ParameterBinderBase.cs Unwraps scalar PSObject values prior to collection insertion to avoid invalid casts and align with element-binding behavior.
Review details

Suppressed comments (1)

src/System.Management.Automation/engine/ParameterBinderBase.cs:1827

  • The binding tracer format string uses {1} but only supplies a single argument, so traces will literally print {1} instead of the element type. This makes diagnosing coercion-required cases harder.
                            if (currentValueElementType != desiredElementType &&
                                !currentValueElementType.IsSubclassOf(desiredElementType))
                            {
                                bindingTracer.WriteLine(
                                    "COERCION REQUIRED: Did not coerce scalar arg value to type {1}",
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

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.

Binary cmdlet binding fails to unwrap a scalar PSObject for StringCollection parameters

2 participants