PowerShell / ConsoleGuiTools

Modules that mix PowerShell and GUIs/CUIs!
https://www.powershellgallery.com/packages/Microsoft.PowerShell.ConsoleGuiTools
MIT License
759 stars 52 forks source link

`Show-ObjectTree` does not enumerate object data from PSObject converted from JSON via `ConvertFrom-JSON`. #242

Open RokeJulianLockhart opened 3 months ago

RokeJulianLockhart commented 3 months ago
  1. Prerequisites

  2. Steps to reproduce

    As https://github.com/orgs/PowerShell/discussions/21343#discussioncomment-8810840 states, it doesn't appear to parse the object:

    1. Initialize a variable as JSON

      ```PS1 $Values = @' { "Candidate": { "Name": { "3rd": "Surname", "1st": "Forename", "2nd": "Middle name" }, "Date of Birth": "+1582-10-15", "E-mail Address": "mailTo:address@domain.TLD", "Mobile Telephone Number": "tel:+00-0000-000000", "Blood Group": "" // Unknown }, "Mother": { "Name": { "3rd": "Surname", "1st": "Forename", "2nd": "Middle name" }, "Date of Birth": "+1582-10-15", "E-mail Address": "mailTo:address@domain.TLD", "Mobile Telephone Number": "tel:+00-0000-000000", }, "Father": { "Name": { "3rd": "Surname", "1st": "Forename", "2nd": "Middle name" }, "Date of Birth": "+1582-10-15", "E-mail Address": "mailTo:address@domain.TLD", "Mobile Telephone Number": "tel:+00-0000-000000", }, } '@ ```
    2. Pass the value via a pipe to Show-ObjectTree.

    Expected behavior

    It should display as Get-Process | Show-ObjectTree does:

    Screenshot_20240316_174327

  3. Actual behavior

    1. Visuals

        1. $Values | Show-ObjectTree
        2. Screenshot_20240316_104948

        1. $Values | ConvertFrom-JSON | Show-ObjectTree
        2. Screenshot_20240316_105413

        3. When that expansion indicator is invoked, it disappears:

          Screenshot_20240316_105424

  4. Error details

    Get-Error

    returns $Null.

  5. Environment data

    {
      "PSVersion": {
        "Major": 7,
        "Minor": 4,
        "Patch": 1,
        "PreReleaseLabel": null,
        "BuildLabel": null
      },
      "OS": "cpe:/o:opensuse:tumbleweed:20240314",
      "PSRemotingProtocolVersion": {
        "Major": 2,
        "Minor": 3,
        "Build": -1,
        "Revision": -1,
        "MajorRevision": -1,
        "MinorRevision": -1
      },
      "PSEdition": "Core",
      "GitCommitId": "7.4.1",
      "Platform": "Unix",
      "PSCompatibleVersions": [
        {
          "Major": 1,
          "Minor": 0,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        },
        {
          "Major": 2,
          "Minor": 0,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        },
        {
          "Major": 3,
          "Minor": 0,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        },
        {
          "Major": 4,
          "Minor": 0,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        },
        {
          "Major": 5,
          "Minor": 0,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        },
        {
          "Major": 5,
          "Minor": 1,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        },
        {
          "Major": 6,
          "Minor": 0,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        },
        {
          "Major": 7,
          "Minor": 0,
          "Build": -1,
          "Revision": -1,
          "MajorRevision": -1,
          "MinorRevision": -1
        }
      ],
      "WSManStackVersion": {
        "Major": 3,
        "Minor": 0,
        "Build": -1,
        "Revision": -1,
        "MajorRevision": -1,
        "MinorRevision": -1
      },
      "SerializationVersion": {
        "Major": 1,
        "Minor": 1,
        "Build": 0,
        "Revision": 1,
        "MajorRevision": 0,
        "MinorRevision": 1
      }
    }
  6. Version

    1. PowerShell

      snap install $(what-snap JSNnoJl3EqkMuWoy5Dgq8PMqZ0uNcpie | awk '{print $2}') --classic --channel=latest/stable --revision 264
    2. Microsoft.PowerShell.ConsoleGuiTools

      1. Get-InstalledModule -Name 'Microsoft.PowerShell.ConsoleGuiTools' | Select-Object -ExpandProperty 'Version'
      2. https://github.com/PowerShell/GraphicalTools/releases/tag/v0.7.6.0

tznind commented 3 months ago

The input object produced by $Values | ConvertFrom-JSON | Show-ObjectTree is a single System.Management.Automation.PSCustomObject

I am not familiar with this class and cannot seem to find many c# examples of how to work dynamically with the type. However it should be possible to update GetChildren and AspectGetter to behave better than at the moment (blank string no children).

I have started a Redit post to see if anyone knows how to work with this class in c# 🤞 but any insight here would be appreciated too.

These extension methods (see below) seem to behave badly at runtime too. Also I think they are a read herring. They are most likely just generic extension methods.

PS D:\Repos\GraphicalTools> $Values | ConvertFrom-JSON | Show-ObjectTree
Show-ObjectTree: Could not load file or assembly 'Namotion.Reflection, Version=2.0.10.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102'. The system cannot find the file specified.

image

tznind commented 3 months ago

I have made some progess and opened a WIP PR.

If done correctly this should also mean that other use cases for custom objects will work better. And the refactoring work in the PR will also make it more flexible to future changes.