Handle null reference exception in CsvCommands.cs: ConvertPSObjectToCSV#26144
Conversation
|
@microsoft-github-policy-service agree |
| //Handle null reference exception for existing property with null value | ||
| if (dictionary[propertyName] == null) | ||
| { | ||
| value = null; | ||
| } | ||
| else | ||
| { | ||
| value = dictionary[propertyName].ToString(); | ||
| } |
There was a problem hiding this comment.
Can we simplify the code?
| //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.
| It 'should convert hashtable with null values without error'{ | ||
| { $TestHashTable | ConvertTo-Csv } | Should -Not -Throw | ||
| } | ||
|
|
There was a problem hiding this comment.
We can remove the test since next one does the check.
There was a problem hiding this comment.
Alright, I have addressed these issues in another commit.
|
/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging |
|
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
| if (dictionary.Contains(propertyName)) | ||
| { | ||
| value = dictionary[propertyName].ToString(); | ||
| value = dictionary[propertyName]?.ToString(); |
There was a problem hiding this comment.
I wonder if value is null here, we should try GetToStringValueForProperty?
There was a problem hiding this comment.
If you know how to reproduce a scenario where this could be happen please open new issue.
|
@mikkas456 Thanks for your contribution! |
|
@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. |
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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header