Skip to content

Commit

Permalink
Merge pull request #169 from gabriel-samfira/fix-nested-pscustomobjects
Browse files Browse the repository at this point in the history
Fix nested PSCustomObjects in hashtables
  • Loading branch information
gabriel-samfira authored Dec 17, 2024
2 parents f0da96d + 6270cab commit ff6fce6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Tests/powershell-yaml.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
Binary file modified lib/net47/PowerShellYamlSerializer.dll
Binary file not shown.
Binary file modified lib/netstandard2.1/PowerShellYamlSerializer.dll
Binary file not shown.
9 changes: 8 additions & 1 deletion src/PowerShellYamlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit ff6fce6

Please sign in to comment.