From 6270cab03c0e5824dcdc43155f672b01f8ad3e83 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Tue, 17 Dec 2024 12:40:00 +0200 Subject: [PATCH] Fix nested PSCustomObjects in hashtables This change properly unwraps only PSCustomObjects in hash tables before serializing, leaving PScustomObjects to the root serializer for further processing. Signed-off-by: Gabriel Adrian Samfira --- Tests/powershell-yaml.Tests.ps1 | 18 ++++++++++++++++++ lib/net47/PowerShellYamlSerializer.dll | Bin 11264 -> 11264 bytes .../PowerShellYamlSerializer.dll | Bin 11264 -> 11264 bytes src/PowerShellYamlSerializer.cs | 9 ++++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Tests/powershell-yaml.Tests.ps1 b/Tests/powershell-yaml.Tests.ps1 index 94ba896..dcdac17 100644 --- a/Tests/powershell-yaml.Tests.ps1 +++ b/Tests/powershell-yaml.Tests.ps1 @@ -725,9 +725,17 @@ int64: 9223372036854775807 $nestedPsO = [PSCustomObject]@{ Nested = 'NestedValue' } + $nestedHashTable = @{ + "aKey" = $nestedPsO + } + $nestedArray = @( + $nestedPsO + ) $PsO = [PSCustomObject]@{ Name = 'Value' Nested = $nestedPsO + NestedHashTable = $nestedHashTable + NestedArray = $nestedArray NullValue = $null } @@ -746,6 +754,16 @@ int64: 9223372036854775807 $ret["PsO"]["Name"] = "Value" $ret["PsO"]["Nested"] = [System.Collections.Specialized.OrderedDictionary]::new() $ret["PsO"]["Nested"]["Nested"] = "NestedValue" + $ret["PsO"]["NestedHashTable"] = [ordered]@{ + "aKey" = [ordered]@{ + "Nested" = "NestedValue" + } + } + $ret["PsO"]["NestedArray"] = @( + [ordered]@{ + "Nested" = "NestedValue" + } + ) $ret["PsO"]["NullValue"] = $null $ret["Ok"] = "aye" Assert-Equivalent -Options $compareStrictly -Expected $ret -Actual $result diff --git a/lib/net47/PowerShellYamlSerializer.dll b/lib/net47/PowerShellYamlSerializer.dll index 24af47d5423ffb1ad59967c7146a2e79f7e7ae87..ad5b86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as8Q8+nA#DKmrYGpjl|FTj?MbA4Rrk6$it P5Mq@t?x?s+Lq6-jo7ivD diff --git a/src/PowerShellYamlSerializer.cs b/src/PowerShellYamlSerializer.cs index 6bb4063..09b0616 100644 --- a/src/PowerShellYamlSerializer.cs +++ b/src/PowerShellYamlSerializer.cs @@ -84,8 +84,15 @@ public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerialize continue; } serializer(entry.Key, entry.Key.GetType()); + var objType = entry.Value.GetType(); + var val = entry.Value; if (entry.Value is PSObject nestedObj) { - serializer(nestedObj.BaseObject, nestedObj.BaseObject.GetType()); + var nestedType = nestedObj.BaseObject.GetType(); + if (nestedType != typeof(System.Management.Automation.PSCustomObject)) { + objType = nestedObj.BaseObject.GetType(); + val = nestedObj.BaseObject; + } + serializer(val, objType); } else { serializer(entry.Value, entry.Value.GetType()); }