Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for ConvertTo-PodeYaml failing, when passed object contains a Key named "Count" #1418

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/Private/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3573,7 +3573,7 @@ function ConvertTo-PodeYamlInternal {
}

'hashtable' {
if ($InputObject.Count -gt 0 ) {
if ($InputObject.GetEnumerator().MoveNext()) {
$index = 0
$string = [System.Text.StringBuilder]::new()
foreach ($item in $InputObject.Keys) {
Expand All @@ -3599,7 +3599,7 @@ function ConvertTo-PodeYamlInternal {
}

'pscustomobject' {
if ($InputObject.Count -gt 0 ) {
if ($InputObject.PSObject.Properties.Count -gt 0) {
$index = 0
$string = [System.Text.StringBuilder]::new()
foreach ($item in ($InputObject | Get-Member -MemberType Properties | Select-Object -ExpandProperty Name)) {
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/Helpers.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,51 @@ Describe 'ConvertTo-PodeYamlInternal Tests' {
}
}


# Test case for a hashtable containing a key named 'Count'
Context "When a hashtable contains a 'Count' key" {

It "Should convert the hashtable to YAML without error" {
# Arrange
$hashtable = @{
Name = 'Sample'
Count = 10
}

# Act
$result = { ConvertTo-PodeYamlInternal -InputObject $hashtable -NoNewLine} | Should -Not -Throw

# Assert
$yaml = ConvertTo-PodeYamlInternal -InputObject $hashtable -NoNewLine

# Check if YAML conversion includes both 'Name' and 'Count' keys in the YAML output
$yaml | Should -Match "Name: Sample"
$yaml | Should -Match "Count: 10"
}
}

# Test case for a PSCustomObject containing a key named 'Count'
Context "When a PSCustomObject contains a 'Count' property" {

It "Should convert the PSCustomObject to YAML without error" {
# Arrange
$object = [pscustomobject]@{
Name = 'Sample'
Count = 20
}

# Act
$result = { ConvertTo-PodeYamlInternal -InputObject $object -NoNewLine} | Should -Not -Throw

# Assert
$yaml = ConvertTo-PodeYamlInternal -InputObject $object -NoNewLine

# Check if YAML conversion includes both 'Name' and 'Count' properties in the YAML output
$yaml | Should -Match "Name: Sample"
$yaml | Should -Match "Count: 20"
}
}

Context 'Error handling' {
It 'Returns empty string for null input' {
$result = ConvertTo-PodeYamlInternal -InputObject $null
Expand Down