diff --git a/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs b/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs index 206c0737c10..5a3207c446e 100644 --- a/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs +++ b/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs @@ -1048,10 +1048,25 @@ private void InferTypesFrom(CommandAst commandAst, List inferredType inferredTypes.AddRange(inferTypesFromObjectCmdlets); return; } + + if (cmdletInfo.ImplementingType.FullName.EqualsOrdinalIgnoreCase("Microsoft.PowerShell.Commands.GetRandomCommand") + && pseudoBinding.BoundArguments.TryGetValue("InputObject", out var value)) + { + if (value.ParameterArgumentType == AstParameterArgumentType.PipeObject) + { + InferTypesFromPreviousCommand(commandAst, inferredTypes); + } + else if (value.ParameterArgumentType == AstParameterArgumentType.AstPair) + { + inferredTypes.AddRange(InferTypes(((AstPair)value).Argument)); + } + + return; + } } // The OutputType property ignores the parameter set specified in the OutputTypeAttribute. - // With psuedo-binding, we actually know the candidate parameter sets, so we could take + // With pseudo-binding, we actually know the candidate parameter sets, so we could take // advantage of it here, but I opted for the simpler code because so few cmdlets use // ParameterSetName in OutputType and of the ones I know about, it isn't that useful. inferredTypes.AddRange(commandInfo.OutputType); diff --git a/test/powershell/engine/Api/TypeInference.Tests.ps1 b/test/powershell/engine/Api/TypeInference.Tests.ps1 index 0376abdadab..612912029a8 100644 --- a/test/powershell/engine/Api/TypeInference.Tests.ps1 +++ b/test/powershell/engine/Api/TypeInference.Tests.ps1 @@ -524,6 +524,24 @@ Describe "Type inference Tests" -tags "CI" { $res.Name | Should -Be "System.String" } + It "Infers typeof Get-Random with pipeline input" { + $res = [AstTypeInference]::InferTypeOf( { "Hello","World" | Get-Random }.Ast) + $res.Count | Should -Be 1 + $res.Name | Should -Be "System.String" + } + + It "Infers typeof Get-Random with astpair input" { + $res = [AstTypeInference]::InferTypeOf( { Get-Random -InputObject Hello,World }.Ast) + $res.Count | Should -Be 1 + $res.Name | Should -Be "System.String[]" + } + + It "Infers typeof Get-Random with no input" { + $res = [AstTypeInference]::InferTypeOf( { Get-Random }.Ast) + $res.Count | Should -Be 3 + $res.Name -join ', ' | Should -Be "System.Int32, System.Int64, System.Double" + } + It "Infers typeof Group-Object Group" { $res = [AstTypeInference]::InferTypeOf( { Get-ChildItem | Group-Object | ForEach-Object Group }.Ast) $res.Count | Should -Be 3