Skip to content

ConvertTo-SmarterObject is flattening arrays #55

@atruskie

Description

@atruskie

Hi! When querying the issues using the Get-GitHubIssue command, sub-arrays seem to get flattened. I'm focusing on the labels array that's returned with each issue: sometimes it returns null, sometimes an array (✔️ good), and sometimes the array is flattened and a PSCustomObject is returned (❌ bad).

Test case setup

Two issues, one with multiple labels, one with a single label. I'm using issues in one of my repositories (assigned to the milestone test to make a subset easily filterable) but I believe any other repo will do.

Current behaviour

> (Get-GitHubIssue -OwnerName "QutEcoacoustics" -RepositoryName "audio-analysis" -Milestone 6 -State all) | ConvertTo-Json -Depth 100 | grep -C 3 'labels\": '
DONE - Operation took 4 second(s) [Getting issues for audio-analysis]
      "type": "User",
      "site_admin": false
    },
    "labels": [
      {
        "id": 151803416,
        "node_id": "MDU6TGFiZWwxNTE4MDM0MTY=",
--
      "type": "User",
      "site_admin": false
    },
    "labels": {
      "id": 91215873,
      "node_id": "MDU6TGFiZWw5MTIxNTg3Mw==",
      "url": "https://api.github.com/repos/QutEcoacoustics/audio-analysis/labels/enhancement",

Note that the issue with one label (the second grep result) has an object directly assigned to the labels field instead of an array.

Expected Behaviour

> ((Invoke-WebRequest "https://api.github.com/repos/QutEcoacoustics/audio-analysis/issues?milestone=6&state=all").Content | ConvertFrom-Json | ConvertTo-Json -Depth 100) | grep -C 3 'labels\": '
      "type": "User",
      "site_admin": false
    },
    "labels": [
      {
        "id": 151803416,
        "node_id": "MDU6TGFiZWwxNTE4MDM0MTY=",
--
      "type": "User",
      "site_admin": false
    },
    "labels": [
      {
        "id": 91215873,
        "node_id": "MDU6TGFiZWw5MTIxNTg3Mw==",

Note both labels fields contain an array.

Why is this a problem

If you're only using these values from within PowerShell then it isn't so important because PowerShell supports scalars or arrays pretty transparently.

However, if you have to use any of this data in another language/tool the inconsistent structure (that also deviates from GitHub's API) makes dealing with these values quite difficult.

Why I think the problem is in PowerShellForGitHub

Because essentially, right down in the guts of this module a Invoke-WebRequest and a ConvertFrom-Json are done, just like in my expected behaviour section above. That rules out those cmdlets as being out-right at fault... and I haven't had the time to properly trace the root cause.


Technical details:

  • The version of the module (located in PowerShellForGitHub.psd1)
    • 0.4.0
  • Your OS version
    • Windows 10.0.17134
  • Your version of PowerShell ($PSVersionTable.PSVersion)
    • 6.1.0
    • Also reproduced in 5.1.17134.407

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis relates to a bug in the existing module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions