Fix the NullRefException when using '-PipelineVariable' with 'DynamicParam' block (#6433)

This commit is contained in:
Dongbo Wang
2018-03-20 12:02:46 -07:00
committed by GitHub
parent f7c2b7def1
commit 1b3c8aca50
2 changed files with 14 additions and 1 deletions
@@ -3698,7 +3698,9 @@ namespace System.Management.Automation
if (this.PipelineVariable != null)
{
this.OutputPipe.RemovePipelineVariable();
_state.PSVariable.Remove(this.PipelineVariable);
// '_state' could be null when a 'DynamicParam' block runs because the 'DynamicParam' block runs in 'DoPrepare',
// before 'PipelineProcessor.SetupParameterVariables' is called, where '_state' is initialized.
_state?.PSVariable.Remove(this.PipelineVariable);
}
}
}
@@ -283,6 +283,17 @@ Describe "Parameter Binding Tests" -Tags "CI" {
}
}
It "PipelineVariable shouldn't cause a NullRef exception when 'DynamicParam' block is present" {
function DynamicParamTest {
[CmdletBinding()]
param()
dynamicparam { }
process { 'hi' }
}
DynamicParamTest -PipelineVariable bar | ForEach-Object { $bar } | Should -Be "hi"
}
Context "Use automatic variables as default value for parameters" {
BeforeAll {
## Explicit use of 'CmdletBinding' make it a script cmdlet