Skip to content

Commit 1d7651f

Browse files
pougetatadityapatwardhan
authored andcommitted
Fix Get-Help PSTypeName issue with -Parameter when only one parameter is declared (#8754)
1 parent 5097e2a commit 1d7651f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/System.Management.Automation/help/BaseCommandHelpInfo.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,19 @@ internal override PSObject[] GetParameter(string pattern)
393393
return base.GetParameter(pattern);
394394
}
395395

396+
// The Maml format simplifies array fields containing only one object
397+
// by transforming them into the objects themselves. To ensure the consistency
398+
// of the help command result we change it back into an array.
399+
var param = prmts.Properties["parameter"].Value;
400+
PSObject[] paramAsPSObjArray = new PSObject[1];
401+
402+
if (param is PSObject paramPSObj)
403+
{
404+
paramAsPSObjArray[0] = paramPSObj;
405+
}
406+
396407
PSObject[] prmtArray = (PSObject[])LanguagePrimitives.ConvertTo(
397-
prmts.Properties["parameter"].Value,
408+
paramAsPSObjArray[0] != null ? paramAsPSObjArray : param,
398409
typeof(PSObject[]),
399410
CultureInfo.InvariantCulture);
400411

test/powershell/engine/Help/HelpSystem.Tests.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,45 @@ Describe "Get-Help should accept arrays as the -Parameter parameter value" -Tags
503503
}
504504
}
505505

506+
Describe "Get-Help for function parameter should be consistent" -Tags 'CI' {
507+
BeforeAll {
508+
$test1 = @'
509+
function test1 {
510+
param (
511+
$First
512+
)
513+
}
514+
'@
515+
$test2 = @'
516+
function test2 {
517+
param (
518+
$First,
519+
$Second
520+
)
521+
}
522+
'@
523+
$test1Path = Join-Path $TestDrive "test1.ps1"
524+
Set-Content -Path $test1Path -Value $test1
525+
526+
$test2Path = Join-Path $TestDrive "test2.ps1"
527+
Set-Content -Path $test2Path -Value $test2
528+
529+
Import-Module $test1Path
530+
Import-Module $test2Path
531+
}
532+
533+
AfterAll {
534+
Remove-Module -Name "test1"
535+
Remove-Module -Name "test2"
536+
}
537+
538+
It "Get-Help for function parameter should be consistent" {
539+
$test1HelpPSType = (Get-Help test1 -Parameter First).PSTypeNames
540+
$test2HelpPSType = (Get-Help test2 -Parameter First).PSTypeNames
541+
$test1HelpPSType | Should -BeExactly $test2HelpPSType
542+
}
543+
}
544+
506545
Describe "Help failure cases" -Tags Feature {
507546
It "An error is returned for a topic that doesn't exist: <command>" -TestCases @(
508547
@{ command = "help" },

0 commit comments

Comments
 (0)