mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix the NullRefException when using '-PipelineVariable' with 'DynamicParam' block (#6433)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user