Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3901,7 +3901,11 @@ internal object Convert(object valueToConvert,

if (IsScalar)
{
resultAsList.Add(valueToConvert);
// Fix for Issue #17731: Convert scalar to element type before adding to list
// Previously, this directly added the value without conversion, which failed for
// types like bool where the value's runtime type differs from the expected type
object convertedValue = LanguagePrimitives.ConvertTo(valueToConvert, ElementType, formatProvider);
resultAsList.Add(convertedValue);
}
else if (array == null)
{
Expand Down
28 changes: 28 additions & 0 deletions test/powershell/engine/Api/LanguagePrimitive.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,32 @@ Describe "Language Primitive Tests" -Tags "CI" {
$convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint])
$convertedValue | Should -Be 1
}

# Issue #17731 - Generic List conversion from scalar
It 'Converts Boolean scalar to Generic List[bool]' {
$result = [System.Collections.Generic.List[bool]]$true
$result | Should -BeOfType [System.Collections.Generic.List[bool]]
$result.Count | Should -Be 1
$result[0] | Should -Be $true
}

It 'Converts Boolean `$false to Generic List[bool]' {
$result = [System.Collections.Generic.List[bool]]$false
$result | Should -BeOfType [System.Collections.Generic.List[bool]]
$result.Count | Should -Be 1
$result[0] | Should -Be $false
}

It 'Converts Boolean variable to Generic List[bool]' {
$boolVar = $true
$result = [System.Collections.Generic.List[bool]]$boolVar
$result[0] | Should -Be $true
}

It 'Converts PSObject to Generic List[psobject]' {
$result = [System.Collections.Generic.List[psobject]]$true
$result | Should -BeOfType [System.Collections.Generic.List[psobject]]
$result.Count | Should -Be 1
$result[0] | Should -Be $true
}
}