diff --git a/src/System.Management.Automation/engine/CoreAdapter.cs b/src/System.Management.Automation/engine/CoreAdapter.cs index fe38535d1b..70e25a3a1f 100644 --- a/src/System.Management.Automation/engine/CoreAdapter.cs +++ b/src/System.Management.Automation/engine/CoreAdapter.cs @@ -4887,14 +4887,8 @@ namespace System.Management.Automation /// instructs the adapter to convert before setting, if the adapter supports conversion protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible) { - string valueString = setValue as string; - if (valueString == null) - { - throw new SetValueException("XmlNodeSetShouldBeAString", - null, - ExtendedTypeSystem.XmlNodeSetShouldBeAString, - property.Name); - } + // XML is always a string so implicitly convert to string + string valueString = LanguagePrimitives.ConvertTo(setValue); XmlNode[] nodes = (XmlNode[])property.adapterData; Diagnostics.Assert(nodes.Length != 0, "DoGetProperty would not return an empty array, it would return null instead"); if (nodes.Length > 1) diff --git a/test/powershell/engine/ETS/Adapter.Tests.ps1 b/test/powershell/engine/ETS/Adapter.Tests.ps1 index e46021d9e3..9d74d914ac 100644 --- a/test/powershell/engine/ETS/Adapter.Tests.ps1 +++ b/test/powershell/engine/ETS/Adapter.Tests.ps1 @@ -80,3 +80,31 @@ Describe "Adapter Tests" -tags "CI" { $val | Should Be "book" } } + +Describe "Adapter XML Tests" -tags "CI" { + BeforeAll { + [xml]$x = "" + $testCases = + @{ rval = @{testprop = 1}; value = 'a hash (psobject)' }, + @{ rval = $null; value = 'a null (codemethod)' }, + @{ rval = 1; value = 'a int (codemethod)' }, + @{ rval = "teststring"; value = 'a string (codemethod)' }, + @{ rval = @("teststring1", "teststring2"); value = 'a string array (codemethod)' }, + @{ rval = @(1,2); value = 'a int array (codemethod)' }, + @{ rval = [PSObject]::AsPSObject(1); value = 'a int (psobject wrapping)' }, + @{ rval = [PSObject]::AsPSObject("teststring"); value = 'a string (psobject wrapping)' }, + @{ rval = [PSObject]::AsPSObject([psobject]@("teststring1", "teststring2")); value = 'a string array (psobject wrapping)' }, + @{ rval = [PSObject]::AsPSObject(@(1,2)); value = 'int array (psobject wrapping)' } + } + + Context "Can set XML node property to non-string object" { + It "rval is " -TestCases $testCases { + # rval will be implicitly converted to 'string' type + param($rval) + { + { $x.root.data = $rval } | Should Not Throw + $x.root.data | Should Be [System.Management.Automation.LanguagePrimitives]::ConvertTo($rval, [string]) + } + } + } +}