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

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

Closed
mdaneri opened this issue Oct 15, 2024 · 0 comments · Fixed by #1418
Closed

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

mdaneri opened this issue Oct 15, 2024 · 0 comments · Fixed by #1418
Labels
Milestone

Comments

@mdaneri
Copy link
Contributor

mdaneri commented Oct 15, 2024

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:

  • File Name: Helper.ps1
  • Location:
    • Line 3576: if ($InputObject.Count -gt 0) {
    • Line 3602: if ($InputObject.Count -gt 0) {

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:

  • The fix ensures that when Count is used as a key, it will not conflict with the Count property of the object, and the conversion to YAML will work as expected.
  • The proposed fix uses .GetEnumerator().MoveNext() to check for the presence of elements in a Hashtable, and .PSObject.Properties.Count to properly count the properties in a PSCustomObject.
@mdaneri mdaneri changed the title Issue with ConvertTo-PodeYaml when Count is a Key in Hashtable or PSCustomObject** Issue with ConvertTo-PodeYaml when Count is a Key in Hashtable or PSCustomObject Oct 19, 2024
@Badgerati Badgerati added this to the 2.11.1 milestone Oct 21, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in 🚀 Pode Roadmap Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants