Skip to content

CSV deserialization outputs nothing if there are only a certain number of empty fields #24698

Open

Description

@surfingoldelephant

Prerequisites

If CSV input data doesn't satisfy an uncommunicated threshold regarding the number of columns and empty quoted/unquoted fields, CSV deserialization using ConvertFrom-Csv/Import-Csv yields no output. No error/warning is emitted either; the input is effectively ignored.

Steps to reproduce

Test 1

In the following examples, no custom object is emitted by ConvertFrom-Csv irrespective of row count.

# No custom object output, irrespective of row count.

# Example 1a:
@'
"P1"
""
'@ | ConvertFrom-Csv

# Example 1b:
@'
"P1"
""
""
""
'@ | ConvertFrom-Csv

Tests 2-3

In the following examples, a custom object is emitted for all rows except the last.

# Partial custom object output. 
# Output for each row except the last.
# If there is only one row, no object is emitted.

# Example 2a:
@'
"P1","P2"
"",
'@ | ConvertFrom-Csv # Nothing

# Example 2b:
@'
"P1","P2"
"",
"",
'@ | ConvertFrom-Csv # 1 custom object

# Example 3a:
@'
"P1","P2"
,
'@ | ConvertFrom-Csv # Nothing

# Example 3b:
@'
"P1","P2"
,
,
'@ | ConvertFrom-Csv # 1 custom object

Tests 4-6

Custom object output is correct in following examples.

# Custom object output in examples 4-6.

# Example 4a:
@'
"P1","P2"
,""
'@ | ConvertFrom-Csv

# Example 4b:
@'
"P1","P2"
,""
,""
'@ | ConvertFrom-Csv

# Example 5a:
@'
"P1","P2"
"",""
'@ | ConvertFrom-Csv

# Example 5b:
@'
"P1","P2"
"",""
"",""
'@ | ConvertFrom-Csv

# Example 6a:
@'
"P1","P2","P3"
,,
'@ | ConvertFrom-Csv

# Example 6b:
@'
"P1","P2","P3"
,,
,,
'@ | ConvertFrom-Csv

Note:

Expected behavior

  • Test 1: Should emit a custom object with a P1 property.
  • Tests 2a & 3a: Should emit a custom object with a P1 and P2 property.
  • Tests 2b & 3b: Should emit 2 custom objects, each with a P1 and P2 property.

Actual behavior

  • Test 1: No custom object output.
  • Tests 2-3: Partial custom object output for all rows except the last.

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.0-rc.1
PSEdition                      Core
GitCommitId                    7.5.0-rc.1
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Needs-Triage
    on Dec 22, 2024
  2. added
    WG-NeedsReview
    WG-Cmdlets
    on Dec 23, 2024
  3. changed the title [-]CSV deserialization outputs nothing if there are too few empty fields[/-] [+]CSV deserialization outputs nothing if there are only a certain number of empty fields[/+] on Dec 25, 2024
  4. TobiasPSP commented on Feb 19, 2025

    @TobiasPSP
    Collaborator

    The last data set is missing: @' "P1","P2" , , '@ | ConvertFrom-Csv | Format-List

  5. TobiasPSP commented on Feb 19, 2025

    @TobiasPSP
    Collaborator
    @'
    "P1","P2"
    ,
    ,
    '@ | ConvertFrom-Csv | Format-List
    
  6. SteveL-MSFT commented on Feb 19, 2025

    @SteveL-MSFT
    Member

    The @PowerShell/wg-powershell-cmdlets reviewed this, based on experimentation it appears that if the last row is simply commas (indicating empty columns), then the last object isn't emitted. If other rows are empty, they do get emitted as empty objects and it only affects the last row. We think this is a bucket 3 change as it seems unlikely users are depending on the current behavior which is unpredictable for the user. Correctly emitting an empty object doesn't seem like that would cause a problem as it would be behaving correctly.

  7. added
    Issue-Bug
    WG-Reviewed
    and removed
    Needs-Triage
    WG-NeedsReview
    on Feb 19, 2025
  8. surfingoldelephant commented on Feb 19, 2025

    @surfingoldelephant
    Author

    if the last row is simply commas (indicating empty columns), then the last object isn't emitted.

    Note that with the following, there's no output at all. Custom object output is absent for all rows.

    @'
    "P1"
    ""
    ""
    ""
    '@ | ConvertFrom-Csv # Nothing

    I've updated the issue to note that example #2 and #3 produce output for all rows except the last. #1 as shown above does not produce output irrespective of the row count.

  9. jshigetomi commented on Feb 26, 2025

    @jshigetomi
    Collaborator

    Are we expecting a header only output when we have no values?

  10. added
    In-PR
    on Feb 26, 2025
  11. surfingoldelephant commented on Feb 26, 2025

    @surfingoldelephant
    Author

    Are we expecting a header only output when we have no values?

    For each row in the CSV data:

    For example:

    @'
    "P1"
    ""
    ""
    '@ | ConvertFrom-Csv
    
    # Expected:
    # - 2 custom objects, both with a P1 NoteProperty.
    # - P1's value should be an empty string for both objects.
    
    # In PowerShell code:
    [pscustomobject] @{ P1 = '' }
    [pscustomobject] @{ P1 = '' }
    @'
    "P1","P2"
    "",
    "",
    '@ | ConvertFrom-Csv
    
    # Expected:
    # - 2 custom objects, both with P1 & P2 NoteProperties.
    # - For both objects, P1's value should be an empty string.
    # - For the first object, P2's value should be an empty string.
    # - For the last object, P2's value should be $null.
    
    # In PowerShell code:
    [pscustomobject] @{ P1 = ''; P2 = '' }
    [pscustomobject] @{ P1 = ''; P2 = $null }

    The asymmetry between setting an empty string/$null obviously increases complexity, but if #17702 isn't going to be fixed, unconditionally setting an empty string is technically a breaking change.

  12. surfingoldelephant commented on Feb 26, 2025

    @surfingoldelephant
    Author

    Justin Chung (@jshigetomi) I've created a set of Pester test cases to verify the fix: https://gist.github.com/surfingoldelephant/314d572953fa64555e0f9cba3c9b37aa

    As expected, tests 4-6 already pass based on existing behavior.

    Gist
    Tests to verify the fix for https://github.com//issues/24698 - PSIssue24698.Tests.ps1
  13. jshigetomi commented on Feb 26, 2025

    @jshigetomi
    Collaborator

    surfingoldelephant Thanks for the clarification. I think I got it to behave as you described. Please check it out. #25096

    edit: Amazing! Thanks for providing the pester tests. I did not see it until I refreshed this page. I will add those to the tests. For now they all pass with the new modifications.

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

Metadata

Metadata

Labels

In-PRIssue-BugWG-CmdletsWG-Reviewed

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions