Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/System.Management.Automation/engine/ParameterBinderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,8 @@ private object EncodeCollection(
"Argument type {0} is not IList, treating this as scalar",
currentValue.GetType().Name);

object currentValueElement = PSObject.Base(currentValue);

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.

This applies to every collection type, and that is too broad. It's better to special case the StringCollection type as the WG comment pointed out in the issue.


if (collectionElementType != null)
{
if (coerceElementTypeIfNeeded)
Expand All @@ -1805,17 +1807,17 @@ private object EncodeCollection(

// Coerce the scalar type into the collection

currentValue =
currentValueElement =
CoerceTypeAsNeeded(
argument,
parameterName,
collectionElementType,
null,
currentValue);
}
else
else if (currentValueElement != null)
{
Type currentValueElementType = currentValue.GetType();
Type currentValueElementType = currentValueElement.GetType();
Type desiredElementType = collectionElementType;

if (currentValueElementType != desiredElementType &&
Expand All @@ -1840,23 +1842,23 @@ private object EncodeCollection(
{
bindingTracer.WriteLine(
"Adding scalar element of type {0} to array position {1}",
(currentValue == null) ? "null" : currentValue.GetType().Name,
(currentValueElement == null) ? "null" : currentValueElement.GetType().Name,
0);
resultAsIList[0] = currentValue;
resultAsIList[0] = currentValueElement;
}
else if (collectionTypeInformation.ParameterCollectionType == ParameterCollectionType.IList)
{
bindingTracer.WriteLine(
"Adding scalar element of type {0} via IList.Add",
(currentValue == null) ? "null" : currentValue.GetType().Name);
resultAsIList.Add(currentValue);
(currentValueElement == null) ? "null" : currentValueElement.GetType().Name);
resultAsIList.Add(currentValueElement);
}
else
{
bindingTracer.WriteLine(
"Adding scalar element of type {0} via ICollection<T>::Add()",
(currentValue == null) ? "null" : currentValue.GetType().Name);
addMethod.Invoke(resultCollection, new object[1] { currentValue });
(currentValueElement == null) ? "null" : currentValueElement.GetType().Name);
addMethod.Invoke(resultCollection, new object[1] { currentValueElement });
}
}
catch (Exception error) // OK, we catch all here by design
Expand All @@ -1877,10 +1879,10 @@ private object EncodeCollection(
GetErrorExtent(argument),
parameterName,
toType,
currentValue?.GetType(),
currentValueElement?.GetType(),
ParameterBinderStrings.CannotConvertArgument,
"CannotConvertArgument",
currentValue ?? "null",
currentValueElement ?? "null",
error.Message);
throw bindingException;
}
Expand Down
56 changes: 56 additions & 0 deletions test/powershell/Language/Parser/ParameterBinding.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,59 @@ Describe 'Roundtrippable Conversions for Bare-string Numeric Literals passed to
Invoke-Expression "Test-AdvancedStringValue -Value $Argument" | Should -BeExactly $Argument
}
}

Describe 'Scalar PSObject binding to non-generic IList parameters' -Tags 'CI' {
BeforeAll {
$typeDefinition = @'
using System.Collections.Specialized;
using System.Management.Automation;

namespace ScalarPSObjectCollectionBinding
{
[Cmdlet(VerbsDiagnostic.Test, "ScalarStringCollectionBinding")]
[OutputType(typeof(string))]
public sealed class TestScalarStringCollectionBindingCommand : PSCmdlet
{
[Parameter(Mandatory = true)]
public StringCollection Value { get; set; }

protected override void ProcessRecord()
{
WriteObject(Value[0]);
}
}
}
'@

$types = Add-Type -PassThru -TypeDefinition $typeDefinition
Import-Module $types[0].Assembly
}

It 'binds a scalar PSObject wrapping a string to StringCollection' {
$value = @('expected', 'expected') | Select-Object -Unique

# Confirm that this is the specific problematic input shape.
$value.GetType().FullName | Should -BeExactly 'System.String'
($value -is [System.Management.Automation.PSObject]) |
Should -BeTrue

Test-ScalarStringCollectionBinding -Value $value |
Should -BeExactly 'expected'
}

It 'continues to bind a plain scalar string' {
Test-ScalarStringCollectionBinding -Value 'expected' |
Should -BeExactly 'expected'
}

It 'continues to bind multiple pipeline-produced strings' {
$value = @('first', 'second') | Select-Object -Unique

$result = @(
Test-ScalarStringCollectionBinding -Value $value
)

# The test cmdlet writes Value[0], proving binding succeeded.
$result | Should -BeExactly 'first'
}
}
Loading