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 nested PSCustomObjects in hashtables #169

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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