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
865 stars 92 forks source link

Issue with `ConvertTo-PodeYaml` when `Count` is a Key in Hashtable or PSCustomObject #1417

Closed mdaneri closed 1 month ago

mdaneri commented 1 month ago

Summary:

There is an issue in the ConvertTo-PodeYaml function in Pode that causes the conversion to fail if the source Hashtable or PSCustomObject contains a key named Count. This problem occurs due to the way the Count property is being checked during conversion, leading to conflicts when Count is a key in the object.

File:

Steps to Reproduce:

  1. Create a Hashtable or PSCustomObject with a key named Count.
  2. Attempt to convert the object to YAML using ConvertTo-PodeYaml.
  3. The conversion will fail due to a conflict with the Count property being treated as a method rather than a key.

Example:

$object = @{
    Count = 5
    Name = "TestObject"
}

ConvertTo-PodeYaml -InputObject $object

Root Cause:

The issue arises because the Count property is being accessed directly ($InputObject.Count), which conflicts when Count is used as a key in the Hashtable or PSCustomObject. The Count property is treated as a method in such cases, causing the conversion to fail.

Fix:

The proposed solution is to adjust the conditions to avoid using the direct Count property in situations where Count could be a key.

  1. Line 3576:

    • Current Code:
      if ($InputObject.Count -gt 0) {
    • Proposed Fix:
      if ($hashtable.GetEnumerator().MoveNext()) {
  2. Line 3602:

    • Current Code:
      if ($InputObject.Count -gt 0) {
    • Proposed Fix:
      if ($InputObject.PSObject.Properties.Count -gt 0) {

Additional Notes: