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

Conversation

mdaneri
Copy link
Contributor

@mdaneri mdaneri commented Oct 15, 2024

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

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

Proposed Fix

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

  • Line 3576: Replaced with if ($InputObject.GetEnumerator().MoveNext())
  • Line 3602: Replaced with if ($InputObject.PSObject.Properties.Count -gt 0)

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.

Copy link
Owner

@Badgerati Badgerati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Badgerati Badgerati added this to the 2.11.1 milestone Oct 21, 2024
@Badgerati Badgerati changed the title ConvertTo-PodeYaml Fails on 'Count' Key** Fix for ConvertTo-PodeYaml failing, when passed object contains a Key named "Count" Oct 21, 2024
@Badgerati Badgerati merged commit 22db5c5 into Badgerati:develop Oct 21, 2024
14 checks passed
@mdaneri mdaneri deleted the issue-1417 branch October 21, 2024 14:11
@Badgerati Badgerati mentioned this pull request Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue with ConvertTo-PodeYaml when Count is a Key in Hashtable or PSCustomObject
2 participants