Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1044,7 +1044,15 @@ internal string ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyN
{
if (dictionary.Contains(propertyName))
{
value = dictionary[propertyName].ToString();
//Handle null reference exception for existing property with null value
if (dictionary[propertyName] == null)
{
value = null;
}
else
{
value = dictionary[propertyName].ToString();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we simplify the code?

Suggested change
//Handle null reference exception for existing property with null value
if (dictionary[propertyName] == null)
{
value = null;
}
else
{
value = dictionary[propertyName].ToString();
}
value = dictionary[propertyName]?.ToString();

Also please remove obvious comment.

}
else if (mshObject.Properties[propertyName] is PSPropertyInfo property)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Describe "ConvertTo-Csv" -Tags "CI" {
[ordered]@{ Number = $_; Letter = $Letters[$_] }
}
$CsvString = $Items | ConvertTo-Csv
$TestHashTable = [ordered]@{ 'first' = 'value1'; 'second' = $null; 'third' = 'value3' }
}

It 'should treat dictionary entries as properties' {
Expand All @@ -235,5 +236,16 @@ Describe "ConvertTo-Csv" -Tags "CI" {
$NewCsvString[0] | Should -MatchExactly 'Extra'
$NewCsvString | Select-Object -Skip 1 | Should -MatchExactly 'Surprise!'
}

It 'should convert hashtable with null values without error'{
{ $TestHashTable | ConvertTo-Csv } | Should -Not -Throw
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can remove the test since next one does the check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Alright, I have addressed these issues in another commit.

It 'should properly convert hashtable with null and non-null values'{
$CsvResult = $TestHashTable | ConvertTo-Csv

$CsvResult[0] | Should -BeExactly "`"first`",`"second`",`"third`""
$CsvResult[1] | Should -BeExactly "`"value1`",$null,`"value3`""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Describe "Export-Csv" -Tags "CI" {
$P1 = [pscustomobject]@{"P1" = "first"}
$P2 = [pscustomobject]@{"P2" = "second"}
$P11 = [pscustomobject]@{"P1" = "eleventh"}
$testHashTable = @{ 'first' = "value1"; 'second' = $null; 'third' = "value3" }
}

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

It "Should not throw when exporting hashtable with property that has null value"{
{ $testHashTable | Export-Csv -Path $testCsv } | Should -Not -Throw
}

It "Should not throw when exporting PSCustomObject with property that has null value"{
$testObject = [pscustomobject]$testHashTable
{ $testObject | Export-Csv -Path $testCsv } | Should -Not -Throw
}

It "Export hashtable with null and non-null values"{
$testHashTable | Export-Csv -Path $testCsv
$result2 = Import-CSV -Path $testCsv

$result2.first | Should -BeExactly "value1"
$result2.second | Should -BeNullOrEmpty
$result2.third | Should -BeExactly "value3"
}

It "Export hashtable with non-null values"{
$testTable = @{ 'first' = "value1"; 'second' = "value2" }
$testTable | Export-Csv -Path $testCsv
$results = Import-CSV -Path $testCsv

$results.first | Should -BeExactly "value1"
$results.second | Should -BeExactly "value2"
}

Context "UseQuotes parameter" {

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