Skip to content

Handle null reference exception in CsvCommands.cs: ConvertPSObjectToCSV#26144

Merged
iSazonov merged 2 commits intoPowerShell:masterfrom
mikkas456:exportcsv-null-reference-fix
Oct 16, 2025
Merged

Handle null reference exception in CsvCommands.cs: ConvertPSObjectToCSV#26144
iSazonov merged 2 commits intoPowerShell:masterfrom
mikkas456:exportcsv-null-reference-fix

Conversation

@mikkas456
Copy link
Copy Markdown
Contributor

@mikkas456 mikkas456 commented Oct 3, 2025

PR Summary

This PR handles a null reference exception in the method ConvertPSObjectToCSV in the file CsvCommands.cs. This fixes a crash that would occur when passing a hash table with a null value to Export-Csv and ConvertTo-Csv. This PR also adds several test cases to Export-Csv.Tests.ps1 and ConvertTo-Csv.Tests.ps1, which will test if Export-Csv and ConvertTo-Csv can properly handle hash tables.

PR Context

Closes #25836

PR Checklist

@mikkas456
Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Comment on lines +1047 to +1055
//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.

Comment on lines +240 to +243
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.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 3, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 3, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Oct 13, 2025
if (dictionary.Contains(propertyName))
{
value = dictionary[propertyName].ToString();
value = dictionary[propertyName]?.ToString();
Copy link
Copy Markdown
Contributor

@xtqqczze xtqqczze Oct 15, 2025

Choose a reason for hiding this comment

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

I wonder if value is null here, we should try GetToStringValueForProperty?

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.

If you know how to reproduce a scenario where this could be happen please open new issue.

@iSazonov iSazonov merged commit 123b450 into PowerShell:master Oct 16, 2025
37 of 38 checks passed
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Oct 16, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

@mikkas456 Thanks for your contribution!

@TravisEz13
Copy link
Copy Markdown
Member

@PowerShell/powershell-maintainers I don't think we can bring this to 7.6 and therefore not 7.5. removing the backport label unless we get more feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export-Csv cannot export hashtables with null values

5 participants