Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
866 stars 92 forks source link

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

Closed mdaneri closed 1 month ago

mdaneri commented 1 month ago

Issue Reference

This pull request addresses the issue described in Bug #1417, where ConvertTo-PodeYaml fails when the source Hashtable or PSCustomObject contains a key named 'Count'.

Issue Description

The ConvertTo-PodeYaml function incorrectly assumes that .Count refers to the number of elements in the object, which causes a conflict if the object contains a key named 'Count'. This results in a failure during the YAML conversion process.

Affected File

Proposed Fix

To avoid conflicts with the 'Count' key, the following changes have been made:

Code Changes

- if ($InputObject.Count -gt 0 ) {
+ if ($InputObject.GetEnumerator().MoveNext()) {

- if ($InputObject.Count -gt 0 ) {
+ if ($InputObject.PSObject.Properties.Count -gt 0) {

Testing

The issue has been reproduced with a Hashtable and PSCustomObject containing a key named 'Count'. After applying the fix, the ConvertTo-PodeYaml function works as expected without any errors.

Example of Reproducing the Issue

Before the fix:

$data = @{
    Name  = 'Sample'
    Count = 10
}
ConvertTo-PodeYaml -InputObject $data

This would throw an error due to the key 'Count'.

After the fix, the same command should successfully convert the object to YAML.