Title: Unexpected behavior of AutomationNull in object initializers using [pscustomobject] · Issue #19552 · PowerShell/PowerShell · GitHub
Open Graph Title: Unexpected behavior of AutomationNull in object initializers using [pscustomobject] · Issue #19552 · PowerShell/PowerShell
X Title: Unexpected behavior of AutomationNull in object initializers using [pscustomobject] · Issue #19552 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/19552
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unexpected behavior of AutomationNull in object initializers using [pscustomobject]","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://docs.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nNote: Initializing via `[hashtable]` used to break too, but that was fixed in #19402 / #19415.\r\n\r\nIn an array-type-constrained variable (as in expressions in general), AutomationNull is treated like `$null`; e.g.:\r\n\r\n```powershell\r\n# Note: \u0026 { } is a simple way to return the AutomationNull singleton.\r\n# OK: no error\r\n[string[]] $arr = \u0026 { }\r\n```\r\n\r\nBy contrast, using AutomationNull in `[pscustomobject]`-based object initializers fails:\r\n\r\n```powershell\r\nclass Example {\r\n [string[]]$LogMessage\r\n}\r\n\r\n$automationNull = \u0026 {}\r\n\r\n# !! BREAKS: \"\"Type 'System.String[]' does not have a default constructor (Parameter 'type')\"\"\r\n[Example] [pscustomobject] @{ 'LogMessage' = $automationNull }\r\n```\r\n\r\nThe symptom is reminiscent of https://github.com/PowerShell/PowerShell/issues/19384#issuecomment-1478411266\r\n\r\n### Expected behavior\r\n \r\nAn `[Example]` instance should be constructed, with `$null` returned from its .LogMessage property.\r\n\r\nNote: 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`.\r\n\r\nOutside of a class this behavior is directly visible:\r\n\r\n```powershell\r\n# -\u003e $true\r\n [object]::ReferenceEquals((\r\n [string[]] $arr = \u0026 { }), \r\n [System.Management.Automation.Internal.AutomationNull]::Value\r\n)\r\n```\r\n\r\n### Actual behavior\r\n\r\n```console\r\nInvalidArgument: \r\nCannot convert value \"@{LogMessage=}\" to type \"Example\". \r\nError: \"Type 'System.String[]' does not have a default constructor (Parameter 'type')\"\r\n```\r\n\r\n\r\n### Error details\r\n\r\n_No response_\r\n\r\n### Environment data\r\n\r\n```powershell\r\nPowerShell Core 7.4.0-preview.3\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2023-04-20T19:13:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/19552/PowerShell/issues/19552"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8cd83c00-07bd-97f7-1dce-b07f734016fd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B792:3A642C:271CB:3440A:6A558228 |
| html-safe-nonce | 4bb6d2264d2fc5a0638abebff6e6e052d2408ef99ddf85f5e5e5ec1076ffecf3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzkyOjNBNjQyQzoyNzFDQjozNDQwQTo2QTU1ODIyOCIsInZpc2l0b3JfaWQiOiIyMTQ2NDE1Mjg5NTU0MDEwNjY0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ecbc9423c2f3a9039c641650449799e8256b1ac1ce2897142a761b91a23e21c6 |
| hovercard-subject-tag | issue:1677272416 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/19552/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d12f852193e0d652889cba69f442117296b3a84d06e9a61306af45843798ff84/PowerShell/PowerShell/issues/19552 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d12f852193e0d652889cba69f442117296b3a84d06e9a61306af45843798ff84/PowerShell/PowerShell/issues/19552 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b5665c84831ed9ac4fb79519c16c9c5580ba8092fb8bb6e3e72972ec7197348e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 617ea94e2807dc3052601c560530bb3f8259bc9a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width