From 1d7651f2336f3af5a21d3a889dabf3be8f19ffe8 Mon Sep 17 00:00:00 2001 From: pougetat Date: Wed, 30 Jan 2019 17:55:33 +0100 Subject: [PATCH] Fix `Get-Help` `PSTypeName` issue with `-Parameter` when only one parameter is declared (#8754) --- .../help/BaseCommandHelpInfo.cs | 13 ++++++- .../engine/Help/HelpSystem.Tests.ps1 | 39 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/help/BaseCommandHelpInfo.cs b/src/System.Management.Automation/help/BaseCommandHelpInfo.cs index a051b4b3e4c5..832abdfed1ef 100644 --- a/src/System.Management.Automation/help/BaseCommandHelpInfo.cs +++ b/src/System.Management.Automation/help/BaseCommandHelpInfo.cs @@ -393,8 +393,19 @@ internal override PSObject[] GetParameter(string pattern) return base.GetParameter(pattern); } + // The Maml format simplifies array fields containing only one object + // by transforming them into the objects themselves. To ensure the consistency + // of the help command result we change it back into an array. + var param = prmts.Properties["parameter"].Value; + PSObject[] paramAsPSObjArray = new PSObject[1]; + + if (param is PSObject paramPSObj) + { + paramAsPSObjArray[0] = paramPSObj; + } + PSObject[] prmtArray = (PSObject[])LanguagePrimitives.ConvertTo( - prmts.Properties["parameter"].Value, + paramAsPSObjArray[0] != null ? paramAsPSObjArray : param, typeof(PSObject[]), CultureInfo.InvariantCulture); diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index f1cf27f8b5c2..2b090a62a5ac 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -503,6 +503,45 @@ Describe "Get-Help should accept arrays as the -Parameter parameter value" -Tags } } +Describe "Get-Help for function parameter should be consistent" -Tags 'CI' { + BeforeAll { + $test1 = @' + function test1 { + param ( + $First + ) + } +'@ + $test2 = @' + function test2 { + param ( + $First, + $Second + ) + } +'@ + $test1Path = Join-Path $TestDrive "test1.ps1" + Set-Content -Path $test1Path -Value $test1 + + $test2Path = Join-Path $TestDrive "test2.ps1" + Set-Content -Path $test2Path -Value $test2 + + Import-Module $test1Path + Import-Module $test2Path + } + + AfterAll { + Remove-Module -Name "test1" + Remove-Module -Name "test2" + } + + It "Get-Help for function parameter should be consistent" { + $test1HelpPSType = (Get-Help test1 -Parameter First).PSTypeNames + $test2HelpPSType = (Get-Help test2 -Parameter First).PSTypeNames + $test1HelpPSType | Should -BeExactly $test2HelpPSType + } +} + Describe "Help failure cases" -Tags Feature { It "An error is returned for a topic that doesn't exist: " -TestCases @( @{ command = "help" },