From 9eeee795e0ef2331cd28b435b716d572c5ec5303 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Sun, 15 Dec 2024 00:08:41 +0200 Subject: [PATCH] Fix serialization of PSCustomObject wrapped object With the switch away from converting PSCustomObjects to dictionaries we broke the way values that are wrapped in a PSCustomObject are serialized. This is evident when values of hashtables are generated using Write-Output which generates a PSCustomObject (Which is not evident in powershell, as there is a lot of magic in powershell), but is clearly evident when we try to use an object generated in PowerShell, in C#. This change looks at the nested type of a PSCustomObject, and unwraps it if it's not just another PSCustomObject. Fixes: #157 Signed-off-by: Gabriel Adrian Samfira --- Tests/powershell-yaml.Tests.ps1 | 15 +++++++++++++++ lib/net47/PowerShellYamlSerializer.dll | Bin 10240 -> 10240 bytes .../PowerShellYamlSerializer.dll | Bin 10240 -> 10240 bytes src/PowerShellYamlSerializer.cs | 12 +++++++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Tests/powershell-yaml.Tests.ps1 b/Tests/powershell-yaml.Tests.ps1 index db55a59..0c21647 100644 --- a/Tests/powershell-yaml.Tests.ps1 +++ b/Tests/powershell-yaml.Tests.ps1 @@ -28,6 +28,21 @@ Import-Module $modulePath InModuleScope $moduleName { $compareStrictly = Get-EquivalencyOption -Comparator Equality + Describe "Test PSCustomObject wrapped values are serialized correctly" { + Context "Wrapped values" { + It "Should serialize correctly" { + $expectBigInt = [System.Numerics.BigInteger]::Parse("9999999999999999999999999999999999999999999999999") + $obj = [PSCustomObject]@{a = Write-Output 'string'; b = Write-Output 1; c = Write-Output @{nested = $true};d = [pscustomobject]$expectBigInt} + $asYaml = ConvertTo-Yaml $obj + $fromYaml = ConvertFrom-Yaml $asYaml + + Assert-Equivalent -Options $compareStrictly -Expected "string" -Actual $fromYaml["a"] + Assert-Equivalent -Options $compareStrictly -Expected 1 -Actual $fromYaml["b"] + Assert-Equivalent -Options $compareStrictly -Expected $expectBigInt -Actual $fromYaml["d"] + } + } + } + Describe "Test encode-decode symmetry." { Context "Simple-Items" { diff --git a/lib/net47/PowerShellYamlSerializer.dll b/lib/net47/PowerShellYamlSerializer.dll index 5adca39adcbc88513b95c9736b3a7fdb21fff8af..e0d04d917597a02dc132a403cb69d8ebd771a70a 100644 GIT binary patch delta 2731 zcmZWr4QvzV8GhgI&UZd1apFq?A#sfJN1R{m{F^ig64ny7)v5SdGfHXVq=6AIAqhW= z!0s4nHUT3!QI;~KMJrNS$3zvi6HJI^65XGwHkGC>5~Hv&CRDKzZFHks$DTVohH7`Z z=X;;${rO(sec$K0#b8UOJnr`kdkA{?uTGUV}s%+wc%Qo*yH zAlhIQh)ScAOT`QM#<%#;l4eAk$68#1Y;>WRsAn+|8?&B9qeJLiW`xDZ>@nJ;suUNS zQRI?`JjRHW>v<1ZN~D0uk)lQlF;qPzMkFG6#WXy|1*w_$0dt~pZv@31sc}0kJ`8&> zSElTeD{EYGq!fND=z|8xJ{{EO15{=|?o3UPG^ecDBPH-Fkuu;4-@}IwuRD}VMTwMy zUy}waFhuewN)8Cb8D{%#`uN}E0Hs19`6>}%`ZE0z9mk07T~egV%u5GUNsB5n61*DR zw;bS5a;sHksa{=J(fL^H*=Dn8KLRRSr6rL7jEbP;kH(8dmOx55B;y9Dks32jTZq%u zrH)7~r0jqyaLSgZ|)G>EkW(>QJO@Ji#7t&CS0w}7h+ zrxN5=W395PWxt)M=n&B@j7K%t>xcGi-_w4$drwy(iB z27b-Bs+6MUU1c$<+0^rV-}{i>TYUqi$VRsl9Di=n_e+P`?(i zJ17W6r#4zA>og&oc-L<1vcH~xkYr57xUSfU4q|C%T5Q~8My*!T_bD%kzv*;_7t8|NH z)Q_#Z&~7;mSu8$bImN!<8?)G9?66&;oK7TI1??3TbSA-0(;-nwXA^9eMg=w;ZmF!A zxJ|>6&cnx)l>L{+5!@AtNodEaCmVRWu$M zGTKu|qFsEOL)6LVr7bi^ZqY^Cc~tAC0pI|=N%PP`8{UHWfAWWP4)GtMrm9^>{B?T?&Cq`B&vY@$Pil8* zhO>l1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