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

Display issue with hashtable: only the first field is shown and if it's null it looks like it's empty #19354

Open
5 tasks done
cnotin opened this issue Mar 16, 2023 · 1 comment
Assignees
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine-Format

Comments

@cnotin
Copy link

cnotin commented Mar 16, 2023

Prerequisites

Steps to reproduce

Run these commands:

@{ id = "id"; KeyCredentials = @(@{ Key = $null; DisplayName = "CN=MyServicePrincipalCert"})} | select KeyCredentials
@{ id = "id"; KeyCredentials = @(@{ Key = "Test Key"; DisplayName = "CN=MyServicePrincipalCert"})} | select KeyCredentials

In the first command output, it looks like the KeyCredentials is empty whereas it isn't. I think it's because it shows its first field, Key, which is $null
But this is surprising in my opinion, and it led me to thinking there was a bug in the Microsoft Graph PowerShell SDK whereas there wasn't any: microsoftgraph/msgraph-sdk-powershell#1849

The second command is a more normal case, and its output looks more natural to me.

Expected behavior

PS C:\> @{ id = "id"; KeyCredentials = @(@{ Key = $null; DisplayName = "CN=MyServicePrincipalCert"})} | select KeyCredentials

KeyCredentials
--------------
{$null}
# or perhaps:
# Key = $null; DisplayName = "CN=MyServicePrincipalCert"
# or even the second field considering the first one is $null:
# DisplayName = "CN=MyServicePrincipalCert"

PS C:\> @{ id = "id"; KeyCredentials = @(@{ Key = "Test Key"; DisplayName = "CN=MyServicePrincipalCert"})} | select KeyCredentials

KeyCredentials
--------------
{Test Key}

Actual behavior

PS C:\> @{ id = "id"; KeyCredentials = @(@{ Key = $null; DisplayName = "CN=MyServicePrincipalCert"})} | select KeyCredentials

KeyCredentials
--------------
{}
# this {} is surprising to me. Is the array empty? Is there nothing? No! There's something

PS C:\> @{ id = "id"; KeyCredentials = @(@{ Key = "Test Key"; DisplayName = "CN=MyServicePrincipalCert"})} | select KeyCredentials

KeyCredentials
--------------
{Test Key}

Workaround

Instead of using | select, if I access the property directly the hashtable output is clearer:

PS C:\> $Object1 = New-object psobject -Property @{ id = "id"; KeyCredentials = @(@{ Key = $null; DisplayName = "CN=MyServicePrincipalCert"})}
PS C:\> $object1.KeyCredentials

Name                           Value
----                           -----
DisplayName                    CN=MyServicePrincipalCert
Key

Error details

None

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0-preview.1
PSEdition                      Core
GitCommitId                    7.4.0-preview.1
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

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

Conceptually related:

I haven't looked at the source code, but it appears that there is hard-coded logic in the formatting system for properties whose array elements happen to be of type [hashtable]: they are rendered by the value of an entry whose key is 'Key', if present, and otherwise by the value of whatever happens to be the first entry in the (inherently unordered) hash table.

By contrast, [ordered] hashtables and [System.Collections.Generic.Dictionary[T1, T2]] instances (unhelpfully) stringify by their type name (i.e. they are rendered with .ToString()).

@vexx32 vexx32 added the Issue-Enhancement the issue is more of a feature request than a bug label Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine-Format
Projects
None yet
Development

No branches or pull requests

5 participants