Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior of AutomationNull in object initializers using [pscustomobject] #19552

Closed
5 tasks done
mklement0 opened this issue Apr 20, 2023 · 4 comments
Closed
5 tasks done
Labels
Needs-Triage The issue is new and needs to be triaged by a work group. Resolution-No Activity Issue has had no activity for 6 months or more

Comments

@mklement0
Copy link
Contributor

mklement0 commented Apr 20, 2023

Prerequisites

Steps to reproduce

Note: Initializing via [hashtable] used to break too, but that was fixed in #19402 / #19415.

In an array-type-constrained variable (as in expressions in general), AutomationNull is treated like $null; e.g.:

# Note: & { } is a simple way to return the AutomationNull singleton.
# OK: no error
[string[]] $arr = & { }

By contrast, using AutomationNull in [pscustomobject]-based object initializers fails:

class Example {
   [string[]]$LogMessage
}

$automationNull = & {}

# !! BREAKS: ""Type 'System.String[]' does not have a default constructor (Parameter 'type')""
[Example] [pscustomobject] @{ 'LogMessage' = $automationNull }

The symptom is reminiscent of #19384 (comment)

Expected behavior

An [Example] instance should be constructed, with $null returned from its .LogMessage property.

Note: If this is resolved in the same way as for hashtable initializers, it is actually [System.Management.Automation.Internal.AutomationNull]::Value that is stored in the property, but on accessing (getting) it, it turns to $null.

Outside of a class this behavior is directly visible:

# -> $true
 [object]::ReferenceEquals((
  [string[]] $arr = & { }), 
  [System.Management.Automation.Internal.AutomationNull]::Value
)

Actual behavior

InvalidArgument: 
Cannot convert value "@{LogMessage=}" to type "Example". 
Error: "Type 'System.String[]' does not have a default constructor (Parameter 'type')"

Error details

No response

Environment data

PowerShell Core 7.4.0-preview.3

Visuals

No response

@mklement0 mklement0 added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 20, 2023
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

1 similar comment
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 15, 2023
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage The issue is new and needs to be triaged by a work group. Resolution-No Activity Issue has had no activity for 6 months or more
Projects
None yet
Development

No branches or pull requests

1 participant