Skip to content

Commit 123b450

Browse files
authored
Handle null reference exception in CsvCommands.cs: ConvertPSObjectToCSV (#26144)
1 parent e733fd7 commit 123b450

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ internal string ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyN
10441044
{
10451045
if (dictionary.Contains(propertyName))
10461046
{
1047-
value = dictionary[propertyName].ToString();
1047+
value = dictionary[propertyName]?.ToString();
10481048
}
10491049
else if (mshObject.Properties[propertyName] is PSPropertyInfo property)
10501050
{

test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ Describe "ConvertTo-Csv" -Tags "CI" {
212212
[ordered]@{ Number = $_; Letter = $Letters[$_] }
213213
}
214214
$CsvString = $Items | ConvertTo-Csv
215+
$TestHashTable = [ordered]@{ 'first' = 'value1'; 'second' = $null; 'third' = 'value3' }
215216
}
216217

217218
It 'should treat dictionary entries as properties' {
@@ -235,5 +236,12 @@ Describe "ConvertTo-Csv" -Tags "CI" {
235236
$NewCsvString[0] | Should -MatchExactly 'Extra'
236237
$NewCsvString | Select-Object -Skip 1 | Should -MatchExactly 'Surprise!'
237238
}
239+
240+
It 'should properly convert hashtable with null and non-null values'{
241+
$CsvResult = $TestHashTable | ConvertTo-Csv
242+
243+
$CsvResult[0] | Should -BeExactly "`"first`",`"second`",`"third`""
244+
$CsvResult[1] | Should -BeExactly "`"value1`",$null,`"value3`""
245+
}
238246
}
239247
}

test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Describe "Export-Csv" -Tags "CI" {
1111
$P1 = [pscustomobject]@{"P1" = "first"}
1212
$P2 = [pscustomobject]@{"P2" = "second"}
1313
$P11 = [pscustomobject]@{"P1" = "eleventh"}
14+
$testHashTable = @{ 'first' = "value1"; 'second' = $null; 'third' = "value3" }
1415
}
1516

1617
AfterEach {
@@ -249,6 +250,33 @@ Describe "Export-Csv" -Tags "CI" {
249250
$contents[1].Contains($delimiter) | Should -BeTrue
250251
}
251252

253+
It "Should not throw when exporting hashtable with property that has null value"{
254+
{ $testHashTable | Export-Csv -Path $testCsv } | Should -Not -Throw
255+
}
256+
257+
It "Should not throw when exporting PSCustomObject with property that has null value"{
258+
$testObject = [pscustomobject]$testHashTable
259+
{ $testObject | Export-Csv -Path $testCsv } | Should -Not -Throw
260+
}
261+
262+
It "Export hashtable with null and non-null values"{
263+
$testHashTable | Export-Csv -Path $testCsv
264+
$result2 = Import-CSV -Path $testCsv
265+
266+
$result2.first | Should -BeExactly "value1"
267+
$result2.second | Should -BeNullOrEmpty
268+
$result2.third | Should -BeExactly "value3"
269+
}
270+
271+
It "Export hashtable with non-null values"{
272+
$testTable = @{ 'first' = "value1"; 'second' = "value2" }
273+
$testTable | Export-Csv -Path $testCsv
274+
$results = Import-CSV -Path $testCsv
275+
276+
$results.first | Should -BeExactly "value1"
277+
$results.second | Should -BeExactly "value2"
278+
}
279+
252280
Context "UseQuotes parameter" {
253281

254282
# A minimum of tests. The rest are in ConvertTo-Csv.Tests.ps1

0 commit comments

Comments
 (0)